OnItemClickListener 未触发
OnItemClickListener is not firing
我正在制作一个应用程序,该应用程序在 ListView 中显示了要执行的练习列表。我试图让用户 select 列表中的一个项目开始一个新的 activity,但我的 OnItemClickListener 没有触发。这是我的Activity,(不是列表Activity,它是appCompatActivity):
ArrayList<Exercise> myExercises = new ArrayList<>();
AlertDialog.Builder alertDialogBuilder;
ArrayAdapter<Exercise> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
refreshList();
Button newExButton = (Button) findViewById(R.id.newExButton);
arrayAdapter = new ArrayAdapter<Exercise>(
this,
android.R.layout.simple_list_item_1,
myExercises );
ListView lv = (ListView) findViewById(R.id.actList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("listener heard");
// selected item
int selection = position;
startExercise(selection);
}
});
lv.setAdapter(arrayAdapter);
}
public void refreshList(){
setContentView(R.layout.activity_list);
ListView lv = (ListView) findViewById(R.id.actList);
lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lv.setAdapter(arrayAdapter);
}
public void startExercise(int selection){
Intent exIntent = new Intent(this, CommenceExercise.class);
Exercise chosenEx = myExercises.get(selection);
Bundle info = new Bundle();
info.putLong("duration", chosenEx.getTime());
info.putString("name", chosenEx.getName());
info.putString("description", chosenEx.getDescription());
exIntent.putExtras(info);
startActivity(exIntent);
}
列表最初是空的,但用户通过按下按钮添加了项目。该按钮通过以下代码创建一个 alertDialog:
public void addNewActivity(View view) {
//get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custom, null);
alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.exNameInput);
final EditText durInput = (EditText) promptsView
.findViewById(R.id.exDurInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
long duration = 0;
String exName;
exName = userInput.getText().toString();
duration = Integer.valueOf(durInput.getText().toString());
myExercises.add(new Exercise(exName, duration));
// create new exercise with user input
refreshList();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
这是我的 xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
tools:context="com.example.mytimer.ListActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="146dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="Activities"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp"
android:clickable="false"
android:layout_marginEnd="16dp" />
<Button
android:id="@+id/newExButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="87dp"
android:onClick="addNewActivity"
android:text="New Exercise"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<ListView
android:id="@+id/actList"
android:layout_width="328dp"
android:layout_height="301dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newExButton"
app:layout_constraintVertical_bias="1.0">
<requestFocus/>
</ListView>
<TextView
android:id="@+id/debugText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:clickable="false"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
当我点击已添加到列表中的项目时,没有任何反应。我可以看出 OnItemClickListener 没有被触发,因为从未打印 System.out 行。我不知道为什么我不能让它工作。任何帮助将不胜感激。提前致谢。
我认为问题在 XML 请设置 ListView
可点击 true
请在 XML
中做一些更改
<ListView
android:id="@+id/actList"
android:layout_width="328dp"
android:layout_height="301dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:descendantFocusability="blocksDescendants"
android:focusable="true" // change this
android:clickable="true" // change this
android:focusableInTouchMode="true" // change this
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newExButton"
app:layout_constraintVertical_bias="1.0">
<requestFocus/>
</ListView>
这真的很奇怪...您的代码应该可以正常工作...我建议您尝试在 activity 中实施以包含 lv.setClcickable(true)
和
lv.setEnable(true)
:
public class MenuActivity extends AppCompatActivity implements
AdapterView.OnItemClickListener{
ArrayList<Exercise> myExercises = new ArrayList<>();
AlertDialog.Builder alertDialogBuilder;
ArrayAdapter<Exercise> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
refreshList();
Button newExButton = (Button) findViewById(R.id.newExButton);
arrayAdapter = new ArrayAdapter<Exercise>(
this,
android.R.layout.simple_list_item_1,
myExercises );
ListView lv = (ListView) findViewById(R.id.actList);
lv.setClickable(true);
lv.setEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view , int
position, long id)
{
startExercise(position);
}
lv.setAdapter(arrayAdapter);
}
public void refreshList(){
setContentView(R.layout.activity_list);
ListView lv = (ListView) findViewById(R.id.actList);
lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lv.setAdapter(arrayAdapter);
}
public void startExercise(int selection){
Intent exIntent = new Intent(this, CommenceExercise.class);
Exercise chosenEx = myExercises.get(selection);
Bundle info = new Bundle();
info.putLong("duration", chosenEx.getTime());
info.putString("name", chosenEx.getName());
info.putString("description", chosenEx.getDescription());
exIntent.putExtras(info);
startActivity(exIntent);
}
}
或将这三行添加到您的 xml
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true
android:descendantFocusability="blocksDescendants"
如果有任何可获得焦点的子视图,那么第一次点击将始终获得焦点
我建议您将自定义 ListView
与 Viewholder
Pattern 一起使用以提高效率,或者使用支持库的 Recyclerview
这将使您的滚动平滑并且不会创建额外的视图对象......它只会创建适合屏幕的视图对象
这里是 Link RecyclerView
https://guides.codepath.com/android/using-the-recyclerview
将您的 refreshList
方法更改为以下方法
public void refreshList(){
arrayAdapter.notifyDataSetChanged();
}
您正在做的是再次膨胀 ListView
并为其设置新数据,而不是更新旧数据。当您为 ListView
分配新值时,您忘记设置 onClickListener
.
它会起作用。
您需要从 xml 中删除以下列表视图属性:
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
此外,不需要 refreshList() 这实际上是一种糟糕的做事方式,而是在您的 addNewActivity() 中,一旦您的模型准备就绪(可能在单击肯定按钮时)将该项目添加到 arrayAdapter 和然后做 arrayAdapter.notifyDataSetChanged().
希望对您有所帮助!
我正在制作一个应用程序,该应用程序在 ListView 中显示了要执行的练习列表。我试图让用户 select 列表中的一个项目开始一个新的 activity,但我的 OnItemClickListener 没有触发。这是我的Activity,(不是列表Activity,它是appCompatActivity):
ArrayList<Exercise> myExercises = new ArrayList<>();
AlertDialog.Builder alertDialogBuilder;
ArrayAdapter<Exercise> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
refreshList();
Button newExButton = (Button) findViewById(R.id.newExButton);
arrayAdapter = new ArrayAdapter<Exercise>(
this,
android.R.layout.simple_list_item_1,
myExercises );
ListView lv = (ListView) findViewById(R.id.actList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("listener heard");
// selected item
int selection = position;
startExercise(selection);
}
});
lv.setAdapter(arrayAdapter);
}
public void refreshList(){
setContentView(R.layout.activity_list);
ListView lv = (ListView) findViewById(R.id.actList);
lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lv.setAdapter(arrayAdapter);
}
public void startExercise(int selection){
Intent exIntent = new Intent(this, CommenceExercise.class);
Exercise chosenEx = myExercises.get(selection);
Bundle info = new Bundle();
info.putLong("duration", chosenEx.getTime());
info.putString("name", chosenEx.getName());
info.putString("description", chosenEx.getDescription());
exIntent.putExtras(info);
startActivity(exIntent);
}
列表最初是空的,但用户通过按下按钮添加了项目。该按钮通过以下代码创建一个 alertDialog:
public void addNewActivity(View view) {
//get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custom, null);
alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.exNameInput);
final EditText durInput = (EditText) promptsView
.findViewById(R.id.exDurInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
long duration = 0;
String exName;
exName = userInput.getText().toString();
duration = Integer.valueOf(durInput.getText().toString());
myExercises.add(new Exercise(exName, duration));
// create new exercise with user input
refreshList();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
这是我的 xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
tools:context="com.example.mytimer.ListActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="146dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="Activities"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp"
android:clickable="false"
android:layout_marginEnd="16dp" />
<Button
android:id="@+id/newExButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="87dp"
android:onClick="addNewActivity"
android:text="New Exercise"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<ListView
android:id="@+id/actList"
android:layout_width="328dp"
android:layout_height="301dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newExButton"
app:layout_constraintVertical_bias="1.0">
<requestFocus/>
</ListView>
<TextView
android:id="@+id/debugText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:clickable="false"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
当我点击已添加到列表中的项目时,没有任何反应。我可以看出 OnItemClickListener 没有被触发,因为从未打印 System.out 行。我不知道为什么我不能让它工作。任何帮助将不胜感激。提前致谢。
我认为问题在 XML 请设置 ListView
可点击 true
请在 XML
中做一些更改<ListView
android:id="@+id/actList"
android:layout_width="328dp"
android:layout_height="301dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:descendantFocusability="blocksDescendants"
android:focusable="true" // change this
android:clickable="true" // change this
android:focusableInTouchMode="true" // change this
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newExButton"
app:layout_constraintVertical_bias="1.0">
<requestFocus/>
</ListView>
这真的很奇怪...您的代码应该可以正常工作...我建议您尝试在 activity 中实施以包含 lv.setClcickable(true)
和
lv.setEnable(true)
:
public class MenuActivity extends AppCompatActivity implements
AdapterView.OnItemClickListener{
ArrayList<Exercise> myExercises = new ArrayList<>();
AlertDialog.Builder alertDialogBuilder;
ArrayAdapter<Exercise> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
refreshList();
Button newExButton = (Button) findViewById(R.id.newExButton);
arrayAdapter = new ArrayAdapter<Exercise>(
this,
android.R.layout.simple_list_item_1,
myExercises );
ListView lv = (ListView) findViewById(R.id.actList);
lv.setClickable(true);
lv.setEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view , int
position, long id)
{
startExercise(position);
}
lv.setAdapter(arrayAdapter);
}
public void refreshList(){
setContentView(R.layout.activity_list);
ListView lv = (ListView) findViewById(R.id.actList);
lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lv.setAdapter(arrayAdapter);
}
public void startExercise(int selection){
Intent exIntent = new Intent(this, CommenceExercise.class);
Exercise chosenEx = myExercises.get(selection);
Bundle info = new Bundle();
info.putLong("duration", chosenEx.getTime());
info.putString("name", chosenEx.getName());
info.putString("description", chosenEx.getDescription());
exIntent.putExtras(info);
startActivity(exIntent);
}
}
或将这三行添加到您的 xml
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true
android:descendantFocusability="blocksDescendants"
如果有任何可获得焦点的子视图,那么第一次点击将始终获得焦点
我建议您将自定义 ListView
与 Viewholder
Pattern 一起使用以提高效率,或者使用支持库的 Recyclerview
这将使您的滚动平滑并且不会创建额外的视图对象......它只会创建适合屏幕的视图对象
这里是 Link RecyclerView
https://guides.codepath.com/android/using-the-recyclerview
将您的 refreshList
方法更改为以下方法
public void refreshList(){
arrayAdapter.notifyDataSetChanged();
}
您正在做的是再次膨胀 ListView
并为其设置新数据,而不是更新旧数据。当您为 ListView
分配新值时,您忘记设置 onClickListener
.
它会起作用。
您需要从 xml 中删除以下列表视图属性:
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
此外,不需要 refreshList() 这实际上是一种糟糕的做事方式,而是在您的 addNewActivity() 中,一旦您的模型准备就绪(可能在单击肯定按钮时)将该项目添加到 arrayAdapter 和然后做 arrayAdapter.notifyDataSetChanged().
希望对您有所帮助!