I have a custom ListView with two TextViews, I have two string arrays in my class how can I add these two string arrays as items for my ListView? Each representing one textview.
Thank you.
-
duplicate of stackoverflow.com/questions/6383330/…Hiren Dabhi– Hiren Dabhi2012-02-14 11:58:20 +00:00Commented Feb 14, 2012 at 11:58
-
@HirenDabhi I've edited my question please check it my question is different.Dinesh– Dinesh2012-02-14 12:04:09 +00:00Commented Feb 14, 2012 at 12:04
-
check this codehenge.net/blog/2011/05/…Hiren Dabhi– Hiren Dabhi2012-02-14 12:13:15 +00:00Commented Feb 14, 2012 at 12:13
Add a comment
|
2 Answers
i think this is useful to you , call simple adapter with two string array
public class simleAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater linflater;
private TextView txt_1, txt_2;
private String[] str1;
private String[] str2;
public simleAdapter(Context context, String[] s1, String[] s2) {
mContext = context;
str1 = s1;
str2 = s2;
linflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return str1.length;
}
@Override
public Object getItem(int arg0) {
return str1[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = linflater.inflate(R.layout.feed_raw, null);
}
txt_1 = (TextView) convertView.findViewById(R.id.txtlist1);
txt_2 = (TextView) convertView.findViewById(R.id.txtlist2);
txt_1.setText(str1[position]);
txt_2.setText(str2[position]);
return convertView;
}
}
let me know if you have any doubt here
5 Comments
Dinesh
what about the second string array? I mean the methods getCount and getItem are not being used for str2 right?
Ramesh Solanki
the length of two array should be same , otherwise you have to used the arraylist of object. make a class with two string member and pass arraylist og those class object to the adapter
Dinesh
Thank u but I've found a great tutorial which is similar to yours. Worked for me.
Ramesh Solanki
ok No problem but there is any relation between two string array then you should use object's arraylist it is much easier and faster
Sabre
@Dinesh can you give a link to the tutorial which you found, please?
Check my answer in this thread Custom adapter: get item number of clicked item in inflated listview. That should answer what you are trying to do.
3 Comments
Dinesh
thanks, but I can hardly find any similarity between what I'm trying to do and the your answer in the above thread.
RobGThai
The original poster has two string arrays as well. So I turned it into one object and manage it that way.
Dinesh
I want both of them to be different.