Java 单击listview中的按钮不会';行不通
知道有类似的案例报告,并且有很多关于这个话题的答案,不幸的是,我的案例中任何东西都是有效的。 以下是我的活动代码:Java 单击listview中的按钮不会';行不通,java,android,Java,Android,知道有类似的案例报告,并且有很多关于这个话题的答案,不幸的是,我的案例中任何东西都是有效的。 以下是我的活动代码: listViewOfReastaurants = findViewById(R.id.listViewOfSensors); listViewOfReastaurants.setDividerHeight(10); listOfRestaurants = new ArrayList<>(); adapter = new A
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);
}
});
ListViewOfRestaurants=findViewById(R.id.listViewOfSensors);
Restaurants的列表视图。setDividerHeight(10);
listofRestarants=newArrayList();
adapter=new ArrayAdapter(getApplicationContext(),R.layout.list_按钮,Restaurants列表);
ListViewofRestaurants.setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),
Toast.LENGTH_LONG).show();
Intent myIntent=新的Intent(getApplicationContext(),
每日一班);
星触觉(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">
不幸的是,点击按钮没有任何效果。我尝试过设置参数,如可聚焦等,但没有任何帮助。首先,您发送的是空的驻留列表。
对于您的解决方案,请尝试更改按钮键入文本视图并删除可聚焦属性。首先,您发送的是空的驻留列表。
对于您的解决方案,请尝试更改按钮键入文本视图并删除可聚焦属性。我认为通过使用该类的
上下文
,您可以在活动之间切换
试试这样的方法:
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);
}
});
ListViewOfRestaurants.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),
Toast.LENGTH_LONG).show();
Intent myIntent=新的Intent(getApplicationContext(),
每日一班);
Context=getBaseContext();
背景。起始触觉(myIntent);
}
});
我希望这篇文章会有帮助。我认为通过使用课程的
上下文
,你可以在活动之间切换
试试这样的方法:
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);
}
});
ListViewOfRestaurants.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),
Toast.LENGTH_LONG).show();
Intent myIntent=新的Intent(getApplicationContext(),
每日一班);
Context=getBaseContext();
背景。起始触觉(myIntent);
}
});
我希望这一个会有帮助。看起来:ListofRestarants是一个空数组,您是如何更新它的值的?它不会工作,这是正常的行为。您正在列表视图的每一行项目上设置click listener。因此,侦听器仅适用于行视图。但是你点击了一个按钮视图。虽然在这里,按钮似乎是每行中唯一的项目。但是在这里,按钮的点击监听器覆盖了自定义的点击监听器。希望你能理解。:)@Cao,如果列表是空的,那么他可能在列表视图中看不到任何项目(此处按钮),也无法单击这些按钮。看起来:ListofRestarants是一个空数组,您是如何更新其值的?它将不起作用,这是正常行为。您正在列表视图的每一行项目上设置click listener。因此,侦听器仅适用于行视图。但是你点击了一个按钮视图。虽然在这里,按钮似乎是每行中唯一的项目。但是在这里,按钮的点击监听器覆盖了自定义的点击监听器。希望你能理解。:)@Cao,如果列表是空的,那么他可能在列表视图中看不到任何项目(此处按钮),也无法单击这些按钮。