删除 recycleView 中的 sqlite 条目
delete sqlite entry within recycleView
我在 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++;
}
activity:
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();
}
}
您的 onclicklistener
只删除数据库条目,但不更新视图。如果您使用的是游标适配器,请尝试调用 notifyDataSetChanged
。否则你必须重新创建你的适配器正在使用的数据集,然后调用 notifyDataSetChanged
.
我在 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++;
}
activity:
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();
}
}
您的 onclicklistener
只删除数据库条目,但不更新视图。如果您使用的是游标适配器,请尝试调用 notifyDataSetChanged
。否则你必须重新创建你的适配器正在使用的数据集,然后调用 notifyDataSetChanged
.