Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使ListView和Listener工作?_Android_Xml_Listview - Fatal编程技术网

Android 如何使ListView和Listener工作?

Android 如何使ListView和Listener工作?,android,xml,listview,Android,Xml,Listview,我一直在寻找使我的listview工作的方法。我使用了几乎所有我知道的和我在这里看到的东西。我使用了代码和XML标记,但仍然不起作用。我一直在改变在xml中放置BlockDecentant、可聚焦、可点击标记的位置 这是我的相关代码: lstMeals = (ListView)findViewById(R.id.lstMeals); lstMeals.setItemsCanFocus(true); lstMeals.setChoiceMode(ListView.CHOICE_MODE_SING

我一直在寻找使我的listview工作的方法。我使用了几乎所有我知道的和我在这里看到的东西。我使用了代码和XML标记,但仍然不起作用。我一直在改变在xml中放置BlockDecentant、可聚焦、可点击标记的位置

这是我的相关代码:

lstMeals = (ListView)findViewById(R.id.lstMeals);

lstMeals.setItemsCanFocus(true);
lstMeals.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
这是包含listview的xml:

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ProgressBar
            android:id="@+id/pgMeals"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/txtMNoRecord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:layout_centerInParent="true" />

        <ListView
            android:id="@+id/lstMeals"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"/>

    </RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

这是适配器的布局:

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants">

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

        <RelativeLayout
            android:id="@+id/mealInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true">

                <ImageView
                    android:id="@+id/imgMeal"
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="8dp"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>

                <RelativeLayout
                    android:id="@+id/picLayout"
                    android:layout_width="200dp"
                    android:layout_height="70dp"
                    android:layout_alignBottom="@+id/imgMeal"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true">

                    <TextView
                        android:id="@+id/txtWatermark"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentStart="true"
                        android:layout_centerVertical="true"
                        android:gravity="center"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:text="Large Text"
                        android:textColor="#ffffff"
                        android:textSize="25sp"
                        android:textStyle="bold"
                        android:textIsSelectable="false"/>

                    <TextView
                        android:id="@+id/txtWatermark2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="false"
                        android:layout_centerVertical="true"
                        android:gravity="center_vertical|center_horizontal"
                        android:text="Large Text"
                        android:textSize="30sp"
                        android:textStyle="bold"
                        android:visibility="gone"
                        android:textIsSelectable="false"/>
                </RelativeLayout>
            </RelativeLayout>

            <TextView
                android:id="@+id/adptrMealName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="8dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black80"
                android:textSize="24sp"
                android:textStyle="bold"
                android:textIsSelectable="false"/>

            <TextView
                android:id="@+id/txtMealDesc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtMealPrice"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black70"
                android:textSize="24sp"
                android:textIsSelectable="false"/>

            <Button
                android:id="@+id/btnViewIngredients"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtMealDesc"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="15dp"
                android:text="View Ingredients"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

            <TextView
                android:id="@+id/txtMealPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/adptrMealName"
                android:layout_marginBottom="8dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black70"
                android:textSize="24sp"
                android:textIsSelectable="false"/>
        </RelativeLayout>

    </RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

这可能有效

    clubList = (ListView) dialog.findViewById(R.id.club_listview_dialog);
    clubList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //postion is the position of item clicked in the list
        }
    });
clubList=(ListView)dialog.findViewById(R.id.club\u ListView\u dialog);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//position是列表中单击的项目的位置
}
});

将其添加到onCreate中

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

                   Toast.makeText(getApplicationContext(), "Position->" + position + "is selected.", Toast.LENGTH_SHORT).show();

            }
        });
lstFinds.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
Toast.makeText(getApplicationContext(),选择了“位置->”+Position+”,Toast.LENGTH_SHORT).show();
}
});

希望这有帮助。

非常感谢您的帮助。我已经找到了答案。问题是由底部板材引起的。适配器没有使用它,因为该活动是显示底部表单的活动。此代码是我朋友的代码,这就是为什么我不知道有关它的一些事情。

你好,AwaisMajeed,谢谢你的帮助。我尝试了此代码,但仍然无效。谢谢tahsinRupam,但它只允许adapterview。onitemclicklistener@RickyManalo,
ListView
扩展了
AdapterView
,因此,每个
ListView
都是一个
AdapterView
。这个答案解决了您的问题吗?如果它有帮助,您可以将其标记为已接受,其他人将来也可以从中得到帮助?listview无法正常工作。