Listview & Custom Listview
I need help on how to change my code below to use my own XML list view. The items 'label', 'title', & 'discription' in my cursor needs to be inflated into itemLabel, itemTitle,
Solution 1:
The basic process is to create a Custom Adapter which will contain the layout R.layout.list_item
Thus, you replace this line setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results));
in your code
by
setListAdapter(newCustomAdapter(this,R.layout.list_item, results));
Now you need to create a CustomAdapter which extends ArrayAdapter or BaseAdapter and override the getView method to inflate R.layout.list_item
Please refer to this excellent tutorial by Mark Murphy Custom ListView Adapter
If you do have any other doubts after trying this, please post it over here.
Solution 2:
Everything is fine, just add a class extends from ArrayAdapter and do something like this:
publicclassCustomAdapterextendsArrayAdapter<String>
{
List<String> Title= newArrayList<String>();
List<String> Label= newArrayList<String>();
Context myContext;
publicCustomAdapter(Context context, int resource,
int textviewresourceid,
List<String> aTitle,
List<String> aLabel)
{
super(context, resource,textviewresourceid,aType);
myContext = context;
Title= aTitle;
Label= aLabel;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent)
{
Viewv= convertView;
if (v == null)
{
LayoutInflatervi= (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_view, null);
}
TextViewtv_title= (TextView) v.findViewById(R.id.id_tv_Title);
TextView tv_Label= (TextView) v.findViewById(R.id.id_tv_Label);
if(tv_title != null)
{
tv_title.setText(Title.get(position));
}
if(tv_Label != null)
{
tv_Label.setText(Label.get(position));
}
return v;
}
And then use this adapter like:
CustomAdapteradapter=newCustomAdapter(getAppContext(),R.layout.list_view,Textview Title Id,your lists...);
setListAdapter(adapter);
Something like this.... Hope it helps...
Solution 3:
I have the correct anwser HERE along with the correct code.
Post a Comment for "Listview & Custom Listview"