无法为ListView设置onItemClickListener

时间:2019-06-24 08:11:43

标签: java android onitemclicklistener onitemclick

我的应用程序基本上是一个简单的待办事项列表-用户可以添加项目,将其选中并删除。通过长按或双击列表项来进行删除。在启动屏幕上选择了哪个手势(不要问,这是大学的事情)。添加和检查项目非常有效,长按即可删除。但是,我无法双击工作。我在手势检测方面做了大量工作,作为一个实验,我现在试图简单地使该应用程序检测到我单击/轻击ListView中的一个项目。我不明白为什么我无法使它正常工作,因为长按可以完美工作。

我的主要活动TodoActivity:

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View.*;


import java.util.ArrayList;

public class TodoActivity extends AppCompatActivity implements
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener{
    private GestureDetectorCompat mDetector;

    Button addButton;
    EditText textEditor;
    DbHelper dbHelper;
    ListView listView;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo);

        addButton = findViewById(R.id.add_button);
        textEditor = findViewById(R.id.entry_input);
        dbHelper = new DbHelper(this);
        listView = findViewById(R.id.list_view);

        showItemList();

        addButton.setOnClickListener(new OnClickListener(){

            public void onClick(View view){
                String text = textEditor.getText().toString();
                if (!text.equals("")){
                    textEditor.setText("");
                    dbHelper.insertNewItem(text);
                    showItemList();
                } else {
                }
            }
        });

        listView.setOnItemClickListener(new ListView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    //code never reaches this point
                    System.out.println("++++++ Tap on item detected ++++++");
                }
         });

    }

    public void deleteListItem(View view) {
        TextView tv = view.findViewById(R.id.item_text);
        String itemText = String.valueOf((tv).getText());
        dbHelper.deleteItem(itemText);
        showItemList();
    }


    private void showItemList() {
        ArrayList<String> itemList = dbHelper.getTodoList();
        if(adapter==null) {
            adapter = new ArrayAdapter<>(this, R.layout.listitems, R.id.item_text, itemList);
            listView.setAdapter(adapter);
        }
        else {
            adapter.clear();
            adapter.addAll(itemList);
            adapter.notifyDataSetChanged();
        }
    }
}

dbhelper,以备您复制:

package com.example.p10_studio_version;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;

public class DbHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "ToDoDB";
    public static final String TABLE_NAME = "ItemsToDo";
    public static final String COLUMN_NAME = "Items";
    public static final int DB_VERSION = 1;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = String.format("CREATE TABLE %s (ID INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT NOT NULL);", TABLE_NAME, COLUMN_NAME);
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = String.format("DELETE TABLE IF EXISTS %s", TABLE_NAME);
        db.execSQL(query);
        onCreate(db);
    }

    public void insertNewItem(String item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values= new ContentValues();
        values.put(COLUMN_NAME, item);
        db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        db.close();
    }
    public void deleteItem(String item) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, COLUMN_NAME + " = ?", new String[]{item});
        db.close();
    }

    public ArrayList<String> getTodoList() {
        ArrayList<String> todoList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.query(TABLE_NAME, new String[]{COLUMN_NAME}, null, null, null, null, null);
        while (c.moveToNext()) {
            int index = c.getColumnIndex(COLUMN_NAME);
            todoList.add(c.getString(index));
        }
        c.close();
        db.close();
        return todoList;
    }
}

activity_todo.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>-->
<androidx.constraintlayout.widget.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:background="#DEDFE2"
    tools:context="com.example.p10_studio_version.TodoActivity"
    android:id="@+id/relativeLayout">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="@string/title_text"
        android:textSize="50sp"
        app:layout_constraintBottom_toTopOf="@+id/list_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="#80DF675D"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"
        android:descendantFocusability="blocksDescendants"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <EditText
        android:id="@+id/entry_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"

        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="8dp"
        android:autofillHints="hi"
        android:background="#B3FFFFFF"
        android:gravity="bottom"
        android:inputType="text"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@+id/add_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="20dp"
        android:background="#DF675C"
        android:text="@string/button_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.606"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <ImageView
        android:id="@+id/doubletap_anim"
        android:layout_width="126dp"
        android:layout_height="126dp"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="332dp"
        android:visibility="gone"
        app:background="@drawable/animation_double"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/list_view" />

    <ImageView
        android:id="@+id/longtap_anim"
        android:layout_width="126dp"
        android:layout_height="126dp"
        android:layout_marginStart="68dp"
        android:layout_marginLeft="68dp"
        android:layout_marginTop="332dp"
        android:visibility="gone"
        app:background="@drawable/animation_long"
        app:layout_constraintStart_toEndOf="@id/doubletap_anim"
        app:layout_constraintTop_toTopOf="@id/list_view" />


</androidx.constraintlayout.widget.ConstraintLayout>

listitems.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:longClickable="true"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants" >


    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:scaleX="2"
        android:scaleY="2"
        android:text="@null"
        android:translationX="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/item_text"
        app:layout_constraintTop_toTopOf="parent"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:foregroundGravity="center_horizontal"
        android:gravity="left|center_vertical"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingRight="10dp"
        android:paddingBottom="5dp"
        android:textSize="20sp"
        app:layout_constraintHeight_min="50dp"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        tools:layout_conversion_wrapHeight="71"
        tools:layout_conversion_wrapWidth="258"
        tools:layout_editor_absoluteY="10dp"
        tools:ignore="MissingConstraints"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 个答案:

没有答案