0

MainActivity的扩展ListActivityonCreate我遵循代码的方法

setListAdapter(new MyAdapter(this,
    android.R.layout.simple_list_item_1,
    R.id.textView1,
    getResources().getStringArray(R.array.countries)));

而这个类在我的 MainActivity 范围内

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

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

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflator.inflate(R.layout.list_item, parent, true);

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

        String[] items = getResources().getStringArray(R.array.countries);

        tv.setText(items[position]);
        iv.setImageResource(R.drawable.flag_1);


        return super.getView(position, convertView, parent);
    }

}

现在,当我运行此代码时,应用程序因警报错误而崩溃

很遗憾,APPLICATION_NAME 已停止

我认为这条线有问题View row = inflator.inflate(R.layout.list_item, parent, true);

PS:我对Android很陌生,不知道为什么会这样,LogCat太难读了,但这里有几行:

10-19 13:06:09.878: D/AndroidRuntime(1736): Shutting down VM
10-19 13:06:09.878: W/dalvikvm(1736): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-19 13:06:09.896: E/AndroidRuntime(1736): FATAL EXCEPTION: main
10-19 13:06:09.896: E/AndroidRuntime(1736): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.AdapterView.addView(AdapterView.java:477)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at com.me.listexample.MainActivity$MyAdapter.getView(MainActivity.java:57)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.ListView.onMeasure(ListView.java:1159)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.view.View.measure(View.java:15848)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
10-19 13:06:09.896: E/AndroidRuntime(1736):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
4

3 回答 3

1

试试这样,

public class MyAdapter extends ArrayAdapter<String> {
    private Context context;

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            this.context=context;
        }

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

            LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflator.inflate(R.layout.list_item,null);

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

            String[] items = (Activity)context.getResources().getStringArray(R.array.countries);

            tv.setText(items[position]);
            iv.setImageResource(R.drawable.flag_1);


            return row;
        }

    }
于 2013-10-19T17:21:57.970 回答
0

试试这个

View row = inflator.inflate(R.layout.list_item, null, true);

代替

View row = inflator.inflate(R.layout.list_item, parent, true);
于 2013-10-19T17:26:55.360 回答
0

在你的构造函数中

 LayoutInflater inflator;
 String items[];
 public MyAdapter(Context context, int resource, int textViewResourceId,
        String[] strings) {
    super(context, resource, textViewResourceId, strings);
    inflator = LayoutInflate.from(context);
    this.items = strings;
}

getView

View view = inflator.inflate(R.layout.list_item, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textView1);
ImageView iv = (ImageView)row.findViewById(R.id.imageView1);
tv.setText(items[position]);
iv.setImageResource(R.drawable.flag_1);
return view;  

消除

String[] items = getResources().getStringArray(R.array.countries);

您已经将数组传递给适配器类的构造函数

此外,最好使用ViewHolder模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

编辑:

static class ViewHolder
{
TextView tv;
ImageView iv;
} 

在 getView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = inflator.inflate(R.layout.list_item, null);
        holder.iv= (ImageView) convertView.findViewById(R.id.imageView1);
        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
        holder.tv.setText(items[position]);
        holder.iv.setImageResource(R.drawable.flag_1);
    return convertView;
}
于 2013-10-19T17:17:22.383 回答