1

我为一些游戏制作了一个列表视图,当一个游戏被删除时,我希望它在列表视图中消失。我正在尝试使用 adapter.notifyDataSetChanged(),但我无法让它工作,你能帮帮我吗?

    public static final String MY_SETTINGS = "MySettings";
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);



    DBHelpeR entry = new DBHelpeR(Loadmenu.this);
    entry.open();
    final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());

    setListAdapter(adapter);
    entry.close();

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setBackgroundResource(R.drawable.background);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    lv.setOnItemLongClickListener(new OnItemLongClickListener(){
        public boolean onItemLongClick(AdapterView<?> parent, final View view,
                int position, long id) {

            String gamename = ((TextView) view).getText().toString();
            builder.setTitle("Slet " + gamename + "?");
            builder.setItems(new CharSequence[]{"Slet"} , new DialogInterface.OnClickListener() {

                // Click listener

                public void onClick(DialogInterface dialog, int item) {
                    DBHelpeR entry = new DBHelpeR(Loadmenu.this);
                    entry.open();
                    int game_id = entry.getGameID(((TextView) view).getText().toString());
                    Log.d("load",Integer.toString(game_id));
                    entry.deleteGame(game_id);
                    entry.close();
                    adapter.notifyDataSetChanged();
                }

            });
            builder.show();
            return true;
        }

    });
}
4

2 回答 2

2
final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());

用下面的 ::: 替换上面的行

ArrayList<String> entries = entry.getGameNames();
final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this,R.layout.list_item,entries);

并且当您删除时,只需再次调用以下两行::

entries = entry.getGameNames();
adapter.notifyDataSetChanged();

更新::

再次调用 adapter.notifyDataSetChanged(); 调用以下行::

final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, entry.getGameNames());
setListAdapter(adapter);
于 2012-04-21T15:00:48.387 回答
0

称呼

final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());
setListAdapter(adapter);

再次调用之前

adapter.notifyDataSetChanged();

注意:确保您已更改数组以使更改可见。

于 2012-04-21T15:12:42.687 回答