2012-03-09 63 views
0

我想在ListView中顯示具有2個不同textView的arrayList項目。 我正在使用ListViewCustomAdapter和getView(),getItem()...方法在那裏。在ListView中顯示ArrayList項目包含2個文字瀏覽

這是我的代碼: MyCustom.java:

public class MyCustom extends BaseAdapter { 
    public Activity context; 
    public LayoutInflater inflater; 
    ArrayList mylist; 

    public MyCustom(Activity context,ArrayList viewList) { 
     super(); 
     this.context=context; 
     this.mylist=viewList; 

     inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 

    } 

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

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

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


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View List; 
    if(convertView==null) 
    { 
     List=new View(context); 
     LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     List=mLayoutinflater.inflate(R.layout.listitem_row, parent, false); 
    } 
    else 
    { 
     List=(View)convertView; 

    } 

    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle); 
    t1.setText((CharSequence) mylist.get(position)); 

    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription); 
    t2.setText(mylist.get(position).toString()); 


    return List; 
    } 
} 

上面的代碼,如下所述存在問題。 這將在ListView中的文本視圖中顯示arrayList項目。這是我的數組列表,並傳遞給MyCustomAdapter。

在運行時,'a1'將顯示在textviews.and等。

我想顯示textView1 'A1' 和 'A2' 是textView2 ..和B1是..等等...

我想我可以在getItem()時,getItemId有問題() ,getCount將()方法.. 請幫我...

回答

0

適配器getview是完美的,但你的邏輯是錯的.. 我寧願做類對象,2串, 喜歡。

public class MyClass 
{ 
    String one; 
    String two; 
} 

,讓你列表像

ArrayList<MyClass> mylist = new ArrayList<MyClass>(); 

,然後像的setText你想。

TextView t1=(TextView)List.findViewById(R.id.txtViewTitle); 
t1.setText(mylist.get(position).one);   //String one= "a1" according to position in mylist 
               //it will be = "b1" on next position 
               //no need of casting to CharSequence 

TextView t2=(TextView)List.findViewById(R.id.txtViewDescription); 
t2.setText(mylist.get(position).two);   //String two= "a2" 
0

你有一些適配器方法錯誤執行。

的getItem()應在的位置從您的列表返回對象:

@Override 
public Object getItem(int position) { 
    return myList.get(position); 
} 

然後,在getView

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Create view 
    // ... 

    String[] item = (String[])getItem(position); // Get the current object at 'position' 


    // Update your view here 
    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle); 
    t1.setText(item[0]); 

    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription); 
    t2.setText(item[1]); 
} 

如果你想顯示兩個不同的字符串,我建議你清單應像這樣

[new String[]{"a1", "a2"}, new String[]{"b1", "b2"}, ...] 
+0

不工作...我想顯示'a1'到textview1和以下'a2'到textview2 ... – EESHA 2012-03-09 09:47:27

+0

已更新。所以考慮列表中的對象結構。爲了更好地理解,列表中的每個對象都代表一行。因此,列表中的每個對象應包含有關該行的所有數據。 – Hanon 2012-03-09 09:57:00

2

其從要顯示〔A1,A2你的問題清楚, B1,B2,C1,C2 ...] 作爲

a1a2 
b1b2 
c1c2 

所以你需要更改您的代碼如下:

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    if(myList.size()%2==0) 
     return mylist.size()/2; 
    else 
     return myList.size()/2+1; 
} 

和getView方法如下:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View List; 
    if(convertView==null) 
    { 
     List=new View(context); 
     LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     List=mLayoutinflater.inflate(R.layout.listitem_row, parent, false); 
    } 
    else 
    { 
     List=(View)convertView; 

    } 

    TextView t1=(TextView)List.findViewById(R.id.txtViewTitle); 
    t1.setText((CharSequence) mylist.get(position*2)); 


    TextView t2=(TextView)List.findViewById(R.id.txtViewDescription); 
    if(position*2<getCount()) 
      t2.setText(mylist.get(position*2+1).toString()); 


    return List; 
} 
+0

嗨..它不是那樣..我想作爲a1 a2,b1b2,c1c2 .. – EESHA 2012-03-09 09:32:09

+0

是nt這種方法是做同樣的 – jeet 2012-03-09 10:07:30

+0

非常感謝...它的工作... – EESHA 2012-03-09 11:44:04