English 中文(简体)
更新名单
原标题:Updating listview in Android

我为一些游戏做了一个清单,在删除游戏时,如在清单中消失。 我是想使用适应器的。

    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;
        }

    });
}
最佳回答
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();

更新:

Again before calling adapter.notifyDataSetChanged(); call the below lines::

final ArrayAdapter<String> adapter =  new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, entry.getGameNames());
setListAdapter(adapter);
问题回答

电话

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

再次呼吁

adapter.notifyDataSetChanged();

说明:确保你改变变化阵列,使之明显可见。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签