Java Can';t使用CustomAdapter启用上下文操作模式<;MyClass>;
我正试图使用从Java Can';t使用CustomAdapter启用上下文操作模式<;MyClass>;,java,android,xml,Java,Android,Xml,我正试图使用从ArrayAdapter继承的自定义适配器在ListView中启用上下文操作模式。而且不能。只是长时间点击没有发生任何事情。既不在UI中,也不在日志中。 当我使用ArrayAdapter时,一切正常。我犯了什么错,请解释一下。提前谢谢 这是我的档案 MainActivity.java public class MainActivity extends AppCompatActivity { public static final String TAG = "LOG"; List
ArrayAdapter
继承的自定义适配器在ListView
中启用上下文操作模式。而且不能。只是长时间点击没有发生任何事情。既不在UI中,也不在日志中。当我使用
ArrayAdapter
时,一切正常。我犯了什么错,请解释一下。提前谢谢
这是我的档案
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String TAG = "LOG";
ListView listView;
ArrayAdapter<Note> adapter; // It doesn't work.
//ArrayAdapter<String> adapter; // It works.
List<Note> notes;
static final int REQUEST_CODE_NOTE = 1;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(android.R.id.list);
notes = new ArrayList<>();
notes.add(new Note("First note"), new Note("Second note"));
adapter = new NoteAdapter(this, notes);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
Log.d(MainActivity.TAG, "onCreateActionMode");
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
Toast.makeText(MainActivity.this, "onCreateActionMode", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
actionMode.finish();
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
//registerForContextMenu(listView);
btnAdd = (Button) findViewById(R.id.add_button);
btnAdd.setOnClickListener(this);
}
}
activity_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="0dp"
android:paddingStart="0dp"
android:paddingRight="0dp"
android:paddingEnd="0dp"
android:paddingTop="0dp"
android:background="@color/colorBackground">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="2dp"
android:id="@+id/add_button"
android:text="@string/add_button"
android:background="@drawable/button_background"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/add_button"
android:padding="0dp"
android:layout_marginLeft="2dp"
android:layout_marginStart="2dp"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="0dp"
android:divider="@android:color/transparent"
android:dividerHeight="6dp"/>
</RelativeLayout>
注意_item.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/note_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/note_margin"
android:paddingLeft="@dimen/note_margin"
android:paddingRight="@dimen/note_margin"
android:paddingTop="@dimen/note_margin"
android:background="?android:attr/activatedBackgroundIndicator"
android:clickable="true"
android:focusable="true"
android:longClickable="true"
tools:context="ru.geekbrains.lesson7.simplenotes.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:padding="10dp"
android:id="@+id/note"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test note"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/note"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:id="@+id/bottom_panel" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/date_last_edited"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date: 01.01.2017 12:33:21"/>
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/delete_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="Delete note"
android:src="@drawable/delete"/>
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/edit_button"
android:layout_toLeftOf="@id/delete_button"
android:layout_toStartOf="@id/delete_button"
android:contentDescription="Edit note"
android:src="@drawable/edit"/>
</RelativeLayout>
</RelativeLayout>
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="@string/delete_all_context_menu"
android:id="@+id/delete_all_context"
android:icon="@android:drawable/ic_menu_delete">
</item>
</menu>
将代码减少到可验证的最小完整示例将大大有助于获得答案。你的代码墙太大了。如果我知道错误在哪里,我肯定会减少代码。通过删除代码并保留问题,你通常可以自己发现问题。如果你花点时间举一个简单的例子,你会让更多的人关注你的问题。谢谢你的建议。我明白这一点。我花了今天的时间来确定问题,但没有成功。所以我认为在这种情况下,所有这些代码都与搜索解决方案相关。
<?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/note_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/note_margin"
android:paddingLeft="@dimen/note_margin"
android:paddingRight="@dimen/note_margin"
android:paddingTop="@dimen/note_margin"
android:background="?android:attr/activatedBackgroundIndicator"
android:clickable="true"
android:focusable="true"
android:longClickable="true"
tools:context="ru.geekbrains.lesson7.simplenotes.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:padding="10dp"
android:id="@+id/note"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test note"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/note"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:id="@+id/bottom_panel" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/date_last_edited"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date: 01.01.2017 12:33:21"/>
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/delete_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="Delete note"
android:src="@drawable/delete"/>
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/edit_button"
android:layout_toLeftOf="@id/delete_button"
android:layout_toStartOf="@id/delete_button"
android:contentDescription="Edit note"
android:src="@drawable/edit"/>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="@string/delete_all_context_menu"
android:id="@+id/delete_all_context"
android:icon="@android:drawable/ic_menu_delete">
</item>
</menu>