TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Change String Array to List<String> - Java/Android

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?

Upvotes: 2

Views: 4860

Answers (5)

a.r.
a.r.

Reputation: 575

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

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

Upvotes: 2

Frank Sposaro
Frank Sposaro

Reputation: 8531

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

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

Upvotes: 0

tskuzzy
tskuzzy

Reputation: 36446

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).

Upvotes: 0

Cat
Cat

Reputation: 67502

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

String s = values.get(position);
textView.setText(s);

Upvotes: 1

Try this

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

Upvotes: 0

Related Questions