【发布时间】:2014-01-16 12:07:55
【问题描述】:
我正在尝试将图标添加到我的 ActionBar (v7)。我创建了一个自定义 ArrayAdapter
我想在列表项旁边显示适当的图标。但是,我在选择时得到了正确的图标,而不是在列表本身中
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;
}
而列表布局如下:
<?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>
如何设置实际列表项以包含正确的图标?
更新:
一定与 getView 不被称为 OnCreate 的事实有关?
actionBar.setListNavigationCallbacks(
new ObjectsArrayAdapter(actionBar.getThemedContext(),
Ipsum.Types), this);
【问题讨论】:
标签: android listview icons android-arrayadapter