自定义ArrayList适配器 - 在ActionBar列表中显示图标

时间:2014-01-16 12:07:56

标签: android listview icons android-arrayadapter

我正在尝试向ActionBar添加图标(v7)。我创建了一个自定义ArrayAdapter

我想在列表项旁边显示相应的图标。但是,我在选择时获得了正确的图标,而不是列表本身

Menu non-functioning

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);

2 个答案:

答案 0 :(得分:2)

我找到了一个解决方案,万一有人遇到类似的问题。

如果您的ArrayAdapter实现public View getDropDownView(int position, View convertView, ViewGroup parent)而不是public View getView(int position, View convertView, ViewGroup parent - 请参阅SpinnerAdapter http://developer.android.com/reference/android/widget/SpinnerAdapter.html

视图已正确初始化

答案 1 :(得分:0)

创建一个array.xml文件并添加图像,如下所示:

<string-array name="random_imgs">
    <item>@drawable/car_01</item>
    <item>@drawable/balloon_random_02</item>
    <item>@drawable/dog_03</item>
</string-array>

然后在您的活动中添加此代码:

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