私のデータベースからデータを取得していて、それを自分のリスト項目として設定したいと思います。アイテムをクリックして保持すると、ダイアログを開いて、指定されたオプションの1つを選択するようユーザーに求めます。次のように私のコードは次のとおりです。 マイMainActivity.javaクラス私はonItemLongClickListenerをリストビューに設定しましたが、リスト項目をクリックすると何も起こりません。どうすればこの問題を解決できますか?
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();
}
}
マイ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>
マイレイアウトリスト項目のために:
<?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>
あなたはOnItemclickリスナーを設定しましたが、あなたのコードではOnItemLongclickListnerを移植したと言いました。最初に実装するメソッドを確認してください。 –
あなたのレイアウトコードをここに追加してください。おそらく乗り物のクリックイベントの上にあるレイアウトがあります。 – hchouhan02
アイテムをクリックして**保持していない可能性があります。私はクリックだけがこのメソッドを呼び出さないと思う。 –