How To Add An Icon To A Listview In A Fragment
I made a listview in a fragment. Now I need to add a simple icon to the listview items. The icon must be the same on every item. This will be a simple arrow (imageview). Is this po
Solution 1:
In onCreateView
ListViewlist= (ListView)view.findViewById(R.id.listView1);
CustomAdaptercus=newCustomAdapter(getActivity(),items);
list.setAdapter(cus);
Use a custom adapter
publicclassCustomAdapterextendsBaseAdapter {
String items[];
LayoutInflater mInflater;
publicCustomAdapter(Context context, String[] items) {
mInflater = LayoutInflater.from(context);
this.items = items;
}
@OverridepublicintgetCount() {
return items.length;
}
@Overridepublic Object getItem(int position) {
return position;
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView ==null)
{
convertView = mInflater.inflate(R.layout.list_item,parent,false);
holder = newViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(items[position])
// use holder.iv to set whatever image you want according to the positionreturn convertView;
}
staticclassViewHolder
{
ImageView iv;
TextView tv;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="42dp"android:layout_marginTop="32dp"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/imageView1"android:layout_marginLeft="64dp"android:layout_marginTop="14dp"android:layout_toRightOf="@+id/imageView1"android:text="TextView" /></RelativeLayout>
Snap
Edit:
If you feel this is over kill you can use a compound textview with image on the left and text on the right.
In the above I have used list_item.xml
which is a custom layout. I inflate that layout in getView
of custom adapter. I set the text to textview. I have set the default launcher icon to imageview. You can change it though.
I have also used a ViewHolder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Also check
Solution 2:
Create ListAdapter
publicclassListAdapterextendsArrayAdapter<Item> {
publicListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
publicListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewv= convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
Itemp= items.get(position);
if (p != null) {
ImageViewICon= ( ImageView) v.findViewById(R.id.description);
}
}
return v;
}
In your Activity
ListViewyourListView= (ListView) findViewById(R.id.itemListView);
// get data from the table by the ListAdapterListAdaptercustomAdapter=newListAdapter(this, R.layout.itemlistrow, List<yourItem>);
yourListView .setAdapter(customAdapter);
Post a Comment for "How To Add An Icon To A Listview In A Fragment"