我想刷新/更新afeter我选择“删除”ContextItem,它删除但不“刷新”列表视图 请参阅代码ListActivity:
public class ProjetoProTelefoneActivity extends ListActivity {
public final static String ID_EXTRA = "br.com.DaniloDeLuca.ProjetoProTelefone._ID";
Cursor modelo = null;
RestaurantAdapter adapter = null;
RestauranteHelper helper=null;
SharedPreferences prefs=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper = new RestauranteHelper(this);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
initList();
prefs.registerOnSharedPreferenceChangeListener(prefListener);
registerForContextMenu(getListView());
}
public void onDestroy(){
super.onDestroy();
helper.close();
}
//*********************************************************************************************************************************
// Long Press menu
//*********************************************************************************************************************************
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
return;
}
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
if (cursor == null) {
return;
}
new MenuInflater(this).inflate(R.menu.option_item,menu);
super.onCreateContextMenu(menu,v,menuInfo);
}
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);
return(true);
}
return super.onContextItemSelected(item);
}
//*********************************************************************************************************************************
// Fim Long Press menu
//*********************************************************************************************************************************
public void onListItemClick(ListView list, View view,
int position,long id){
Intent i=new Intent(ProjetoProTelefoneActivity.this, DetailForm.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
//hook into menu button for activity
public boolean onCreateOptionsMenu(Menu menu){
new MenuInflater(this).inflate(R.menu.option,menu);
return(super.onCreateOptionsMenu(menu));
}
/// when menu button option selected
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.add){
startActivity(new Intent(ProjetoProTelefoneActivity.this, DetailForm.class));
return(true);
}
else if(item.getItemId()==R.id.prefs){
startActivity(new Intent(this, EditPreferences.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
private SharedPreferences.OnSharedPreferenceChangeListener prefListener=
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if(key.equals("sort_order")){
initList();
}
}
};
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);
}
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);
}
}
static class RestaurantHolder {
private TextView name=null;
private TextView address=null;
private ImageView icon=null;
RestaurantHolder(View row) {
name=(TextView)row.findViewById(R.id.title);
address=(TextView)row.findViewById(R.id.address);
icon=(ImageView)row.findViewById(R.id.icon);
}
void populateFrom(Cursor r,RestauranteHelper helper) {
name.setText(helper.getNome(r));
address.setText(helper.getEnd(r));
if (helper.getTipo(r).equals("casa")) {
icon.setImageResource(R.drawable.casa_icon);
}
else if (helper.getTipo(r).equals("apartamento")) {
icon.setImageResource(R.drawable.apartamento_icon);
}
else {
icon.setImageResource(R.drawable.comercio_ico);
}
}
}
}
现在我的DeleteItemList:
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);
}
idk如果清楚我要做什么...我想刷新列表,选择“删除”选项。
= d
答案 0 :(得分:0)
更改列表中的项目后,请在列表适配器上调用notifyDatasetChanged。那样做。
以下是ListView的工作方式。
初始化列表后,在列表适配器中设置项目。 现在调用listview.setAdapter方法来设置适配器。
现在,当您对项目进行任何更改时,请在已传递给适配器的列表中更改它们。
然后调用适配器上的notofyDatasetChanged。
那应该有用。如果这不起作用,则其他错误并尝试调试代码的每一步。
**在您的情况下,您在数据库帮助程序上调用delete,但是当您更改数据库中的内容时,您的游标或适配器不会更新。您需要从游标中删除该项或再次查询数据库,然后将其传递给适配器。