【问题标题】:Custom ArrayList Adaptor - Displaying Icons in the ActionBar List自定义 ArrayList 适配器 - 在 ActionBar 列表中显示图标
【发布时间】: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


    【解决方案1】:

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

    如果您的 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

    视图已正确初始化

    【讨论】:

      【解决方案2】:

      创建一个 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

      【讨论】:

        猜你喜欢
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 2015-04-27
        相关资源
        最近更新 更多