0

I have a recyclerView with 5 cardView. there are three Button in each cardview. One of this button invisible and when GPS data changed can be visible in only one cardview. my problem is I can't do this.

I try some way. one away is:

    public void onLocationChanged(Location currentLocation) {
        double latitude = currentLocation.getLatitude();
        double longitude = currentLocation.getLongitude();

        float[] results = new float[1];

        for (int i = 1; i < list_count; i++) {
            Location.distanceBetween(oldLat[i], oldLng[i],
                    latitude, longitude, results);
            if (results[0] < 30) {
                visible[i] = true;

            } else {
                visible[i] = false;
            }
         adapter.notifyItemChanged(i);
         //  adapter.notifyDataSetChanged();
        }
                }

and another way is:

      public void onBindViewHolder(@NonNull final CustomAdapter.MyViewHolder holder, final int listPosition) {

        final Button buttonPresent = holder.buttonPresent;
        final Button buttonAbsent = holder.buttonAbsent;
        final Button buttonGeo = holder.buttonGeo;

        if (listPosition > -1) {
            if (visible[listPosition]) {
                buttonGeo.setVisibility(View.GONE);
            } else {
                buttonGeo.setVisibility(View.INVISIBLE);
            }
        }

        adapter.notifyItemChanged(i);
         //  adapter.notifyDataSetChanged();
        }
}

Default state for all buttonGeo in recyclerView is INVISIBLE. when one location reached,the button related to it must be VISIBLE for only one cardView. But I can't change button visibility and all button invisible. How do I solve this problem? Excuse me for my English.

mehdi
  • 340
  • 4
  • 17

2 Answers2

1

you have several errors in your code, maybe a bad copy, but not only.

First of all, suppress the line adapter.notifyItemChanged(i) or adapter.notifyDataSetChanged() in onBindViewHolder, that has no sense.

Also, I guess that buttonGeo. buttonGeo.setVisibility(1) if bad copy, since like that, the code can't compile.

Your loop forgot the first element of the list or array. Write for (int i = 0; i < student_count; i++) instead of for (int i = 1; i < student_count; i++)

Otherwise I don't see anything wrong, just check "results" and visible[i] are well `

Turvy
  • 882
  • 1
  • 8
  • 23
  • Tanks, You are right, because I wrote previous code. The first element of the array is empty and the code work correctly. My question is how change button property including visibility and background and etc in recyclerView. – mehdi Jul 24 '19 at 02:12
  • I really don't know why it's does not work. Ckeck with breakpoint that onBindViewHolder is call again when a location is intercepted. Maybe you should put the condition in th CustomAdapter.MyViewHolder – Turvy Jul 24 '19 at 07:49
  • yes onBindViewHolder is calling and buttonGeo.setVisibility(View.GONE); runs but not effected! – mehdi Jul 24 '19 at 08:28
0

finally I solve my problem. I use follow code in onBindViewHolder:

   public void onBindViewHolder(@NonNull final CustomAdapter.MyViewHolder holder, final int listPosition) {

    final Button buttonPresent = holder.buttonPresent;
    final Button buttonAbsent = holder.buttonAbsent;
    final Button buttonGeo = holder.buttonGeo;

    if (listPosition > -1) {
        if (visible[listPosition]) {
            buttonGeo.setVisibility(View.VISIBLE);
        } else {
            buttonGeo.setVisibility(View.INVISIBLE);
        }
    }
    }
   }

and buttonGeo.setVisibility(View.GONE); don't work.

mehdi
  • 340
  • 4
  • 17