Spinner With List View
Solution 1:
Use following code i have tried it is working fine
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
publicclassMainActivityextendsActivityimplementsOnItemSelectedListener {
privateSpinner sp;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uI();
spElements();
}
publicvoiduI() {
sp = (Spinner) findViewById(R.id.mydsp);
sp.setOnItemSelectedListener(this);
}
publicvoidspElements() {
List<String> months = newArrayList<String>();
months.add("June");
months.add("July");
months.add("August");
months.add("September");
months.add("October");
months.add("November");
months.add("December");
// Creating adapter for spinnerArrayAdapter<String> dataAdapter = newArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, months);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp.setAdapter(dataAdapter);
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@OverridepublicvoidonItemSelected(AdapterView<?> main, View view, int position,
long Id) {
String item = main.getItemAtPosition(position).toString();
int index = item.getSelectedItemPosition();
Toast.makeText(main.getContext(), "You selected Month is: " + item,
Toast.LENGTH_LONG).show();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Solution 2:
Showing custom data in your ListView
is actually not very complicated. There are several steps involved.
What to show This is quite easy. When a user selects an item from your spinner, you know what to show; the month he selected. Now, as your months are actual classes, I would recommend you implement a variable that lets you know what is selected. That variable will be used by other classes to actually show the content.
How to show
Here is where it gets more tricky. Your ListView
will show a list. The items contained in the list need to be set by you. You need to define what data from your month class you will actually want to show. And here is where your design falls a bit short. The items you would want to show would ideally need to be homogeneous. So it would be easier to visualise a list of month objects then of several different items from one month. What I propose is that you switch your implementation from ListView
to a simple TextView
inside a ScrollView
.
Show the data
With the above modification, every time a selection is made, visualise it. Now here is where you can apply a little trick. Simply implement a toString()
method in your object that will return, as a String
what you want to show to the user. That way you can control what a user sees and leave the responsibility of what to show with the month object which is generally a good design choice.
I hope you will consider this new design. I believe you erred in trying to use ListView
, ArrayAdapter
, and the ViewHolder pattern when it wasn't necessary. Also, you will need to pre-load all month objects or use, as David says, an AsyncTask
to retrieve the data. Never block your UI thread with network operations. Not only is it a bad practice, newer versions of Android will throw an exception.
Solution 3:
Try this:
When any Month is selected then try to set adapter for Listview. like this:
s1.setOnItemSelectedListener(newOnItemSelectedListener()
{
publicvoidonItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
intindex= s1.getSelectedItemPosition();
if(index>0)
{
StringMonth= MonthArray[index];
// get data as per your month here.//set your adapter for listview here according to your month data.
}
}
publicvoidonNothingSelected(AdapterView<?>arg0) {}
});
If you want to load data from server and then want to change the adapter then i would suggest that you use AsyncTask for loading data. and on completion of data load set adapter of Listview.
Hope it Helps!!
Solution 4:
Here is one example for you
spinner.setOnItemSelectedListener(newOnItemSelectedListener()
{
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
StringA= arg0.getSelectedItem().toString();
newabc(Context, A).excute();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0)
{
}
});
privateclassabcextendsAsyncTask<Void, Void, HashMap<String, String>>
{
ProgressDialog pd;
HashMap<String, String> map;
String Month;
abc(Context context, String a)
{
pd = newProgressDialog(context);
map = newHashMap<String, String>();
Month = a;
}
protectedvoidonPreExecute()
{
pd.setTitle("Please Wait...");
pd.setMessage("Saving...");
pd.setCancelable(false);
pd.show();
}
protectedvoidonPostExecute(HashMap<String, String> result)
{
// Get value from map and sent it to your adapter class where you can set value to textview in your listviewStringtitle= result.get(KEY_TITLE);
ListAdapteradapter=newSimpleAdapter(this, menuItems,
R.layout.list_item, newString[] { KEY_TITLE, KEY_DESC,
KEY_COUNTRY_NAME, KEY_VENUE, KEY_LATITUDE,
KEY_LONGITUDE, KEY_TIME, }, newint[] { R.id.title,
R.id.description, R.id.countryName, R.id.venueName,
R.id.lat, R.id.lng, R.id.startTime });
setListAdapter(adapter);
// selecting single ListView itemListViewlv= getListView();
if(pd.isShowing()) pd.dismiss();
}
@Overrideprotected HashMap<String , String> doInBackground(Void... params)
{
XMLParserparser=newXMLParser();
Stringxml= parser.getXmlFromUrl(URL); // getting XMLDocumentdoc= parser.getDomElement(xml); // getting DOM elementNodeListnl= doc.getElementsByTagName(KEY_EVENT);
// looping through all item nodes <item>for (inti=0; i < nl.getLength(); i++) {
// creating new HashMapElemente= (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_URL, parser.getValue(e, KEY_URL));
map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
if (parser.getValue(e, KEY_STOP_TIME) != "") {
map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME) + "-"
+ parser.getValue(e, KEY_STOP_TIME));
} else {
map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME));
}
map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
map.put(KEY_VENUE_ADDRESS, parser.getValue(e, KEY_VENUE_ADDRESS));
map.put(KEY_LATITUDE, parser.getValue(e, KEY_LATITUDE));
map.put(KEY_LONGITUDE, parser.getValue(e, KEY_LONGITUDE));
map.put(KEY_VENUE, parser.getValue(e, KEY_VENUE_NAME) + ", "
+ parser.getValue(e, KEY_VENUE_ADDRESS));
// adding HashList to ArrayList
menuItems.add(map);
}
return map;
}
}
Hope it helps you.
Post a Comment for "Spinner With List View"