2017-02-16 2 views
0

J'essaie de cliquer sur une vue enfant spécifique dans un élément de vue liste.Comment cliquer sur une vue de liste spécifique de la vue enfant

Envisager ListView avec les articles. Chaque élément est une vue composée avec des vues enfant.

Je veux tout d'abord sélectionner un élément de liste spécifique et que d'utiliser cet élément afficher trouver un enfant avec un ID spécifique et effectuer un clic.

Voici mon code pour obtenir l'élément de liste, mais j'ai essayé de poursuivre cette chaîne plus loin pour atteindre l'objectif désiré, mais je ne peux pas trouver de méthodes appropriées.

onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0).??

Pourriez-vous s'il vous plaît aider à trouver un moyen de cliquer sur la vue enfant de l'élément sélectionné (dans ce cas à la position 0) avec id - R.id.button_delete.

Je serais reconnaissant pour toute aide.

EDIT *

onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0) fonctionne très bien et je peux obtenir un élément, mais je ne sais pas comment cliquer sur un enfant de cet élément.

Voici mon fichier XML pour l'article s'il aide.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:bootstrap="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/relative_layout_root" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:background="@drawable/rounded_corner" 
    android:padding="3dp"> 

    <RelativeLayout 
     android:id="@+id/relative_layout_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="2dp"> 


     <ImageView 
      android:id="@+id/image_view_color" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_centerVertical="true" 
      android:layout_margin="10dp" 
      android:contentDescription="@null" 
      android:padding="15dp" 
      android:src="@drawable/ic_add_large" /> 


     <EditText 
      android:id="@+id/edit_text_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_marginTop="15dp" 
      android:layout_toEndOf="@+id/image_view_color" 
      android:layout_toLeftOf="@+id/button_accept" 
      android:layout_toRightOf="@+id/image_view_color" 
      android:layout_toStartOf="@+id/button_accept" 
      android:ellipsize="none" 
      android:textColor="@color/white" 
      android:textSize="22sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tvDuration" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/edit_text_name" 
      android:layout_alignStart="@+id/edit_text_name" 
      android:layout_below="@+id/edit_text_name" 
      android:layout_marginTop="10dp" 
      android:paddingBottom="10dp" 
      android:textColor="@color/white" 
      android:textSize="18sp" 
      tools:text="Date info" /> 

     <com.beardedhen.androidbootstrap.BootstrapButton 
      android:id="@+id/button_edit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/button_remove" 
      android:layout_toStartOf="@+id/button_remove" 
      android:layout_centerVertical="true" 
      android:layout_margin="5dp" 
      bootstrap:bb_icon_left="fa-pencil" 
      bootstrap:bb_roundedCorners="true" 
      bootstrap:bb_type="warning" /> 

     <com.beardedhen.androidbootstrap.BootstrapButton 
      android:id="@+id/button_accept" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/button_remove" 
      android:layout_toStartOf="@+id/button_remove" 
      android:layout_centerVertical="true" 
      android:layout_margin="5dp" 
      bootstrap:bb_icon_left="fa-check" 
      bootstrap:bb_roundedCorners="true" 
      bootstrap:bb_type="success" /> 

     <com.beardedhen.androidbootstrap.BootstrapButton 
      android:id="@+id/button_remove" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_margin="5dp" 
      bootstrap:bb_icon_left="fa-trash-o" 
      bootstrap:bb_roundedCorners="true" 
      bootstrap:bb_type="danger" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/linear_layout_filter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/relative_layout_container" 
     android:layout_marginBottom="15dp" 
     android:layout_marginEnd="15dp" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginStart="15dp" 
     android:orientation="horizontal" 
     android:visibility="visible" 
     android:weightSum="2"> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <TextView 
       android:id="@+id/text_view_from" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_margin="5dp" 
       android:gravity="center" 
       android:text="@string/duration_start" 
       android:textColor="@color/white" 
       android:textSize="25sp" 
       android:textStyle="bold" /> 

      <com.beardedhen.androidbootstrap.BootstrapButton 
       android:id="@+id/button_briefing_date" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text_view_from" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       bootstrap:bb_icon_left="fa-calendar" 
       bootstrap:bb_roundedCorners="true" 
       bootstrap:bb_type="primary" 
       tools:text="test" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <TextView 
       android:id="@+id/text_view_to_date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_margin="5dp" 
       android:gravity="center" 
       android:text="@string/duration_end" 
       android:textColor="@color/white" 
       android:textSize="25sp" 
       android:textStyle="bold" /> 

      <com.beardedhen.androidbootstrap.BootstrapButton 
       android:id="@+id/button_briefing_to" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text_view_to_date" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       bootstrap:bb_icon_left="fa-calendar" 
       bootstrap:bb_roundedCorners="true" 
       bootstrap:bb_type="primary" 
       tools:text="test" /> 

     </RelativeLayout> 

    </LinearLayout> 

</RelativeLayout> 

Et mon adaptateur

public class CustomAdapter extends ArrayAdapter<Book > implements View.OnClickListener{ 

    private ArrayList<Book> dataSet; 
    Context mContext; 

    // View lookup cache 
    private static class ViewHolder { 
     Button deleteButton; 
    // Other fields 
    } 

    public CustomAdapter(ArrayList<Book > data, Context context) { 
     super(context, R.layout.list_view_item_book, data); 
     this.dataSet = data; 
     this.mContext=context; 

    } 

    @Override 
    public void onClick(View v) { 

     switch (v.getId()) 
     { 
      case R.id.button_delete: 
       // Delete 
       break; 
      ............. 
     } 
    } 


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

     Book dataModel = getItem(position); 

     ViewHolder viewHolder; // view lookup cache stored in tag 

     final View result; 

     if (convertView == null) { 

      viewHolder = new ViewHolder(); 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.list_view_item_book, parent, false); 
      // Find views 

      result=convertView; 

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

     viewHolder.deleteButton.setOnClickListener(this); 
     return convertView; 
    } 
} 
+0

comment vous implémentez la 'ListView' ?, quel adaptateur utilisez-vous ?, montrer un peu de code. – leobelizquierdo

+0

@leobelizquierdo s'il vous plaît voir ma mise à jour – gzbuaapzroyn

+0

@gzbuaapzroyn VOUS DEVEZ PARTAGER L'ENSEMBLE ENTIER JAVA CLASS ... au lieu de nous envoyer le xml de l'article complet. merci – ymz

Répondre

0

essayez ceci:

listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       View target = view.findViewById(R.id.button_delete); 
       target.performClick(); 
      } 
     }); 
+0

Est-ce possible sans changer le code? – gzbuaapzroyn