简体   繁体   English

单击列表视图上的项目不起作用

[英]Click on an item on Listview doesn't work

I want to open a new intent when I click on an item in my ListView. 当我在ListView中单击一个项目时,我想打开一个新的意图。 When I use setTask() in the item_todo.xml directly it works but open the first one. 当我直接在item_todo.xml中使用setTask()时,它可以工作,但打开第一个。

So I have to use the OnItemClick but it doesn't do anything... Here is my TodoListActivity, the part that matters: 因此,我必须使用OnItemClick,但它什么也不做...这是我的TodoListActivity,重要的部分:

public class TodoListActivity extends AppCompatActivity {


// for the calendar
    Calendar calendar = Calendar.getInstance();
    static String getTitleTask;
    static String getDescTask;
    static String getYearTask;
    static String getMonthTask;
    static String getDayTask;
    static String getHourTask;
    static String getMinTask;
    //todolist with databse

    public static final String TAG = "TodoListActivity";
    private TodoHelper mHelper;
    private ListView mTodoListView;

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

        // todolist with database
        mHelper = new TodoHelper(this);
        mTodoListView = (ListView) findViewById(R.id.list_todo);
        List<TaskData> tasks = genererTasks();
        TaskAdapter taskAdapter = new TaskAdapter(TodoListActivity.this, tasks);
        mTodoListView.setAdapter(taskAdapter);
        taskAdapter.notifyDataSetChanged();
        mTodoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    System.out.println("This should show when press on one button and should be the item at position"+i);
                    Toast.makeText(TodoListActivity.this, "Hello world", Toast.LENGTH_LONG).show();
                }
            });
    }

    public List<TaskData> genererTasks() {

        List<TaskData> taskDataList;
        TodoHelper.init(TodoListActivity.this);
        taskDataList = TodoHelper.getAllUserData();

        return taskDataList;
    }
}

Here is the activity_todo_list.xml 这是activity_todo_list.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_todo_list2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.schmid_charlesa_esig.wakemeup.TodoListActivity">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_todo"
        android:clickable="true">
    </ListView>
</RelativeLayout>

And finally the item_todo.xml 最后是item_todo.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/todoTitle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Hi"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/todoDesc"
        android:layout_below="@id/todoTitle"
        android:text="desc"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/todoDate"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="DateOfDay"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/todoHour"
        android:layout_below="@id/todoDate"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="Heure"
        android:textSize="20sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/todoDelete"
        android:layout_alignParentBottom="true"
        android:onClick="deleteTask"
        android:layout_centerHorizontal="true"
        android:text="Done"/>

</RelativeLayout>

I'm using sqlite if it's matters. 如果有问题,我正在使用sqlite。 Thanks for the replies. 感谢您的答复。

So thanks to @0X0nosuga I've figured out there were too much android:clickable="true" 因此,感谢@ 0X0nosuga,我发现android:clickable="true"太多了

When there is a clickable thing in an item of the listview we need to put android:focusable="false" in the button. 当列表视图的某项中有可点击的东西时,我们需要在按钮中放入android:focusable="false"

Here is the code working : 这是工作代码:

TodoListActivity.java TodoListActivity.java


public class TodoListActivity extends AppCompatActivity {


// for the calendar
    Calendar calendar = Calendar.getInstance();
    static String getTitleTask;
    static String getDescTask;
    static String getYearTask;
    static String getMonthTask;
    static String getDayTask;
    static String getHourTask;
    static String getMinTask;
    //todolist with databse

    public static final String TAG = "TodoListActivity";
    private TodoHelper mHelper;
    private ListView mTodoListView;

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

        // todolist with database
        mHelper = new TodoHelper(this);
        mTodoListView = (ListView) findViewById(R.id.list_todo);
        List<TaskData> tasks = genererTasks();
        TaskAdapter taskAdapter = new TaskAdapter(TodoListActivity.this, tasks);
        mTodoListView.setAdapter(taskAdapter);
        taskAdapter.notifyDataSetChanged();
        mTodoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    System.out.println("This should show when press on one button and should be the item at position"+i);
                    Toast.makeText(TodoListActivity.this, "Hello world", Toast.LENGTH_LONG).show();
                }
            });
    }

    public List<TaskData> genererTasks() {

        List<TaskData> taskDataList;
        TodoHelper.init(TodoListActivity.this);
        taskDataList = TodoHelper.getAllUserData();

        return taskDataList;
    }
}

item_todo.xml item_todo.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/todoTitle"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:text="Hi"
    android:textSize="20sp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/todoDesc"
    android:layout_below="@id/todoTitle"
    android:text="desc"
    android:textSize="20sp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/todoDate"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="DateOfDay"
    android:textSize="20sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/todoHour"
    android:layout_below="@id/todoDate"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Heure"
    android:textSize="20sp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/todoDelete"
    android:layout_alignParentBottom="true"
    android:onClick="deleteTask"
    android:layout_centerHorizontal="true"
    android:focusable="false"
    android:text="Done"/>
</RelativeLayout>

activity_todo_list.xml activity_todo_list.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_todo_list2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.schmid_charlesa_esig.wakemeup.TodoListActivity">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_todo">
    </ListView>
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM