[英]Clicking button in listview doesn't work
知道已经报告了类似的案例并且对此主题有很多答案,很不幸,在我的案例中任何事情都有效。 这是我的活动代码:
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">
不幸的是,单击按钮没有任何效果。 我尝试设置可聚焦等参数,但没有任何帮助。
我认为通过使用类的context
,您可以在活动之间进行切换。
尝试类似的方法:
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);
}
});
我希望这会有所帮助。
首先,您要发送空的listOfRestaurants 。 解决方案的答案是,尝试更改Button类型TextView并删除focusable属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.