我在我的数据库中有一些值,并使用自定义光标适配器填充自定义Listview中的值。因此,当用户点击listView时,会打开一个新的活动并且他选择该值或视图的删除按钮应消失,并且应该刷新列表。从列表视图中删除视图/值Android
我在SO上提到了很多答案,但他们都建议从数据库中删除值(这不是我想要的),有的使用ArrayList,并建议我将它与自定义数组适配器一起使用。但是我我正在使用光标适配器。
所以我的整个问题是如何删除值或从列表视图中删除视图?
的ListView项的onClick监听器打开一个新的活动
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Constants.ROW_NUMBER=position;
TextView getRecordID = (TextView) view.findViewById(R.id.recordID);
Constants.ROW_NUMBER = Integer.parseInt(getRecordID.getText().toString());
Intent editIntent = new Intent(this,NoteDisplay.class);
editIntent.putExtra(Constants.ROW_NAME,Constants.ROW_NUMBER);
startActivity(editIntent);
}
这是新的活动,并在菜单按钮的按下记录将被标记为删除,但不会从数据库中删除。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.deletebutton:
DataBaseHelper deletedb = new DataBaseHelper(this);
int deletedata = deletedb.deleteAction(position,System.currentTimeMillis());
Intent dintent = new Intent(this,MainActivity.class);
dintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(dintent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是我的自定义光标适配器类,
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.customlistview, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//int getDeleteFlag is initialized above
getDeleteFlag = cursor.getInt(cursor.getColumnIndex(DELETE_FLAG));
if(getDeleteFlag==0)
{
ll = (LinearLayout)view.findViewById(R.id.listViewLayout);
setListViewHeight(ll,context);
if (cursor != null) {
getText = cursor.getString(cursor.getColumnIndex(NOTE_TITLE));
existsRecordID = cursor.getString(cursor.getColumnIndex(RECORD_ID));
datevalue = cursor.getLong(cursor.getColumnIndex(RECORD_DATE));
}
Date newdate = new Date(datevalue);
tv = (TextView) view.findViewById(R.id.content);
recordID = (TextView) view.findViewById(R.id.recordID);
dateET = (TextView) view.findViewById(R.id.date);
tv.setText(getText.trim());
recordID.setText(existsRecordID);
dateET.setText(dateFormatter.format(newdate));
}else{
Toast.makeText(context, "FALSE VALUE", Toast.LENGTH_SHORT).show();
}
}
我很困惑是否要删除视图或在该点的数值。
我有一个名为“deleteflag”的行,在我的DB中存储1 =>删除了 0 =>未删除。
所以只有具有1的值才会被标记为删除。
编辑1:
我bindView()
方法上面的代码不起作用
如果你只是从列表视图中删除,那么它将被删除,但它不会从数据库中删除,所以当你再次在该页面时,你可以看到删除的视图(行),因为它不会从数据库中删除 – Piyush
@Piyush如果我检查'if(deleteflag == 0)'只有显示视图否则不显示?如果是的话,这怎么办? –
您可以将该标志存储在数据库中 – Piyush