删除recycleView中的sqlite条目

时间:2015-07-03 08:31:01

标签: java android android-adapter

我在RecycleView中显示我的SQLITE。但我不知道如何从RecycleView中删除单个元组。我试过这样的东西,但当然这不起作用。有什么建议吗?

@Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {

        cal = new GregorianCalendar(stints.get(i).year, stints.get(i).month, stints.get(i).day);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy",Locale.GERMAN);

        personViewHolder.deleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                db.deleteEntry(id-1);
                Log.i("1","you are here");
            }
        });   

        id++;
    }

活性:

public class DB_view extends Activity implements OnClickListener {

    Button back_btn;
    DatabaseHandler db;
    RecyclerView recList;
    RecycleViewAdapter RVAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.db_view);

        // RecyclerView

        recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);

        db = new DatabaseHandler(getApplicationContext());
        db.getAllStintsList();

        RVAdapter = new RecycleViewAdapter(db.getAllStintsList(),db);
        recList.setAdapter(RVAdapter);

        // buttons

        back_btn = (Button) findViewById(R.id.back_btn);
        back_btn.setOnClickListener(this);

        db = new DatabaseHandler(getApplicationContext());

    }

    public void onClick(View v) {

        if (v == findViewById(R.id.back_btn)) {

            startActivity(new Intent(getApplicationContext(),
                    MainActivity.class));
            finish();

        }
    }
}

适配器:

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView stintDate;
        TextView stintDuration;
        TextView stintStartTime;
        TextView stintWage;
        Button deleteButton;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.card_view);

            stintDate = (TextView)itemView.findViewById(R.id.txtDate);
            stintStartTime = (TextView)itemView.findViewById(R.id.txtStartTime);
            stintDuration = (TextView)itemView.findViewById(R.id.txtDuration);
            stintWage = (TextView)itemView.findViewById(R.id.txtWage);
            deleteButton = (Button) itemView.findViewById(R.id.back_delete);
        }
    }

    List<Stint> stints;
    Calendar cal;
    int id =1;
    DatabaseHandler db;

    RecycleViewAdapter(List<Stint> stints, DatabaseHandler db){
        this.stints = stints;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_card_layout, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {

        cal = new GregorianCalendar(stints.get(i).year, stints.get(i).month, stints.get(i).day);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy",Locale.GERMAN);

        personViewHolder.stintDate.setText(id +") " + sdf.format(cal.getTime()));
        personViewHolder.stintStartTime.setText("" + stints.get(i).startTime);
        personViewHolder.stintDuration.setText(stints.get(i).duration + " h");
        personViewHolder.stintWage.setText(String.format("%.2f €",stints.get(i).wage));

        personViewHolder.deleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                db.deleteEntry(id-1);
                Log.i("1","you are here");
            }
        });   

        id++;
    }

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

1 个答案:

答案 0 :(得分:1)

您的onclicklistener仅删除数据库条目,但不更新视图。如果您使用的是游标适配器,请尝试拨打notifyDataSetChanged。否则,您必须重新创建适配器正在使用的数据集,然后调用notifyDataSetChanged