0
Onclick和OnlongClick不起作用,我使用AppCompatActivity來支持KitKat。我不知道爲什麼它不起作用。它是適配器還是視圖?我知道我正在使用已棄用的適配器,但我將其用於某個目的。ListView適配器 - OnClick啓動一個新的Activity&OnLongClick啓動一個ContextActionBar
下面的代碼:
public class MainActivity extends AppCompatActivity {
ActionMode mActionMode;
private CartDbAdapter dba;
private Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
databaseview();
}
else {
setContentView(R.layout.cart);
dba = new CartDbAdapter(this);
dba.open();
}
private void databaseview(){
cursor = dba.fetchAllBooks();
ListView listView = (ListView) findViewById(list);
startManagingCursor(cursor);
String [] from = new String[] {BookContract.TITLE , BookContract.AUTHORS };
int [] to = new int [] {android.R.id.text1 , android.R.id.text2 };
SimpleCursorAdapter databaseAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, cursor, from, to);
listView.setAdapter(databaseAdapter);
listView.setOnLongClickListener(longListener);
listView.setOnClickListener(clickListener);
}
View.OnLongClickListener longListener = new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = MainActivity.this.startSupportActionMode(callback);
view.setSelected(true);
return true;
}
};
View.OnClickListener clickListener = new View.OnClickListener(){
public void onClick(View view) {
Intent viewIntent = new Intent(MainActivity.this, BookActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(BookActivity.KEY, dba.fetchBook(view.getId()));
viewIntent.putExtras(bundle);
startActivity(viewIntent);
}
};
private ActionMode.Callback callback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextbar_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
mode.finish();
return true;
default:
return false;
}
}
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
謝謝!
檢查此[鏈接](http://www.androhub.com/android-contextual-action-mode-over -toolbar /)。 –