自定义的ArrayList适配器 - 在在ActionBar列表中显示的图标 [英] Custom ArrayList Adaptor - Displaying Icons in the the ActionBar List

查看:178
本文介绍了自定义的ArrayList适配器 - 在在ActionBar列表中显示的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的图标添加到我的动作条(第7版)。我创建了一个自定义的ArrayAdapter

我想旁边的列表项显示相应的图标。但是,我越来越对种选择正确的图标,而不是在列表本身

 公共类ObjectsArrayAdapter扩展ArrayAdapter<串GT; {
私人最终上下文的背景下;
私人最终的String []值;
公共ObjectsArrayAdapter(上下文的背景下,的String []值){
    超(背景下,R.layout.objects_type_list,R.id.label,价值观);    this.context =背景;
    this.values​​ =值;}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LayoutInflater吹气=(LayoutInflater)上下文
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    查看rowView = inflater.inflate(R.layout.objects_type_list,父母,假);
    TextView中的TextView =(TextView中)rowView.findViewById(R.id.label);
    ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.logo);
    textView.setText(值[位置]);    //更改图标基于名称
    字符串s =值[位置]    如果(s.equals(墙)){
        imageView.setImageResource(R.drawable.ic_action_email);
        Log.d(IMG,发现墙);
    }否则如果(s.equals(门)){
        imageView.setImageResource(R.drawable.ic_drawer);
        Log.d(IMG,找到门);
    }否则如果(s.equals(楼梯)){
        Log.d(IMG,发现楼梯);
        imageView.setImageResource(R.drawable.ic_action_email);
    }其他{
        Log.d(IMG,默认);
        imageView.setImageResource(R.drawable.ic_action_email);
    }    返回rowView;
}

和列表布局如下:

 <?XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:填充=5DP>< ImageView的
    机器人:ID =@ + ID /标志
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@绘制/ ic_launcher>
< / ImageView的><的TextView
    机器人:ID =@ + /标记
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@ + /标记
 >
< / TextView的>< / LinearLayout中>

如何设置实际的列表项包含正确的图标?

更新:

必须是与事实getView不叫的OnCreate?

  actionBar.setListNavigationCallbacks(
            新ObjectsArrayAdapter(actionBar.getThemedContext()
                                                                        Ipsum.Types),这一点);


解决方案

我发现了一个解决方案,如果任何人有类似的问题。

如果您ArrayAdapter工具公开查看getDropDownView(INT位置,查看convertView,一个ViewGroup父),而不是公共查看getView(INT位置,查看convertView ,父母的ViewGroup - 见SpinnerAdapter 的http://开发商。 android.com/reference/android/widget/SpinnerAdapter.html

该视图正确初始化

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

解决方案

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

这篇关于自定义的ArrayList适配器 - 在在ActionBar列表中显示的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆