2017-02-16 6 views
0

リストビューアイテムで特定の子ビューをクリックしようとしています。特定のリストビューアイテムをクリックする方法子ビュー

ListViewを商品と考えてください。各アイテムは、子ビューを持つ複合ビューです。

まず特定のリストアイテムを選択し、このアイテムビューを使用して特定のIDを持つ子を見つけてクリックを実行します。

リストアイテムを取得するためのコードですが、このチェーンをさらに進めて目的の目標を達成しようとしましたが、適切な方法が見つかりません。 R.id.button_delete -

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

あなたがidと(0の位置で、この場合は)選択した項目の子ビューをクリックする方法を見つける助けてくださいでした。

私は何か助けに感謝します。

EDIT *

onData(anything()).inAdapterView(withId(R.id.list_view_books)).atPosition(0)は正常に動作し、私は要素を取得することができますが、私は、この要素の子をクリックする方法がわかりません。

これは私の役に立つXMLファイルです。

<?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> 

そして、私のアダプタ

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

どのようにあなたは何アダプタあなたには、いくつかのコードを表示?使用している? 'ListView'を実施しています。 – leobelizquierdo

+0

@leobelizquierdo私の更新を参照してください – gzbuaapzroyn

+0

@gzbuaapzroynあなたは完全なアイテムのXMLを送信するのではなく、全体のアイテムのJavaクラスを共有する必要があります。ありがとう – ymz

答えて

0

この試してみてください。

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

コードを変更せずに可能ですか? – gzbuaapzroyn

関連する問題