2

i have written code for highlighted item from listview which i am click but it is working only for 1 item at a time. I want to highlighted multiple items .

public class myadapter extends ArrayAdapter<ItemList> {
    private Context context;
    private ArrayList<ItemList> values;

    private int selectedIndex;
    private int selectedColor = Color.parseColor("#1b1b1b");

    public BrandselectedAdapter(Context context, ArrayList<ItemList> values) {
        super(context, R.layout.row_item, values);

        this.context = context;
        this.values = values;
        selectedIndex = -1;

    }

    public void setSelectedIndex(int ind) {
        selectedIndex = ind;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, final View convertView,
                        ViewGroup parent) {
        final ViewHolder holder;
        View v = convertView;

        if (v == null) {
            final LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row_item, null);
            holder = new ViewHolder();
            holder.txt = (TextView) v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        ItemList mi = values.get(position);

        holder.txt.setText(mi.getName().toString());

        if (selectedIndex != -1 && position == selectedIndex) {
            holder.txt.setTextColor(Color.YELLOW);
        } else {
            holder.txt.setTextColor(Color.RED);
        }

        return v;
    }

    static class ViewHolder {
        public TextView txt;

    }
}

Listview click event:

itemlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        int wantedPosition = position; 
        int firstPosition = listbrand.getFirstVisiblePosition() - listbrand.getHeaderViewsCount(); // This is the same as child #0
        int wantedChild = wantedPosition - firstPosition;
        if (wantedChild < 0 || wantedChild >= listbrand.getChildCount()) {

            Toast.makeText(mContext, "Sorry", Toast.LENGTH_SHORT).show();
            return;
        }

        View wantedView = listbrand.getChildAt(wantedChild);

        myadapter.setSelectedIndex(position);


    }
});

From above code i can change the color of selected item from listview but it works only for 1 item.

Suppose i have item

a
b
c
d
e

When i click on a then the color of a will be change to RED as per my code.Now when i select the b then the color of b will be change to RED AND color of a is also change to YELLOW as per my code.

So i want like when i click on any item then it colors turn into RED and when i again click on the same item then and then only item colors turn into YELLOW.

0 Answers0