Ive used this so far:
setListAdapter(new ArrayAdapter<String>(this,R.layout.list, R.id.label, names));
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="match_parent">
<ImageView android:id="@+id/icon"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_width="22px"
android:layout_marginTop="15px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_开发者_StackOverflow中文版content"
android:layout_height="wrap_content"
android:id="@+id/label" android:textSize="35px"></TextView>
</LinearLayout>
That works however my question is how i make every item unique(i have a lot of image in R.drawable)
So on the item dog i want a dog picture and the cat item is going to be a cat. Now is every item the default app icon.
Thank you!
Your current implementation should be binding the text data from your names data object to the textview. The reason why the image view is not displaying the correct image is because there is no way for the adapter to communicate with the image view. The array adapter's underlying implementation will inflate your list.xml view and from that list.xml pull out the view which R.id.label represents. It will check if that view is either a textview or an imageview. If it's neither it will throw an exception. It will then bind the data from the names data object to the textview.
What you need is to specify a custom adapter and also ensure that your data model holds the string for the name and a reference to the image resource - in your case it will be an integer.
Here's a sample Name object that might work for you:
public class Name
{
private String _name;
private int _imageResourceId;
public void Name(String name, int resId)
{
_name = name;
_imageResourceId = resId;
}
public void setImageResId(int resId)
{
_imageResrouceId = resId;
}
public void setName(String name)
{
_name = name;
}
public int getImageResId()
{
return _imageResourceId;
}
public String getName()
{
return _name;
}
}
Begin by creating a class and extending BaseAdapter and the implement the getView, getPosition, getCount, etc.
Here's a sample Adapter that might work well for you:
public class NameAdapter extends BaseAdapter
{
private Name[] _data;
private LayoutInflater _inflater;
public NameAdapter(Context context, Name[] data)
{
_data = data;
_inflater = LayoutInflater.from(context);
}
public int getCount()
{
if(_data != null)
{
return _data.length;
}
else
{
return 0;
}
}
public Object getItem(int position)
{
if(_data != null)
{
return _data[position];
}
else
{
return null;
}
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView == null)
{
convertView = _inflater.inflate(R.layout.list, parent);
holder = new ViewHolder();
holder._image = (ImageView) convertView.findViewById(R.id.icon);
holder._label = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(holder);
}
if(holder == null)
{
holder = (ViewHolder) convertView.getTag();
}
holder._image.setImageResource(_data[position].getImageResourceId());
holder._label.setText(_data[position].getName());
}
static class ViewHolder
{
TextView _label;
ImageView _image;
}
}
*Note: I have not tested this out so you may have to tweak this a bit.
Also, as an optimization I would suggest changing your list.xml to:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label"
android:textSize="16sp" />
With the TextView you can use "setCompoundDrawable" or "setCompoundDrawablesWithIntrinsicBounds" and really optimize your layout. The compound drawable allows you to place an image to the left or right or above or below the text view. I suggest you have have a look at the api documentation for further implementation details.
Hope that helps!
精彩评论