0

我有一个带有自定义 ArrayAdapter 的列表,它在列表项旁边加载图标,除了每个其他列表项都显示一个图标

public class MyAdapter extends ArrayAdapter<String>{
    Context mContext;

    public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
        super(context, resource, textViewResourceId, strings);
        this.mContext = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.filetypeslist, parent, false);
        String[] items = mContext.getResources().getStringArray(R.array.filetypes);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.tvtype);

        tv.setText(items[position]);

        if(items[position].equals("txt")){
            iv.setImageResource(R.drawable.txticon);//not showing up
        }else if(items[position].equals("HTML")){
            iv.setImageResource(R.drawable.htmlicon);
        }else if(items[position].equals("JS")){
            iv.setImageResource(R.drawable.javascripticon);//not showing up
        }else if(items[position].equals("CSS")){
            iv.setImageResource(R.drawable.cssicon);
        }

        return row;
    }

}

public class NewFile extends ListActivity {
    EditTextLineNumbers newet;
    LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_file);
        setListAdapter(new MyAdapter(this, 
                android.R.layout.simple_list_item_1, R.id.tvtype, 
                getResources().getStringArray(R.array.filetypes)));
    }


}

***EDIT***
public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.filetypeslist, parent, false);
        }
        String[] items = mContext.getResources().getStringArray(R.array.filetypes);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.tvtype);

        tv.setText(items[position]);

        if(items[position].equals("txt")){
            iv.setImageResource(R.drawable.txticon);
        }else if(items[position].equals("HTML")){
            iv.setImageResource(R.drawable.htmlicon);
        }else if(items[position].equals("JS")){
            iv.setImageResource(R.drawable.javascripticon);
        }else if(items[position].equals("PHP")){
            iv.setImageResource(R.drawable.phpicon);
        }else if(items[position].equals("CSS")){
            iv.setImageResource(R.drawable.cssicon);
        }

        return row;
    }

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <string-array name="filetypes">
        <item name="txt">Text</item>
        <item name="html">HTML</item>
        <item name="js">Javascript Class</item>
        <item name="css">CSS</item>
        <item name="php">PHP Class</item>
    </string-array>
</resources>
4

2 回答 2

2

您的比较链String.equals没有检查未显示的两个值的正确值。您正在检查“JS”而不是“Javascript 类”和“txt”而不是“文本”。

除此之外,我有几点建议:

  1. 使用 ArrayAdapter,您可以通过调用来获取当前行的字符串getItem(position)
  2. 在您的资源中创建一个int-array并让每个项目指向与您的项目相对应的可绘制对象string-array
  3. 让您的适配器存储这些数组的本地副本,而不是每次getView调用时都从资源中构建它们。相同的LayoutInflater(在适配器的构造函数中构建这些)。然后你可以得到drawable idintArray[position]而不是一个String.equals()比较链。
  4. 正如 Shoshi 指出的那样,尽可能使用 convertView。
于 2013-02-19T04:41:20.683 回答
0

你应该这样使用:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View row = convertView;
if (view == null) {
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.filetypeslist, parent, false);
}

...............
...............
}
于 2013-02-19T03:34:32.537 回答