Search code examples
androidlistviewadaptercustom-view

How to make a line in the listView always have the same color android


I have CustomListView:

public class CustomView extends View {

    private String[] hoursList = getResources().getStringArray(R.array.hours); ;
    private String[] minutesList = getResources().getStringArray(R.array.minutes);;
    private ListView listView;
    private HourAdapter hourAdapter;
    private MinuteAdapter minuteAdapter;
 
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, ViewGroup viewGroup) {
        super(context);
        inflate(context, R.layout.time_picker_test, viewGroup);

        listView = viewGroup.findViewById(R.id.minutes_list);
        minuteAdapter = new MinuteAdapter(context, minutesList);
        listView.setAdapter(minuteAdapter);

        listView = viewGroup.findViewById(R.id.hours_list);
        hourAdapter = new HourAdapter(context, hoursList);
        listView.setAdapter(hourAdapter);

    }
}

and adapters classes:

public class MinuteAdapter extends BaseAdapter {
    private LayoutInflater lInflater;
    private String[] minutesValueList;

    public MinuteAdapter(Context context, String[] minutesValueList) {
        lInflater = LayoutInflater.from(context);
        this.minutesValueList = minutesValueList;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.row, parent, false);

         TextView textMinutes = view.findViewById(R.id.textRow);
         textMinutes.setText(minutesValueList[position]);

        }

        if (view.getVisibility() == View.VISIBLE) {
            if (position ==2) {
                view.setBackgroundColor(Color.BLUE);
            } else {
                view.setBackgroundColor(Color.WHITE);
            }
        }
        TextView textMinutes = view.findViewById(R.id.textRow);
        textMinutes.setText(minutesValueList[position]);
        return view;
    }
}

I need, that here (see picture) in the middle position (where now 11,2 and PM) always was BLUE color (no matter what element is currently in this position). I used this:

if (view.getVisibility() == View.VISIBLE) {
                if (position ==2) {
                    view.setBackgroundColor(Color.BLUE);
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
            }

but it is changes element color. But I need that only cell (middle of visible elements) was BLUE always.


Solution

  • If you want middle cell of middle row to be blue change if (position ==2) to if (position ==1) , index in array start from zero not from 1