我已成功使用SQLite db
将数据从listview
填充到SimpleCursorAdapter
。
现在我想使用Contextual Action Mode
从listview中删除一些特定的行及其等效的SQLite数据。
到目前为止,我已经能够添加动作模式并相应地进行行选择。
但是,我无法弄清楚如何从列表视图中删除所选行并从SQLite数据库中删除它们。
非常感谢!
这是我的main activity
:
public class FavBawords3 extends ListActivity {
private FavBaDbAdapter2 dbHelper;
SQLiteDatabase db;
String DATABASE_FILE_PATH= Environment.getExternalStorageDirectory() + "/FAVID";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fav_ba_main2);
if (Build.VERSION.SDK_INT >= 11) {
ActionBar actionBar = getActionBar();
getActionBar().setDisplayHomeAsUpEnabled(true);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.bg_new);
Drawable dr = new BitmapDrawable(this.getResources(), myImage);
actionBar.setBackgroundDrawable(dr);
actionBar.setTitle(Html.fromHtml("<font color=\"#FFFFFF\">" + "<b>" + getString(R.string.TextFavourite) + "</b"+ "</font>"));
} else {
}
dbHelper = new FavBaDbAdapter2(this);
dbHelper.open();
db = this.openOrCreateDatabase(DATABASE_FILE_PATH + File.separator + "favtemp", Context.MODE_PRIVATE, null);
displayListView();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getActionBar().setSubtitle("Long press to start selection");
}
public void myClickHandler(View v)
{
//get the row the clicked button is in
LinearLayout child = (LinearLayout) v.getParent().getParent();
LinearLayout parent = (LinearLayout) child.getParent();
TextView text=(TextView)parent.findViewById(R.id.code);
if (v.getId() == v.getId()) {
Toast.makeText(getApplicationContext(),
text.getText().toString(), Toast.LENGTH_SHORT).show();
} else if (v.getId() == v.getId()) {
int count = Integer.parseInt(text.getText().toString());
if (!(count == 0)) {
//text.setText("" + (--count));
Toast.makeText(getApplicationContext(),
text.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllItems();
// The desired columns to be bound
String[] columns = new String[] {
FavBaDbAdapter2.KEY_CODE,
FavBaDbAdapter2.KEY_NAME,
FavBaDbAdapter2.KEY_TIME,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.code,
R.id.name,
R.id.time,
};
// Simple cursor adapter and set it to display
final SimpleCursorAdapter colours =
new SimpleCursorAdapter(this, R.layout.fav_ba_grow2, cursor, columns, to, 0 );
setListAdapter(colours);
colours.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId()==R.id.name)
{
int incOrExpIndex = cursor.getColumnIndex("content");
int incOrExp = cursor.getInt(incOrExpIndex);
switch(incOrExp) {
case 0:
TextView tv0 = (TextView)view;
//tv0.setTextColor(Color.BLUE);
tv0.setTypeface(null, Typeface.ITALIC);
tv0.setText(Utility.decodeContent(cursor.getString(cursor.getColumnIndex("content"))));
break;
case 1:
TextView tv1 = (TextView)view;
tv1.setTextColor(Color.RED);
tv1.setText(cursor.getString(cursor.getColumnIndex("content")));
break;
}
return true;
}
if (view.getId()==R.id.code)
{
int incOrExpIndex = cursor.getColumnIndex("word");
int incOrExp = cursor.getInt(incOrExpIndex);
switch(incOrExp) {
case 0:
TextView tv0 = (TextView)view;
//tv0.setTypeface(null, Typeface.BOLD);
//tv0.setText(cursor.getString(cursor.getColumnIndex("word")));
tv0.setText(Utility.decodeContent(cursor.getString(cursor.getColumnIndex("word"))));
break;
}
return true;
}
return false;}
});
ListView lvItems = getListView(); // Assign adapter to ListView
lvItems.setAdapter(colours);
lvItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lvItems.setMultiChoiceModeListener(new ModeCallback());
lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return true;
}
});
lvItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String FavWord = Utility.encodeContent(cursor.getString(cursor.getColumnIndexOrThrow("word")));
}
});
colours.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchItemsByName(constraint.toString());
}
});
}
private class ModeCallback implements ListView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fav_view2, menu);
mode.setTitle("Select Items");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
getCheckedItems();
mode.finish();
break;
default:
Toast.makeText(FavBawords3.this, "Clicked " + item.getTitle(),
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private String getCheckedItems() {
SparseBooleanArray checked = getListView().getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i) == true) {
String test = getListView().getItemAtPosition(checked.keyAt(i)).toString();
Toast.makeText(FavBawords3.this, "Clicked " + test,
Toast.LENGTH_SHORT).show();
}
else
{
}
}
// TODO Auto-generated method stub
return null;
}
public void onDestroyActionMode(ActionMode mode) {
}
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = getListView().getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + checkedCount + " items selected");
break;
}
}
}
}