无法使用CustomAdapter <myclass>启用上下文操作模式

时间:2017-01-25 16:45:03

标签: java android xml

我尝试使用继承自ListView的自定义适配器在ArrayAdapter<MyClass>中启用上下文操作模式。并且不能。长按一下就什么也没发生。在用户界面和日志中都没有 当我使用ArrayAdapter<String>时,一切正常。我的错误是什么,请解释一下。提前谢谢。

这是我的档案。

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);
}
}

NoteAdapter.java

class NoteAdapter extends ArrayAdapter<Note> {

private AppCompatActivity parentActivity;

NoteAdapter(Context context, List<Note> notes) {
    super(context, R.layout.note_item, notes);
    this.parentActivity = (AppCompatActivity) context;
}

@NonNull
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final Note note = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.note_item, parent, false);
        convertView.setLongClickable(true);
    }
    ((TextView) convertView.findViewById(R.id.note)).setText(note.getText());
    ((TextView) convertView.findViewById(R.id.date_last_edited)).setText(note.getFormattedDate());
    return  convertView;
}

}

Note.java

class Note {

private String note;
private Calendar date_last_edited;

Note(String note) {
    this.note = note;
    this.date_last_edited = Calendar.getInstance();
}

Note(String note, Calendar date) {
    this.note = note;
    this.date_last_edited = date;
}

void setText(String text) {
    this.note = text;
    this.date_last_edited = Calendar.getInstance();
}

String getText() {
    return this.note;
}

Calendar getDate() {
    return this.date_last_edited;
}

String getFormattedDate() {
    SimpleDateFormat formattedDate= new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
    return "Last edited: " + formattedDate.format(date_last_edited.getTime());
}

}

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>

note_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>

0 个答案:

没有答案