当我在recyclerView中拉行时,如何删除我的客户

时间:2018-03-16 19:47:31

标签: android viewmodel instantiation

我有一个RecyclerView,当我拉动该客户的行时,我想从我的数据库和我的UI中删除一个项目。我如何解决这个问题我在我的DAO中有一个@Delete函数,我想使用它看起来像这样

@Delete
void deleteOneCustomer(Customer customer);

从哪里可以获得我想从RecyclerView中删除的客户?我的适配器中有一个缓存的客户,但我不知道如何到达它。这是我的适配器...

适配器:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHolder>{

    class CustomerViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView address;
        private TextView phonenumber;
        private TextView email;


        private CustomerViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.nameTxt);
            address = itemView.findViewById(R.id.addressTxt);
            phonenumber = itemView.findViewById(R.id.addressTxt);
            email = itemView.findViewById(R.id.addressTxt);
        }
    }

    private final LayoutInflater mInflater;
    private List<Customer> mCustomer;

    CustomerAdapter(Context context) { mInflater = LayoutInflater.from(context); }

    @Override
    public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.list_item, parent, false);
        return new CustomerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CustomerViewHolder holder, int position) {
        if (mCustomer != null) {
            Customer current = mCustomer.get(position);
            holder.itemView.setTag(current);
            holder.name.setText(current.getFullName());
            holder.address.setText(current.getAddress());
            holder.phonenumber.setText(current.getAddress());
            holder.email.setText(current.getAddress());

        } else {
            holder.name.setText("Full name");
            holder.address.setText("Address");
            holder.phonenumber.setText("Phonenumber");
            holder.email.setText("Email");
        }

    }

    void setCustomer(List<Customer> customers){
        mCustomer = customers;
        notifyDataSetChanged();
    }


    // getItemCount() is called many times, and when it is first called,
    // mWords has not been updated (means initially, it's null, and we can't return null).
    @Override
    public int getItemCount() {
        if (mCustomer != null)
            return mCustomer.size();
        else return 0;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{




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


        }
    }
}

如何联系此客户以便删除?我还有一个存储库,当你调用它时,它使用后台线程来运行delete函数。这看起来像这样

private static class deleteAsyncTask extends AsyncTask<Customer, Void, Void> {

    private CustomerDao mAsyncTaskDao;

    deleteAsyncTask(CustomerDao dao) {
        mAsyncTaskDao = dao;
    }

    @Override
    protected Void doInBackground(final Customer... params) {
        mAsyncTaskDao.deleteOneCustomer(params[0]);
        return null;
    }
} 

0 个答案:

没有答案