2

I am updating a custom adapter and had to change the value from a String Array to a List.

Here is code:

private final List<String> values;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.comments_listadapter, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.comments_label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.comments_menu);
    textView.setText(values[position]); // Needs to Change!

    String s = values[position]; // Needs to Change!

    imageView.setImageResource(R.drawable.up_down);

    return rowView;
}

I have marked the two lines that need to change (I am not sure if changing it to a List will need more to change?

5 Answers 5

2
List<String> mStringList = Arrays.asList(values);

where values is a string array. Arrays class is in java.util package.

1

If I understand your question correctly, you're looking for:

String s = values.get(position);
textView.setText(s);
1
  • If the answer worked out for you, don't forget to mark it as correct; it helps others find the answer in the future! Commented Jun 27, 2012 at 6:22
0

Try this

String s = getItem(position);
textView.setText(s); 
0

So it sounds like you have something like String[] values which you want to be List<String> values.

Here is how you can do the conversion:

List<String> listValues = new ArrayList<String>();

for(String s : values) {
    listValues.add(s);
}

Then instead of values[position], you do listValues.get(position).

1
  • 1
    shortcut for you have: List<String> listValues = Arrays.asList(values); :) Commented Jun 27, 2012 at 2:34
0

What exactly is your problem? Do you mean you have to change your references to

mList.get(position);
mList.add(position, string);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.