PiotrP
PiotrP

Reputation: 225

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.

Upvotes: 0

Views: 145

Answers (2)

Nitin
Nitin

Reputation: 1270

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.

Upvotes: 2

jantursky
jantursky

Reputation: 1130

First of all, you are sending empty listOfRestaurants. And the answer for your solution, try to change Button type TextView and remove focusable attribute.

Upvotes: 0

Related Questions