0

OK this is driving me nuts. I was hoping to learn how to do this by today. Here is what I have. This is a Multi choice ListView obviously. You click the the Selection it selects, you unclick the selection it de selects. It ALMOST works perfectly. There is only one problem. It only works when 1 item is already selected. If an item is selected, Items can be selected and de selected NP. But if nothing is selected, the last one stays selected. How to I fix that?

 public void onItemClick(AdapterView<?> adapterView, View view, int Position, long l) {
    int cntChoice = listview.getCount();
    SparseBooleanArray sparseBooleanArray = listview.getCheckedItemPositions();
    for (int i = 0; i < cntChoice; i++) {
        if(sparseBooleanArray.get(i)) {
            switch (Position) {
                case 0:
                    if (listview.isItemChecked(0)) {
                        A = "";
                        A = "T";
                    } else if (!listview.isItemChecked(0)) {A = ""; A = "F";}
                    break;
                case 1:
                    if (listview.isItemChecked(1)) {
                        B = "";
                        B = "T";
                    } else (listview.isItemChecked(1)) {B = ""; B = "F";}
                    break;
                case 2:
                    if (listview.isItemChecked(2)) {
                        C = "";
                        C = "T";
                    } else (listview.isItemChecked(2)) {C = ""; C = "F";}
                    break;
            }

            Toast.makeText(Billing.this, "data" + A + ", " + B + ", " + C, Toast.LENGTH_LONG).show();

        } 
gtcode
  • 47
  • 2
  • 7
  • well if you want to learn, then go for RecyclerView! here's a complete guide for it - https://enoent.fr/blog/2015/01/18/recyclerview-basics/ – Darshan Sep 02 '18 at 03:48
  • ok that was a bit much of a read. I used it and another post and I have a question. – gtcode Sep 03 '18 at 05:06
  • I used it and another post. by Shashanth https://stackoverflow.com/questions/36369913/how-to-implement-multi-select-in-recyclerview I figured out how to get my string-array loaded. and I kind of understand what is going on. I have a view that appears to work, but I am a lost for implementing the function I had above, link variable & toast. I can see I am getting the position in public void onBindViewHolder, I can build a switch there from model.isSelected() i believe but how to toast it as the class has no layout? – gtcode Sep 03 '18 at 05:15
  • in the BindViewHolder, set a click listener to holder.itemView & then show a toast – Darshan Sep 03 '18 at 06:36
  • that worked great. thanks very much. I have to send my variables to the activity now so I'm gonna learn how to do an interface I guess, that's what I read, still kind of new to this. I appreciate your help! I would up vote you if I could on here. – gtcode Sep 03 '18 at 08:15
  • You don't need an Interface explicitly, you can handle the click events in Adapter's BindViewHolder... but if you need to pass the holder's position to the Activity then you will need to use the Interface, if you need that, tell me & I'll show you :) – Darshan Sep 03 '18 at 08:17
  • well what I did is linked a variable to each position. and then I want to send the variable over so I can then use retrofit to send it to the sql. So I got a list of hobbies. and that makes it easier to track user hobbies. But I want to send all variables over not just the ones clicked. Their True or False value changes on the click. I think I can intent it over. I was getting an error before but I am trying Intent sendhobby = new Intent(view.getContext(), hobby.class) and that isn't giving me an error. I have yet to test it yet though. – gtcode Sep 03 '18 at 09:09
  • well if you want to send all the selected variables, you should follow the above tutorial, it has methods like adapter.getCheckedItemPositions(), that will give you the list of current checked items – Darshan Sep 03 '18 at 09:15
  • yeah i seen that, but I want unchecked too. that's why i linked the variable. You can set the variables using a switch(holder.getAdapterPosition(){case 0: if(model.isSelected()}{swim = ""; swim = "T";}else {swim=""; swim="F";} That way you have data for all variables checked or not. Then I think i can intent them over to the hobby activity, and then retrofit. Thank you so much for your help, it was very useful – gtcode Sep 03 '18 at 09:21
  • you're welcome :) – Darshan Sep 03 '18 at 09:23

0 Answers0