据了解,类似的案例已被报道,并且该主题有很多答案,但不幸的是,任何事情都适用于我的案例。 这是我的 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);
}
});
这是我的布局文件:
<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" />
和行模板:
<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">
不幸的是点击按钮没有任何效果。我尝试设置可聚焦等参数,但没有任何帮助。
最佳答案
我认为通过使用类的上下文
,您可以在 Activity 之间切换。
尝试类似:
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);
}
});
我希望这篇文章会对您有所帮助。
关于java - 单击 ListView 中的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202204/