大家好我创建了一个类,将文件保存在内部存储中并在ListView上显示。但我的问题是如何删除该商品?这是我的代码

public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String fileName = edFileName.getText().toString();
                String content = edContent.getText().toString();

                FileOutputStream fos;
                try {
                    fos = openFileOutput(fileName, Context.MODE_PRIVATE);
                    fos.write(content.getBytes());
                    fos.close();

                    Toast.makeText(
                            addThis.this,
                            fileName + " saved",
                            Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                ShowSavedFiles();

            }});
}
 void ShowSavedFiles(){
        SavedFiles = getApplicationContext().fileList();
        ArrayAdapter<String> adapter
        = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                SavedFiles);

        listSavedFiles.setAdapter(adapter);
    }

最佳答案

您可能应该在活动中为ListView创建和注册(registerForContextMenu(MyListView))上下文菜单,并在那里执行删除操作。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.menu, menu); //your xml menu
    return true;
}

@Override
 public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
 super.onCreateContextMenu(menu, v, menuInfo);

     menu.add(0, v.getId(), 0, "Delete");
 }

@Override
public boolean onContextItemSelected(MenuItem item) {

       if(item.getTitle().equals("Delete")){
         TextView tv = (TextView)((RelativeLayout) ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView).getChildAt(0);
      //tv.getText(); will probably hold your filename
      //and just use the deleteFile() operations to remove it from the internal storage
       .............
       }




阅读有关如何使用内部存储的内容
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

希望对您有所帮助

07-28 02:03