-2

I am new to android and java. I would like to make the text of the 3rd rows (not every third rows) in the ListView red color.

ArrayList<String> weekInfoList = new ArrayList<>();
weekInfoList.add("first row");
weekInfoList.add("second row");
weekInfoList.add("third row");

ArrayAdapter arrayAdapter1 = new ArrayAdapter(MainActivity.this, R.layout.list_item2, weekInfoList);
weeklyListView.setAdapter(arrayAdapter1);

weeklyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {

     // intent to next activity
     }
});

Please help me.

CHAN HAU YEEN
  • 343
  • 2
  • 5
  • 18

4 Answers4

1

You can create your own custom ArrayAdapter instead of using the default, then override the getView method to set the color depending on the row position.

public class CustomAdapter extends ArrayAdapter {
    public CustomAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // check position
        // if every 3rd row, set color

        // return the modified convertView
    }
}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
1

You can use BaseAdapter or ArrayAdapter as @ginomempin suggested. and override getView method of it.

Now as you mentioned for every 3rd row you want to change text color, you can do following.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inf = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final Holder vh;
    if (itemLayoutView == null) {
        vh = new Holder();
        itemLayoutView = inf.inflate(R.layout.custom_layout,
                    null);

        vh.textview = (TextView) itemLayoutView
                    .findViewById(R.id.textview);
        temLayoutView.setTag(vh);
    } else {
        vh = (Holder) itemLayoutView.getTag();
    }

    if(position%3==0){
        holder.textview.setTextColor(Color.parseColor("#ff0000"));
    }else{
        holder.textview.setTextColor(Color.parseColor("#666666"));
    }

    return convertView;
}

For more details on how to use custom Adapter refer This and This

Happy Coding.

Community
  • 1
  • 1
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

In your adapter class, you might have more than one item to display, so if you are using android.support.v4 than it will be like,

holder.thirdTextview.setTextColor(ContextCompat.getColor(activity, R.color.red));
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

As I am very new to programming, I tried to avoid custom adapter but seems like we must use it.

The alternate answers are very brief. So here I attach my solution for more detail reference.

ArrayAdapter customAdapter = new MySimpleArrayAdapter(getApplicationContext(),weekInfoList);
weeklyListView.setAdapter(customAdapter);

public class MySimpleArrayAdapter extends ArrayAdapter {
    private final Context context;
    private final ArrayList<String> values;

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, -1, values);
        this.context = context;
        this.values = 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.list_item2, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.weeknumber2);
        textView.setText(values.get(position));

        if (position==2){
            textView.setTextColor(Color.RED);
        }

        return rowView;
    }
}

Thanks to Suraj's solution https://stackoverflow.com/a/13109854/2466516.

Community
  • 1
  • 1
CHAN HAU YEEN
  • 343
  • 2
  • 5
  • 18