当我点击ListView中的某个项目时,我想打开一个新的意图。 当我在item_todo.xml中直接使用setTask()时,它可以工作但是打开第一个。
所以我必须使用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;
}
}
这是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>
最后是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>
我使用sqlite是否很重要。 谢谢你的回复。
答案 0 :(得分:0)
非常感谢@ 0X0nosuga我发现太多了
android:clickable="true"
当列表视图的项目中有可点击的内容时,我们需要将android:focusable="false"
放在按钮中。
以下代码正在运行:
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
<?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
<?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>