嗨,我更改了应用程序状态,然后我想重新加载活动以反映我尝试使用 notifyDataSetChanged 的更改,但它也不工作......基本上当我选择“删除”选项时,删除操作有效,但它不刷新ListActivity 上的光标适配器 ContextMenu:
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
View view = info.targetView;
long id = info.id;
if(item.getItemId()==R.id.edit){
Intent i=new Intent(ProjetoProTelefoneActivity.this, DetailForm.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
return(true);
}
else if(item.getItemId()==R.id.remove){
Intent i=new Intent(ProjetoProTelefoneActivity.this, DeleteItemList.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
ListView listView = getListView();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return(true);
}
return super.onContextItemSelected(item);
}
我的初始化列表():
private void initList(){
if(modelo!=null){
stopManagingCursor(modelo);
modelo.close();
}
modelo =helper.getAll(prefs.getString("sort_order","nome DESC"));
startManagingCursor(modelo);
adapter = new RestaurantAdapter(modelo);
setListAdapter(adapter);
}
删除项目列表:
public class DeleteItemList extends Activity{
RestauranteHelper helper = null;
String restauranteId= null;
public void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
helper= new RestauranteHelper(this);
restauranteId=getIntent().getStringExtra(ProjetoProTelefoneActivity.ID_EXTRA);
helper.delete(restauranteId);
finish();
}
public void onDestroy(){
super.onDestroy();
helper.close();
}
}
我的 RestauranteHelper.delete:
public void delete(String id){
String[] args = {id};
getWritableDatabase().delete("restaurantes", "_ID =?", args);
}
类餐厅适配器:
class RestaurantAdapter extends CursorAdapter {
RestaurantAdapter(Cursor c) {
super(ProjetoProTelefoneActivity.this, c);
}
public void bindView(View row, Context ctxt,
Cursor c) {
RestaurantHolder holder=(RestaurantHolder)row.getTag();
holder.populateFrom(c, helper);
}
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
RestaurantHolder holder=new RestaurantHolder(row);
row.setTag(holder);
return(row);
}
}
我有其他主题,但我在那里尝试做的所有事情都没有工作......我知道该怎么做,删除操作有效,但不要刷新光标适配器
在我更正代码后,onContextItemSelected 看起来像,并且我不再使用 DeleteItemList,如您所见:
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
View view = info.targetView;
long id = info.id;
if(item.getItemId()==R.id.edit){
Intent i=new Intent(ProjetoProTelefoneActivity.this, DetailForm.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
return(true);
}
else if(item.getItemId()==R.id.remove){
restauranteId=String.valueOf(id);
//Toast.makeText(this, String.valueOf(id), Toast.LENGTH_LONG).show();
helper.delete(restauranteId);
initList();
return(true);
}
return super.onContextItemSelected(item);
}
谢谢大家!!