在ListView中显示ArrayList中的项目包含2 textviews [英] Displaying ArrayList items in ListView contains 2 textviews
问题描述
我要显示在ListView中ArrayList的项目由具有2个不同的textViews。
我使用ListViewCustomAdapter和getView(),getItem()时...的方法有哪些。
I want to display the arrayList items in ListView which is having 2 different textViews. I am using ListViewCustomAdapter and getView(),getItem()... methods are there.
这是我的code:
MyCustom.java:
This is my code: 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;
}
}
以上code,有如下所述的问题。
这将是在ListView中既TextView显示该ArrayList项相同。
例如:
MYLIST = ['A1','A2','B1','B2']
这是我的ArrayList和传递给MyCustomAdapter。
Above code, there is problem as mentioned below. This will be display the arrayList items as same in both the textviews in ListView. Ex: mylist=['a1','a2','b1','b2'] this is my arraylist and passing to MyCustomAdapter.
在运行时,'A1'将被同时显示在textviews.and等等。
At runtime, 'a1' will be displayed in both the textviews.and so on.
我要显示textView1A1和A2是textView2 ..和B1是...等等...
I want to display 'a1' in textView1 and 'a2' is in textView2.. and b1 is .. so on...
我想我可能在具有的getItem问题(),getItemId(),getCount将()方法..
请帮我...
I think i may have problem in getItem(), getItemId(), getcount() method.. PLEASE HELP ME...
推荐答案
它从你的问题清楚[A1,A2,B1,B2,C1,C2 ...]
为
Its clear from your question that you want to display [a1, a2, b1, b2, c1, c2...] as
a1a2
b1b2
c1c2
所以你需要改变你的code到以下几点:
so you need to change your code to following:
@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方法如下:
and getView method as below:
@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;
}
这篇关于在ListView中显示ArrayList中的项目包含2 textviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!