0

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.

3

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

what about the second string array? I mean the methods getCount and getItem are not being used for str2 right?
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
Thank u but I've found a great tutorial which is similar to yours. Worked for me.
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
@Dinesh can you give a link to the tutorial which you found, please?
0

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

thanks, but I can hardly find any similarity between what I'm trying to do and the your answer in the above thread.
The original poster has two string arrays as well. So I turned it into one object and manage it that way.
I want both of them to be different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.