0

I'm having a little problem that I can't solve and I searched the net and I can't find an answer to it. I am making a small android application for my phone and I have a listview item with few items in it. On click on some of the item from my listView, I want to show a little check mark on the left side so a user can see what he selected. Next time the user opens or enters that Activity, there should already be check marker on what he clicked last time.

listview item example

I set the drawableLeft icon like this but now every listView item I have has the same check marker.

public class BazaAdapter extends ArrayAdapter<bazeKlasa>
{
    private Context mContext;
    int mResource;


public BazaAdapter(@NonNull Context mContext, int resource, @NonNull 
ArrayList<bazeKlasa> objects) {
    super(mContext, resource, objects);
    this.mContext = mContext;
    this.mResource = resource;
}


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

    String imeBaze = getItem(position).getBaza();
    String nazivBaze = getItem(position).getImeBaze();

    bazeKlasa Baza = new bazeKlasa(imeBaze, nazivBaze);


    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource,parent,false);

    TextView TextGornji = (TextView) 
convertView.findViewById(R.id.lblcountryname);
    TextGornji.setText(nazivBaze);

    return convertView;
}
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Snailtamer
  • 21
  • 9

2 Answers2

0

On getView() method, you need to set drawable programmatically based on your check-in which item has the tick mark and which have not.

Hope this will help.

    TextView TextGornji = (TextView) convertView.findViewById(R.id.lblcountryname);

    // Provide your condition here instead of isShow
    if (isShow) {
        Drawable img = mContext.getResources().getDrawable(R.drawable.tick); //Provide tick mark drawable
        img.setBounds(0, 0, 60, 60);
        TextGornji.setCompoundDrawables(img, null, null, null);
    } else {
        TextGornji.setCompoundDrawables(null, null, null, null);
    }
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

Variable define

  private int mSelectedItem = -1;

and getView method to add this code

 if(mSelectedItem==position)
        {
            holder.textview.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_right, 0,
                    0, 0);
        }
        else
        {
            holder.textview.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_blank, 0,0, 0);
        }

and click event to add this code

  mSelectedItem = getAdapterPosition();
  notifyDataSetChanged();
Monali
  • 173
  • 1
  • 6
  • Tnx, i will try it now. Can you just tell me what do you mean by "holder"? i hope this will help :) – Snailtamer Jun 21 '18 at 11:15
  • holder is a recyclerView adapter method object like ( public void onBindViewHolder(@NonNull final SizeViewHolder holder, int position) ) – Monali Jun 21 '18 at 11:19
  • so far so good, TextGornji.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_right, 0, 0, 0); but for some reason "getAdapterPosition()" is red to me and i dont know what to do. Any reason why this is happening? Should i change something? – Snailtamer Jun 21 '18 at 12:02
  • you are replace by adapter.notifyDataSetChanged(); – Monali Jun 21 '18 at 12:05
  • and follow this link useful for you.....https://stackoverflow.com/questions/12596199/android-how-to-set-onclick-event-for-button-in-list-item-of-listview – Monali Jun 21 '18 at 12:05