【发布时间】:2017-03-19 14:57:12
【问题描述】:
当我单击 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。 感谢您的回复。
【问题讨论】:
-
请替换行“System.out.println(...);”使用
Toast.makeText(TodoListActivity.this, "Hello world", Toast.LENGTH_LONG).show();,在您的设备/模拟器上运行它并尝试再次单击列表项。有什么事吗? -
不,它不会改变任何东西......
-
你为什么写这个 mTodoListView.setItemsCanFocus(false);?
-
好的,只是想确定一下(不知道 System.out.println() 是否会进入 Logcat)。到目前为止,您的代码看起来不错,除了
setItemsCanFocus(),可能与布局 xml 有一些冲突。你能发布布局吗? -
如果列表项包含可聚焦/可点击的元素,也许这个SO post 会有所帮助
标签: java android sqlite listview arraylist