2011-04-17 49 views
2

這裏的錯誤是代碼得到一個錯誤arrayadapter - 你必須提供一個資源ID爲一個TextView定製ArrayAdapter <new_class>讓你必須提供一個資源ID爲一個TextView

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> 

回答

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

} 
+0

謝謝。但你能解釋我爲什麼應該使用baseadapter而不是arrayadapter?或在哪裏閱讀更多信息? – SERG 2011-04-17 15:33:20

+0

陣列適配器本身擴展了基礎適配器,因爲基礎適配器是適配器的基礎類。 – 2011-04-29 00:11:48

相關問題