无法获取Listview来刷新或删除项目

时间:2013-08-22 08:17:08

标签: java android listview view android-listview

以下是代码,我认为难以解决,但我已经尽力了!:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                   R.layout.row, filenames);

            listView1.setAdapter(adapter);

            listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, final View view,
                                        int position, long id) {

                    final String item = (String) parent.getItemAtPosition(position);



                    final File a = new File(extStorageDirectory
                            + "/Android/data/com.tobin.swag/backups/" + item);

                    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

                    v.vibrate(30);

                    AlertDialog.Builder alertbox = new AlertDialog.Builder(Backups.this);

                    alertbox.setTitle("Pick a new task for " + item);
                    alertbox.setCancelable(true);
                    alertbox.setItems(array, new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface dialog, int which){

                            if (which == 0) {
                                Toast.makeText(getApplicationContext(), "Uploading to drive is not done, !!>?", Toast.LENGTH_SHORT).show();
                            }

                            if (which == 1) {
                                Toast.makeText(getApplicationContext(), "Copying to there is not done yet, FAR OUT !!>?", Toast.LENGTH_SHORT).show();


                            }

                            if (which == 2) {
                             a.delete();
                                adapter.notifyDataSetChanged();
                                listView1.invalidate();
                                listView1.refreshDrawableState();


                            }

我要删除列表视图中单击的项目,或者只是在文件被删除时刷新列表视图!

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

尝试在notifyDataChange之后调用invalidate方法,如下所示:

listView1.invalidateViews();

答案 1 :(得分:1)

使用Yourlistview.invalidateViews()。我希望它会有所帮助:)。