0

I am taking data from my database using and want to set that as my list item. Upon clicking and holding an item, I want a dialog to open prompting the user to select one of the given options. My code is as follows: My MainActivity.java class

public class MainActivity extends AppCompatActivity {
ListView todoList;
DbHelper dbHelper;
ArrayAdapter<String> myAdapter;
Button rename, delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHelper=new DbHelper(this);
    todoList=(ListView)findViewById(R.id.to_do);
    loadTaskList();
    todoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
            Dialog dialog=new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.item_dialog);
            dialog.setTitle("Select");
            rename = dialog.findViewById(R.id.rename_task);
            delete = dialog.findViewById(R.id.delete_task);
            dialog.show();
            return false;
        }
    });
}

private void loadTaskList() {
    ArrayList<String> taskList=dbHelper.getTask();
    if(myAdapter==null){
        myAdapter=new ArrayAdapter<String>(this,R.layout.todo_item,R.id.task_title,taskList);
        todoList.setAdapter(myAdapter);
    }else {
        myAdapter.clear();
        myAdapter.addAll(taskList);
        myAdapter.notifyDataSetChanged();
    }
}

My activity_main.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mylist.MainActivity">

    <ListView
            android:id="@+id/to_do"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"/>
</LinearLayout>

My Layout for the list item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">
    <TextView
        android:id="@+id/task_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Task"
        android:textSize="20sp"
        android:layout_centerVertical="true"
        android:padding="5dp"/>

    <ImageButton
        android:id="@+id/task_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_delete_black_24dp" />
    <ImageButton
        android:id="@+id/task_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_done_black_24dp"
        android:layout_toLeftOf="@id/task_delete"/>
</RelativeLayout>
  • Use `registerForContextMenu` inside of onCreate, passing in your list view as a parameter. Then define your menu inside of `onCreateContextMenu` and respond to user selection in `onContextItemSelected` – dr_g Jun 27 '17 at 17:20
  • Just remove `android:clickable="true"` from the RelativeLayout in the list item layout, and it should work. – Daniel Nugent Jun 27 '17 at 17:27
  • 1
    @DanielNugent That did not work. It is still the same. – sahaj jain Jun 27 '17 at 18:08

0 Answers0