单击按钮时如何删除RecyclerView项目

时间:2019-05-15 10:39:16

标签: android node.js mongodb

我正在制作一个使用NodeJ和MongoDb作为后端服务的应用程序。 当用户单击删除按钮时,我想在MongoDb中删除回收器视图项和与其对应的数据。

这是我到目前为止所做的:

MyPostedBookAdapter.java

public class MyPostedBookAdapter extends RecyclerView.Adapter<MyPostedBookAdapter.ViewHolder> {

List<PostedModel> listItem;
Context context;

public MyPostedBookAdapter(List<PostedModel> listItem, Context context){

    this.listItem = listItem;
    this.context = context;
}

@NonNull
@Override
public MyPostedBookAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.posted_book,viewGroup,false);


    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull MyPostedBookAdapter.ViewHolder viewHolder, final int i) {

    PostedModel model =  listItem.get(i);

    viewHolder.userBookName.setText(model.getPurchaseBookName());

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.drawable.openbook);

    Glide.with(context).load(model.getPurchaseImage()).apply(requestOptions).into(viewHolder.userPostBook);

    viewHolder.delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

           }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView userPostBook;
    TextView userBookName;
    Button delete;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        userPostBook = (itemView).findViewById(R.id.userPostBook);
        userBookName = (itemView).findViewById(R.id.userBookName);
        delete = (itemView).findViewById(R.id.delete);
    }
  }
}

Fragment.java

public class Tab2Fragment extends Fragment {


RecyclerView recyclerView;
ProgressBar progressBar1;
ImageView postImage;
List<PostedModel> listItem;

private static final String URL = "https://www.example.com";

public Tab2Fragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_tab2, container, false);

    postImage = view.findViewById(R.id.postImage);
    progressBar1 = view.findViewById(R.id.progressBar1);

    recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    listItem = new ArrayList<>();

    loadPurchasedBook();

    return view;
}

private void loadPurchasedBook(){

    OkHttpClient client = new OkHttpClient.Builder()
                          .connectTimeout(22, TimeUnit.SECONDS)
                          .readTimeout(22,TimeUnit.SECONDS)
                          .writeTimeout(22,TimeUnit.SECONDS)
                          .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, final Response response) throws IOException {

            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    try {

                        progressBar1.setVisibility(View.GONE);

                        JSONArray array = new JSONArray(response.body().string());

                        if(array.length() == 0){

                            postImage.setVisibility(View.VISIBLE);
                        }

                        for(int i=0;i<array.length();i++){

                            JSONObject object = array.getJSONObject(i);

                            String str1 = object.getString("Book_name");
                            String str2 = object.getString("Book_image");

                            PostedModel model = new PostedModel(str2,str1);

                            listItem.add(model);
                        }

                        MyPostedBookAdapter adapter = new MyPostedBookAdapter(listItem,getActivity());
                        recyclerView.setAdapter(adapter);

                    }catch (JSONException e) {
                        e.printStackTrace();

                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                    finally{

                        response.close();
                    }
                }

            });
        }

        @Override
        public void onFailure(Call call, final IOException e) {

            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    progressBar1.setVisibility(View.GONE);

                    TastyToast.makeText(getActivity(),"Error:"+e.getMessage(),TastyToast.LENGTH_LONG
                            ,TastyToast.ERROR).show();
                }
            });
        }
    });
  }

}

这是我现在要从节点服务器获取数据的方式,我想在单击删除按钮时删除特定的回收站视图项。 有人请让我知道该怎么做。

1 个答案:

答案 0 :(得分:1)

将此代码添加到您的适配器

private void removeAt(int position) {
    list.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, list.size());
}

像这样使用

viewHolder.delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       removeAt(i)//i is your adapter position
    }
});