问题
I am trying to add icons to my ActionBar (v7). I created a custom ArrayAdapter
I want to display the appropriate icon next to the list item. However, I am getting the correct icon upon slection, not in the list itself
public class ObjectsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ObjectsArrayAdapter(Context context, String[] values) {
super(context, R.layout.objects_type_list, R.id.label, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.objects_type_list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("Wall")) {
imageView.setImageResource(R.drawable.ic_action_email);
Log.d("IMG", "Found Wall");
} else if (s.equals("Door")) {
imageView.setImageResource(R.drawable.ic_drawer);
Log.d("IMG", "Found Door");
} else if (s.equals("Stairs")) {
Log.d("IMG", "Found Stairs");
imageView.setImageResource(R.drawable.ic_action_email);
} else {
Log.d("IMG", "Default");
imageView.setImageResource(R.drawable.ic_action_email);
}
return rowView;
}
And the list Layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
>
</TextView>
</LinearLayout>
How do I set the actual list items to contain the correct icons ?
Update:
Must be something to do with the fact that getView is not called OnCreate ?
actionBar.setListNavigationCallbacks(
new ObjectsArrayAdapter(actionBar.getThemedContext(),
Ipsum.Types), this);
回答1:
I found a solution, in case anyone has a similar issue.
If your ArrayAdapter implements public View getDropDownView(int position, View convertView, ViewGroup parent)
rather than public View getView(int position, View convertView, ViewGroup parent
- see SpinnerAdapter http://developer.android.com/reference/android/widget/SpinnerAdapter.html
The view is initialized correctly
回答2:
Create an array.xml file and add your images, just like this:
<string-array name="random_imgs">
<item>@drawable/car_01</item>
<item>@drawable/balloon_random_02</item>
<item>@drawable/dog_03</item>
</string-array>
Then add this code in your activity:
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
//get resourceid by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));
</resources>
Source
来源:https://stackoverflow.com/questions/21161369/custom-arraylist-adaptor-displaying-icons-in-the-actionbar-list