0

我有一个列表视图。在我的列表视图中,我在每一行上创建一个自定义项目。这个自定义项目作为一些文本视图和两个按钮。我现在遇到的问题是我想检测用户何时单击列表视图项中的按钮。但我的代码不起作用。

请不要介意对用户隐藏的众多文本视图,只有一个用户可以看到并且两个按钮是可见的。

每次单击按钮时都没有任何反应,没有响应我的 listview 自定义项粘贴在下面。

感谢您阅读我的帖子,也感谢您的时间和精力。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/listitemdesign"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_marginBottom="10dp">

    <TextView
        android:id="@+id/itemprescrip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:visibility="gone"
         />

    <TextView
        android:id="@+id/agerange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/itemanufac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/itemexdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/itemdcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/itemnafno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:visibility="gone"
        android:textAppearance="?android:attr/textAppearanceMedium" />

     <TextView
        android:id="@+id/drugID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:visibility="gone"
        />

       <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

         <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/plus" 
        android:layout_marginTop="8dp"
        />


          <TextView
        android:id="@+id/itemname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:layout_margin="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

       </LinearLayout>

       <View android:layout_width="wrap_content" android:layout_height="2dip"
        android:background="@color/wine" 
        android:layout_marginTop="5dp"
        />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

            <ImageButton
        android:id="@+id/ordernow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/whitebackground"
        android:src="@drawable/ordernow" 

        android:layout_marginTop="15dp"
        />

    <ImageButton
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/detailsicon"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/whitebackground"
         />
    </LinearLayout>



</LinearLayout>

我的活动代码也粘贴在下面。ListView 组件的 OnItemClickListener 如下:

private AdapterView.OnItemClickListener itemclickListener = new AdapterView.OnItemClickListener(){
                String drugID = null,drugName = null, drugPrecription = null, drugManufacturedate = null, drugExpiratorydate = null,drugCode = null, nafdacNo=null,drugageRange = null;

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,long id) {


                    drugID = ((TextView)view.findViewById(R.id.drugID)).getText().toString();
                    drugName = ((TextView)view.findViewById(R.id.itemname)).getText().toString();
                     drugPrecription =((TextView)view.findViewById(R.id.itemprescrip)).getText().toString();
                    drugManufacturedate = ((TextView)view.findViewById(R.id.itemanufac)).getText().toString();
                    drugExpiratorydate = ((TextView)view.findViewById(R.id.itemexdate)).getText().toString();
                     drugCode = ((TextView)view.findViewById(R.id.itemdcode)).getText().toString();
                     nafdacNo = ((TextView)view.findViewById(R.id.textViewName)).getText().toString();
                    drugageRange = ((TextView)view.findViewById(R.id.agerange)).getText().toString();

                    orderButton = (ImageButton)parent.getChildAt(9);//view.findViewById(R.id.ordernow);
                    detailsButton = (ImageButton)parent.getChildAt(0);//view.findViewById(R.id.details);

                    Object o = orderButton.getClass();
                    Toast.makeText(context, o.toString(), Toast.LENGTH_SHORT).show();

                    //Now we are going check for the button that would clicked
                    //int elementID = view.getId();

                    //this.detectItemClicked(elementID);
}
4

0 回答 0