How to delete entry and video file in a listview f

2019-09-19 02:23发布

Problem description: I wanted a "delete" function which could perform delete/remove of the selected entry in a listview and at the same time delete the residing video file string in the Video_List directory then it refresh the content of the listview?

I'm rather new in android/java can someone help me with it? Do scroll down to evaluate the problem i'm facing please!! Can someone tell me what is the specific code i should add into my current codes to perform the above mention function??

4条回答
别忘想泡老子
2楼-- · 2019-09-19 02:39

You defined an override onListItemClick but this code is never been called. You should also register the a listener to the view you're using. Check how android handle user interface events.

newListView.setOnItemClickListener(this);
查看更多
等我变得足够好
3楼-- · 2019-09-19 02:45
@Override // create contextuel menu 
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                super.onCreateContextMenu(menu, v, menuInfo);
                menu.setHeaderTitle("Action");

                menu.add(0,100,1,"delete");

            }

    //////////////////////////////////////////////////
    @Override // Select an item 
        public boolean onContextItemSelected(MenuItem item) {
            final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            switch(item.getItemId()){
            case 100:
    public void onClick(DialogInterface dialog, int id) {
                    db.delete_item(info.id);

                    //here update list view
    }
    });

    ////////////

    public boolean delete_item(long id){ 

    return db.delete("name_table", "_id="+id, null)>0;}
    ////////////////
查看更多
Juvenile、少年°
4楼-- · 2019-09-19 02:52

Do you want to delete the adapter or do you want to delete a row/entry in the list? If latter, then update videoItems and call notifyDataSetChanged on your adapter. If you really want to delete the adapter, then just set it to NULL or have it refer to some other ListAdapter instance and the GC will take care of the rest.

查看更多
在下西门庆
5楼-- · 2019-09-19 02:53

Since you have stored your selection into "item" object then in deleteFile() method you need to retreive the file path from that object, for that to work add the line:

model.absolutePath = mfile.getAbsolutePath();

in getVideoFiles() method 'for' loop.

also before onCreate state:

ListViewAdapter lv;

then in getVideoFiles at the end state:

  lv = new ListViewAdapter(this, R.layout.row, videoItems);
  setListAdapter(lv); 

finally in deleteFile() you need to state:

File myFile = new File(item.absolutePath);

lv.notifyDataSetChanged();

and that should work!

查看更多
登录 后发表回答