I want to open a new intent when I click on an item in my ListView. When I use setTask() in the item_todo.xml directly it works but open the first one.
So I have to use the OnItemClick but it doesn't do anything... Here is my TodoListActivity, the part that matters:
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
<?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
<?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. Thanks for the replies.