简体   繁体   English

单击列表视图中的按钮不起作用

[英]Clicking button in listview doesn't work

Having knowledge that similar cases were reported and there are many answers to this topic, unfortunately anything works in my case. 知道已经报告了类似的案例并且对此主题有很多答案,很不幸,在我的案例中任何事情都有效。 Here is my code from activity: 这是我的活动代码:

listViewOfReastaurants = findViewById(R.id.listViewOfSensors);
        listViewOfReastaurants.setDividerHeight(10);
        listOfRestaurants = new ArrayList<>();
        adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_button, listOfRestaurants);
        listViewOfReastaurants.setAdapter(adapter);

        listViewOfReastaurants.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                        Toast.LENGTH_LONG).show();
                Intent myIntent = new Intent(getApplicationContext(),
                        DishOfTheDay.class);
                startActivity(myIntent);
            }
        });

Here is my layout file: 这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">

<ListView
    android:id="@+id/listViewOfSensors"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView"
    android:layout_centerHorizontal="true" />

And row template: 和行模板:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:textSize="15sp"
android:padding="20dp"
android:focusable="false">

Unfortunately clicking button doesnt take any effect. 不幸的是,单击按钮没有任何效果。 I tried with setting parameters like focusable etc. but nothing helped. 我尝试设置可聚焦等参数,但没有任何帮助。

I think by using context of the class you can switch between activities. 我认为通过使用类的context ,您可以在活动之间进行切换。

Try something like: 尝试类似的方法:

  listViewOfReastaurants.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                        Toast.LENGTH_LONG).show();
                Intent myIntent = new Intent(getApplicationContext(),
                        DishOfTheDay.class);
                Context context=getBaseContext();
                context.startActivity(myIntent);
            }
        });

I hope this one will be helpful. 我希望这会有所帮助。

First of all, you are sending empty listOfRestaurants . 首先,您要发送空的listOfRestaurants And the answer for your solution, try to change Button type TextView and remove focusable attribute. 解决方案的答案是,尝试更改Button类型TextView并删除focusable属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM