自定义ArrayAdapter <new_class>获取错误,您必须为textview提供资源ID </new_class>

时间:2011-04-17 05:58:23

标签: android android-arrayadapter

这里是代码获取错误arrayadapter - 您必须为textview提供资源ID

public class lay extends Activity
{
public class MyCustomAdapter extends ArrayAdapter<new_class> 
{

    private ArrayList<new_class> items;

     public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<new_class> items) 
     {
         super(context, textViewResourceId, items);
         this.items = items;
     }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {  
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        new_class o = items.get(position);
        if (o != null) {
                TextView tt = (TextView) v.findViewById(R.id.text1);
                TextView bt = (TextView) v.findViewById(R.id.text2);
                if (tt != null) {
                      tt.setText("Name: "+o.getName());                            }
                if(bt != null){
                      bt.setText("Status: "+ o.getLink());
                }
        }
        return v;
    }
}


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myclass try1 = new myclass();
    final List<Map<String, ?>> aaa = try1.type();

    //------------------------------------------------------
    ArrayList<new_class> my_orders = null;
    my_orders = new ArrayList<new_class>();
    new_class o1 = new new_class();
    o1.setName("perspectiva");
    o1.setLink("link1");
    new_class o2 = new new_class();
    o2.setName("perspectiva22");
    o2.setLink("link222");
    my_orders.add(o1);
    my_orders.add(o2);

    ArrayAdapter<new_class> adapter = new ArrayAdapter<new_class>(this, R.layout.row, my_orders);

    ListView lv  = (ListView)this.findViewById(R.id.ListView01);
    lv.setAdapter(adapter);
    //------------------------------------------------------


} 

和布局行

<pre> 

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/text2"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout>
</pre>

1 个答案:

答案 0 :(得分:4)

使用以下代码。您使用了错误的适配器作为自定义列表视图。

public class lay extends Activity
{

    public class MyCustomAdapter extends BaseAdapter 
    {

        private ArrayList<new_class> items;
        Context mcontext;
        public MyCustomAdapter(Context context,ArrayList<new_class> items){
            this.items = items;
            mcontext=context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {  
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            new_class o = items.get(position);
            if (o != null) {
                TextView tt = (TextView) v.findViewById(R.id.text1);
                TextView bt = (TextView) v.findViewById(R.id.text2);
                if (tt != null) {
                    tt.setText("Name: "+o.getName());                            }
                if(bt != null){
                    bt.setText("Status: "+ o.getLink());
                }
            }
            return v;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        //------------------------------------------------------
        ArrayList<new_class> my_orders = null;
        my_orders = new ArrayList<new_class>();
        new_class o1 = new new_class();
        o1.setName("perspectiva");
        o1.setLink("link1");
        new_class o2 = new new_class();
        o2.setName("perspectiva22");
        o2.setLink("link222");
        my_orders.add(o1);
        my_orders.add(o2);

        //ArrayAdapter<new_class> adapter = new ArrayAdapter<new_class>(this, R.layout.row, my_orders);
        MyCustomAdapter adapter = new MyCustomAdapter(this, my_orders);
        ListView lv  = (ListView)this.findViewById(R.id.listView1);
        lv.setAdapter(adapter);
        //------------------------------------------------------
    }

}