删除RecyclerView项目

时间:2017-07-20 16:31:49

标签: java android

我正在使用Recyclerview在MainActivity中显示项目。这可能是被问到但没有找到任何特定的解决方案。我保存项目的方式是通过序列化现在问题是如何在Recycler视图的每个项目上实现删除功能?我在utils.delete中创建了自己的删除功能但是我得到了NPE。

如果我拨打notifyDataSetChanged();,它只会从视图中删除它而不是保存的文件

请帮忙。

ItemAdapter类:

public class Itemadapter extends RecyclerView.Adapter<Itemadapter.itemviewholder> {
  static ArrayList<Item> items = new ArrayList<Item>();
 Context mcontext;

private String mFilename = utils.EXTRAS_NOTE_FILENAME;


public Itemadapter(MainActivity mainActivity, int item, ArrayList<Item> items)
{
       this.items = items;
}


@Override
public itemviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
    itemviewholder itemviewholder = new itemviewholder(view);
    return itemviewholder;





}

@Override
public void onBindViewHolder(final itemviewholder holder, final int position) {
  Item item = items.get(position);
    holder.postnumber.setText(item.getPost_number());
    holder.itemname.setText(item.getItem_name());







}



@Override
public int getItemCount() {
    return items.size();

  }
public class itemviewholder extends RecyclerView.ViewHolder implements   View.OnClickListener

 {



TextView itemname,postnumber,ico;
private PopupMenu popupMenu;
 ImageButton ico1;

public itemviewholder(View view)
  {
  super(view);


  itemname =  view.findViewById(R.id.itemdetailsdisplay);
  postnumber =  view.findViewById(R.id.itemnuberdisplay);
  ico1 = view.findViewById(R.id.test);
  view.setOnClickListener(this);
  ico1.setOnClickListener(this);



}


@Override
public void onClick(final View v) {
     final Item mLoadeditem = utils.getitembyfilename(v.getContext(),mFilename);

    if (v.getId() == ico1.getId()){
       // Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
               popupMenu = new PopupMenu(v.getContext(),v);
               popupMenu.inflate(R.menu.recycler_item_menu);
               popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                       @Override
                      public boolean onMenuItemClick(MenuItem item) {
                       switch (item.getItemId()){
                           case R.id.item_delete:
            //delete file
                            utils.delete(v.getContext(),mLoadeditem.getPost_number()+utils.FILE_EXTENSION);
                             items.remove(getAdapterPosition());
                                notifyDataSetChanged();
                               Toast.makeText(v.getContext(),"item deleted",Toast.LENGTH_SHORT).show();

                              break;
                           case R.id.item_share:
            //share info
                               Toast.makeText(v.getContext(),"item shared",Toast.LENGTH_SHORT).show();

                       break;
                     }

                          return false;
                     }
                   });popupMenu.show();






        }
    else {
        Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
    }


  }
 }
}

utils类:

public class utils {

public static final String  FILE_EXTENSION = ".bin";
public static final String EXTRAS_NOTE_FILENAME = "EXRAS_NOTES_FILENAME";

public static boolean save(Context context, Item item){
String filename = String.valueOf(item.getPost_number()) +FILE_EXTENSION;

    FileOutputStream fos;
    ObjectOutputStream oos;
    try {
        fos= context.openFileOutput(filename,context.MODE_PRIVATE);
        oos= new ObjectOutputStream(fos);
        oos.writeObject(item);
        oos.close();
        fos.close();

    }catch (Exception e){
      e.printStackTrace();
        return false;
        //something  went wrong
    }


return true;

}
public static ArrayList<Item> getallsaveditems(Context context){
ArrayList <Item> items= new ArrayList<>();
File filesdir = context.getFilesDir();
ArrayList<String> itemsfiles = new ArrayList<>();
for(String file : filesdir.list()){
    if(file.endsWith(FILE_EXTENSION)){
        itemsfiles.add(file);
    }
}

FileInputStream fis;
ObjectInputStream ois;

for(int i=0; i<itemsfiles.size();i++){
    try {
        fis = context.openFileInput(itemsfiles.get(i));
        ois = new ObjectInputStream(fis);
        items.add((Item) ois.readObject());
        fis.close();
        ois.close();
    }catch (IOException | ClassNotFoundException e){
        e.printStackTrace();
        return null;
    }
}
 return items;
 }

public static Item getitembyfilename(Context context,String filename) {
    File file = new File(context.getFilesDir(), filename);
    if (file.exists() && !file.isDirectory()) {
        Log.v("utils", "file exists" + filename);
        FileInputStream fis;
        ObjectInputStream ois;
    try {
        fis = context.openFileInput(filename);
        ois = new ObjectInputStream(fis);
        Item item = (Item) ois.readObject();
        fis.close();
        ois.close();
        return item;
    } catch (IOException |ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    }else {
        return null;




    }

}









public static void delete(Context ctx ,String filename) {
File dir = ctx.getFilesDir();
    File file = new File(dir,filename);
    if(file.exists()){
        file.delete();
    }


}


}

0 个答案:

没有答案