首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnItemClickListener未触发

OnItemClickListener未触发
EN

Stack Overflow用户
提问于 2017-07-12 12:34:17
回答 4查看 104关注 0票数 0

我正在制作一个应用程序,它有一个在ListView中显示的练习列表。我尝试让用户从列表中选择一项以启动新活动,但我的OnItemClickListener无法触发。下面是我的Activity(不是listActivity,它是appCompatActivity):

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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行从未打印过。我不知道为什么我不能让它工作。任何帮助都将不胜感激。提前谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-07-12 13:50:49

您需要从xml中删除以下listview属性:

代码语言:javascript
复制
    android:descendantFocusability="blocksDescendants"
    android:focusable="true"
    android:clickable="true" 
    android:focusableInTouchMode="true"

此外,不需要refreshList()这实际上是一种糟糕的处理方法,相反,在addNewActivity()中,一旦模型准备好(可能是单击肯定按钮),就将该项目添加到arrayAdapter中,然后执行arrayAdapter.notifyDataSetChanged()。

我希望这对你有帮助!

票数 0
EN

Stack Overflow用户

发布于 2017-07-12 12:49:08

我认为XML中有问题请设置ListView可点击的true

请在XML中进行一些更改

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-07-12 13:05:39

这真的很奇怪..。你的代码应该是可以工作的.我建议您尝试在您的活动中实现,以包括lv.setClcickable(true)lv.setEnable(true)

代码语言:javascript
复制
 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中

代码语言:javascript
复制
 android:focusable="false"
 android:focusableInTouchMode="false"
 android:clickable="true
 android:descendantFocusability="blocksDescendants"

如果有任何可聚焦的子视图,那么第一次单击将始终是焦点,我建议您使用带有Viewholder模式的自定义视图以提高效率,或者使用支持库的Recyclerview。这将使滚动更平滑,不会创建额外的视图对象。它将只创建适合屏幕的View对象

这是RecyclerView https://guides.codepath.com/android/using-the-recyclerview的链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45048520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档