3
String[] values = new String[] { "Apple", "Banana", "Cola", "Dove", "Elephant", "Fan", "Grapes", "Horse" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>((this, R.layout.row,R.id.textview1, values);
list.setAdapter(adapter);

(the class which contains the above code extends ListActivity) I want to give different background colors to different row(For example: The background color of the textview1 should be green at position 0, red at position 6, blue at position 7) For this it is said that I need to use custom adapter, but I don't know how to set these particular values Apple,Banana,Cola,...etc to the textview1 using a custom adapter. My xml file row.xml is as shown below

<TextView
    android:layout_width="500dp"
    android:layout_height="50dp"
    android:text="New Text"
    android:layout_alignParentTop="true"
    android:id="@+id/ListItem"
    android:background="@color/color3"
    android:gravity="center|left"
    android:paddingLeft="5dp"
    android:textSize="20dp"
    android:textColor="@color/color1" />

I don't know how to implement a customadapter. Can anyone help me to set these values to the TextView by using a custom adapter and set different background colors to the TextView?

1

1 Answer 1

0

First give your TextView in row.xml an id (Let's say rowText)

You need to create an Adapter which will bind itself to your ListView. The adapter can in general look like this:

public class CustomListAdapter extends BaseAdapter {
    String[] values = new String[] { "Apple", "Banana", "Cola", "Dove", "Elephant", "Fan", "Grapes", "Horse" };

public ThumbnailAdapter(MainActivity m) {
    thumbnails = thumbs;
    mInflater = LayoutInflater.from(m);
}

@Override
public int getCount() {
    return values.length;
}

@Override
public Object getItem(int position) {
    return values[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row, null);
        holder = new ViewHolder();
        holder.rowText = (TextView) convertView
                .findViewById(R.id.rowText);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
            //Set the text here!
            holder.rowText.setText(values[position]);
            //Set the color here!
            holder.rowText.setBackgroundResource(R.color.green);

    return convertView;
}

static class ViewHolder {
    TextView rowText;
}
}

This is just a start for you. To change the colours you can set an array of colours and select from the position as well. Hope it helps! (I have not tested the code but it should be mostly correct)

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

Comments

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.