问题
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.
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;
}
}
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.
At runtime, 'a1' will be displayed in both the textviews.and so on.
I want to display 'a1' in textView1 and 'a2' is in textView2.. and b1 is .. so on...
I think i may have problem in getItem(), getItemId(), getcount() method.. PLEASE HELP ME...
回答1:
Its clear from your question that you want to display [a1, a2, b1, b2, c1, c2...] as
a1a2
b1b2
c1c2
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;
}
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;
}
回答2:
your adapter getview is perfect, but your logic is wrong.. I would prefer making class object with 2 strings, like.
public class MyClass
{
String one;
String two;
}
and make your list like
ArrayList<MyClass> mylist = new ArrayList<MyClass>();
and then setText like you want.
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"
回答3:
You have wrong implementation in some of the adapter methods.
getItem() should return the object from your list at the position:
@Override
public Object getItem(int position) {
return myList.get(position);
}
Then, in 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]);
}
If your want to display two different strings, I suggest you list should look like this
[new String[]{"a1", "a2"}, new String[]{"b1", "b2"}, ...]
来源:https://stackoverflow.com/questions/9631359/displaying-arraylist-items-in-listview-contains-2-textviews