2017-08-13 1 views
0

donc j'ai déclaré un bouton pour chaque élément dans le ListView mais il arrive qu'il répond à plus d'éléments puis seulement le cliqué. Par exemple, si j'appuie sur le bouton du premier bouton, les 9ème et 18ème objets sont changés aussi. Ce serait génial si vous pouviez me aider avec celui-ci :) merci à l'avanceOnClickListener sur le bouton dans ListView change plusieurs éléments

This is the CusomtListAdapter which fills the ListView 

    public class CustomListAdapter extends BaseAdapter { 

    private ArrayList<Task> tasks; 
    private LayoutInflater inflater = null; 
    private Application application; 

    public CustomListAdapter(Activity activity, Application application, 
          ArrayList<Task> tasks) { 
     this.application = application; 
     this.tasks = tasks; 
     inflater = (LayoutInflater) application.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public static class ViewHolder { 
     TextView lv_tv_description; 
     TextView lv_tv_period; 
     Button lv_bt_done; 
     TextView lv_gone_pk; 
     TextView lv_gone_group_pk; 
     TextView lv_gone_description; 
     TextView lv_gone_period; 
     TextView lv_gone_period_kind; 
     TextView lv_gone_time; 
     TextView lv_gone_done; 
    } 

    @Override 
    public int getCount() { 
     return tasks.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

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

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

     final ViewHolder viewHolder; 

     if (vi == null) { 
      vi = inflater.inflate(R.layout.listview, parent, false); 
      viewHolder = new ViewHolder(); 

      viewHolder.lv_tv_description = (TextView) vi.findViewById(R.id.lv_tv_description); 
      viewHolder.lv_tv_period = (TextView) vi.findViewById(R.id.lv_tv_period); 
      viewHolder.lv_bt_done = (Button) vi.findViewById(R.id.lv_bt_done); 

      viewHolder.lv_gone_pk = (TextView) vi.findViewById(R.id.lv_gone_pk); 
      viewHolder.lv_gone_group_pk = (TextView) vi.findViewById(R.id.lv_gone_group_pk); 
      viewHolder.lv_gone_description = (TextView) vi.findViewById(R.id.lv_gone_description); 
      viewHolder.lv_gone_period = (TextView) vi.findViewById(R.id.lv_gone_period); 
      viewHolder.lv_gone_period_kind = (TextView) vi.findViewById(R.id.lv_gone_period_kind); 
      viewHolder.lv_gone_time = (TextView) vi.findViewById(R.id.lv_gone_time); 
      viewHolder.lv_gone_done = (TextView) vi.findViewById(R.id.lv_gone_done); 


      vi.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) vi.getTag(); 
     } 

     viewHolder.lv_tv_description.setText(tasks.get(position).getDescription()); 
     viewHolder.lv_tv_period.setText(tasks.get(position).getPeriod_kind()); 

     viewHolder.lv_bt_done.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       DatabaseHandler dbhandler = new DatabaseHandler(application); 


       View pk_view = (View) v.getParent().getParent().getParent(); 
       TextView textview_pk = ((TextView) pk_view.findViewById(R.id.lv_gone_pk)); 

       View parentView = (View) v.getParent().getParent(); 
       TextView textview1 = ((TextView) parentView.findViewById(R.id.lv_tv_description)); 
       TextView textview2 = ((TextView) parentView.findViewById(R.id.lv_tv_period)); 
       Button button = ((Button) parentView.findViewById(R.id.lv_bt_done)); 

       if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("done")) { 
        viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
        viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
        viewHolder.lv_bt_done.setText("UNDO"); 
        viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent); 

        notifyDataSetChanged(); 

        dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "true"); 
       } else if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("undo")) { 
        viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
        viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
        viewHolder.lv_bt_done.setText("DONE"); 
        viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background); 

        notifyDataSetChanged(); 

        dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "false"); 
       } 

       dbhandler.close(); 
      } 
     }); 

     viewHolder.lv_gone_pk.setText(String.valueOf(tasks.get(position).getPk())); 

     if (tasks.get(position).getDone().equalsIgnoreCase("true")) { 
      viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
      viewHolder.lv_tv_period.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
      viewHolder.lv_bt_done.setText("UNDO"); 
      viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent); 
     } 

     return vi; 
    }} 

ce qui est le fichier de mise en page correspondant pour tous les articles

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:focusable="false" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="70dp"> 

    <LinearLayout 
     android:focusable="false" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:focusable="false" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <RelativeLayout 
       android:focusable="false" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <TextView 
        android:focusable="false" 
        android:id="@+id/lv_tv_description" 
        android:textSize="20dp" 
        android:textColor="@android:color/black" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:layout_marginTop="7dp"/> 

      </RelativeLayout> 

      <RelativeLayout 
       android:focusable="false" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2"> 

       <TextView 
        android:focusable="false" 
        android:id="@+id/lv_tv_period" 
        android:textColor="@android:color/black" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"/> 

      </RelativeLayout> 

     </LinearLayout> 

     <RelativeLayout 
      android:focusable="false" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="4"> 

      <Button 
       android:id="@+id/lv_bt_done" 
       android:text="DONE" 
       android:focusable="false" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_alignParentTop="true" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/layout_rounded_background"/> 

     </RelativeLayout> 

    </LinearLayout> 

    <!-- invisible TextViews --> 
    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_pk" 
     android:visibility="visible" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_group_pk" 
     android:visibility="visible" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_description" 
     android:visibility="gone" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_period" 
     android:visibility="gone" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_period_kind" 
     android:visibility="gone" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_time" 
     android:visibility="gone" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:focusable="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_gone_done" 
     android:visibility="gone" 
     android:layout_alignParentRight="true"/> 

</RelativeLayout> 

si vous avez besoin d'autre s'il vous plaît laissez-moi savoir dans les commentaires :)

Répondre

1

le View obtient réutilisé dans la ListView via le second paramètre (convertView) de la méthode getView().

De la documentation,

convertView - L'ancienne vue de la réutilisation, si possible. Remarque: Vous devez vérifier que cette vue est non nulle et de type approprié avant l'utilisation de . S'il n'est pas possible de convertir cette vue pour afficher les données correctes , cette méthode peut créer une nouvelle vue.

Ainsi, à tout moment, seul le nombre de vues (plus quelques autres) visibles sur l'écran de votre appareil est disponible en mémoire. Lorsque vous faites défiler la liste, les vues qui sortent de l'écran sont réutilisées pour les éléments qui arrivent à l'écran.

Dans votre cas, le 1er, le 9ème et le 18ème article sont les mêmes View étant réutilisés. C'est pourquoi vous obtenez le View changé pour les articles 9 et 18 lorsque vous appuyez sur le bouton seulement sur le 1er article.

Solution:

Lorsque le bouton est pressé, ajouter que les informations d'état dans une structure de données. Par exemple, dans un array de booleans, enregistrez true pour l'élément dont le bouton est enfoncé et false pour les autres boutons.

Et dans la méthode getView(), vérifiez l'état du bouton à partir de ce tableau de booléens et modifiez la vue respectivement.

+0

J'ai essayé cela mais je n'ai pas travaillé ... peut-être que je ne vous ai pas gerne droit pourriez-vous me donner un exemple de code? :) – user3455536

+0

cela m'a beaucoup aidé aussi https://stackoverflow.com/questions/35538353/onclick-event-repeats-with-listview-items – user3455536