Search code examples
androidandroid-listviewlistadapter

Custom listview and adaptor overriding getView, changing text colour


There are quite a few answers on stack overflow to questions like this, but I cannot find one that I can translate into my situation.

Effectively I have a listview that overrides the getView method, the listview itself is displaying a string, what I am trying to achieve is to be able to update the colour of this text based at a later time, not when it is first created.

What I currently have is the position at which the item is at, from here I am basically stuck, I am unsure of my next step.

 ListView lv = (ListView) rootView.findViewById(R.id.lvBarcodes);
            Adapter adaptor = lv.getAdapter();
            int Position = getPosition(CurrentBarcode);
            lBarcodes l = (lBarcodes) lv.getItemAtPosition(Position);

l basically contains 2 methods, set and get so I believe this step is uneeded, what I would basically like is a setTextColour(position) method!

I have attached my code below:

Setting up the listview:

ListView lv = (ListView) rootView.findViewById(R.id.lvBarcodes);
            LA_Barcodes adaptor = new LA_Barcodes(getActivity(),barcodeList);
            lv.setAdapter(adaptor);

method:

public class LA_Barcodes extends BaseAdapter{

     ViewHolder holder = null;

    private static LayoutInflater inflater = null;
    private final ArrayList Lines;

    public LA_Barcodes(Context _ctx, ArrayList _lines)
    {
        this.Lines = _lines;
        Context ctx = _ctx;
        inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (Lines.size() <= 0)
            return 1;
        return Lines.size();
    }

    @Override
    public Object getItem(int position) {
        return Lines.get(position);
    }

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

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

        View vi = convertView;


        if(convertView == null)
        {
            vi = inflater.inflate(R.layout.lv_barcodes,null);
            holder = new ViewHolder();
            holder.Barcodes = (TextView) vi.findViewById(R.id.barcodes);
            vi.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) vi.getTag();
        }


        if(Lines.size() == 0)
        {
            holder.Barcodes.setText("No Outstanding Deliveries!");
        }
        else
        {
            lBarcodes temp;
            temp = (lBarcodes) Lines.get(position);
            holder.Barcodes.setText(temp.getBarcode());
        }

        return vi;

    }



    public static class ViewHolder
    {
        public TextView Barcodes;
    }
}

Solution

  • you can define another empty arrayList in your adapter that keep position of editText that should change color.

    ArrayList<Integer> pos = new ArrayList<Integer>();
    

    and method that add position in array:

    public void addPos(int position){
       pos.add(position);
       notifyDataSetChanged();
    }  
    

    then in your adapter ( in getView() method) check if position exist in this array change textColor:

    if(pos.contain((Object) position)){
         //change text color
    }  
    

    you can call adapter.addPos(CurrentBarcode); to change textColor