0

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.

Charly
  • 11
  • 5
  • Please replace the line "System.out.println(...);" with `Toast.makeText(TodoListActivity.this, "Hello world", Toast.LENGTH_LONG).show();` , run this on your device/emulator and try to click on a list item once more. Does anything happen? – Bö macht Blau Mar 19 '17 at 15:10
  • No it doesn't change anything ... – Charly Mar 19 '17 at 15:20
  • why you wrote this mTodoListView.setItemsCanFocus(false);? – Jaydeep Devda Mar 19 '17 at 15:27
  • Ok, just wanted to make sure (don't know if System.out.println() will go into Logcat). Your code looks good so far, except for `setItemsCanFocus()`, maybe there's some conflict with the layout xml. Could you post the layout? – Bö macht Blau Mar 19 '17 at 15:28
  • If the list items contain focusable/clickable elements, maybe this [SO post](http://stackoverflow.com/questions/8413656/onitemclicklistener-doesnt-work-with-listview-item-containing-button) is helpful – Bö macht Blau Mar 19 '17 at 15:32
  • @0X0nosugar Thanks I've reread my code and it was actually `android:focusable="false"` on the button not in item_todo – Charly Mar 19 '17 at 15:46

1 Answers1

0

So thanks to @0X0nosuga I've figured out there were too much 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.

Here is the code working :

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>
Charly
  • 11
  • 5