简体   繁体   中英

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.

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 . And the answer for your solution, try to change Button type TextView and remove focusable attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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