我创建了时间表列表。我已经使用了卡片视图和回收者视图。现在我已经为卡片视图设置了id以将其与数据库记录链接。
但是卡的ID没有正确设置。如果我尝试删除记录。有些人正在删除,有些人不会被删除。
TableListAdapter:
public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {
public static TimeTableHelper db;
public static TimeTableList timeTableList;
public static int cardId,id;
public static boolean editMode;
private List<TimeTable> tableList;
public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
this.tableList = tableList;
this.timeTableList = timeTableList;
}
private Context context;
public TableListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return tableList.size();
}
@Override
public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {
TimeTable ci = tableList.get(i);
contactViewHolder.tableTitle.setText(ci.getTitle());
contactViewHolder.color.setBackgroundColor(ci.getTableColor());
}
@Override
public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
cardId++;
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.table_card, viewGroup, false);
return new TableViewHolder(itemView);
}
public static class TableViewHolder extends RecyclerView.ViewHolder {
protected TextView tableTitle;
protected CardView cv;
protected SwitchCompat aSwitch;
protected Button color;
private int status =0;
public TableViewHolder(View v) {
super(v);
tableTitle = (TextView) v.findViewById(R.id.tableTitle);
cv = (CardView) v.findViewById(R.id.card_view);
aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
color = (Button) v.findViewById(R.id.selectColor);
db = new TimeTableHelper(timeTableList);
cv.setId(cardId);
Log.d("cardId", String.valueOf(cardId));
cv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);
builder.setTitle("Delete entry")
.setMessage("Are you sure you want to Delete this Time Table?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
id = cv.getId();
TimeTable t = new TimeTable();
t = db.getTable(id);
db.deleteTable(t);
Intent i = new Intent(timeTableList,TimeTableList.class);
timeTableList.startActivity(i);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_warning_black_36dp)
.show();
return true;
}
});
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
id = cv.getId();
editMode = true;
Intent i = new Intent(timeTableList, NewTimeTable.class);
i.putExtra("editMode", editMode);
i.putExtra("tableId", id);
timeTableList.startActivity(i);
}
});
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
id = cv.getId();
if (isChecked) {
status = 1;
TimeTable t = new TimeTable();
t = db.getTable(id);
t.setStatus(status);
db.updateStatus(t);
Log.d("status", String.valueOf(status));
final List<TimeTable> events = db.getAllTables();
for (TimeTable cn : events) {
String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
"Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
Log.d("Data ", log);
}
} else {
status = 0;
TimeTable t = new TimeTable();
t = db.getTable(id);
t.setStatus(status);
db.updateStatus(t);
final List<TimeTable> events = db.getAllTables();
for (TimeTable cn : events) {
String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
"Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
Log.d("Data ", log);
}
Log.d("status", String.valueOf(status));
}
}
});
}
}
public void updateAdapaterList(List<TimeTable> newTimeTableList) {
//Replace the current list with new list
this.tableList = newTimeTableList;
//notify the adapter that the data set has changed
notifyDataSetChanged();
}
}
出了什么问题?
答案 0 :(得分:0)
为卡片视图设置标记,并在有人点击时获取标记。尝试做这样的事情
public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {
public static TimeTableHelper db;
public static TimeTableList timeTableList;
public static int cardId,id;
public static boolean editMode;
private List<TimeTable> tableList;
public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
this.tableList = tableList;
this.timeTableList = timeTableList;
}
private Context context;
public TableListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return tableList.size();
}
@Override
public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {
contactViewHolder.cv.setTag(i);
TimeTable ci = tableList.get(i);
contactViewHolder.tableTitle.setText(ci.getTitle());
contactViewHolder.color.setBackgroundColor(ci.getTableColor());
}
@Override
public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
cardId++;
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.table_card, viewGroup, false);
return new TableViewHolder(itemView);
}
public static class TableViewHolder extends RecyclerView.ViewHolder {
protected TextView tableTitle;
protected CardView cv;
protected SwitchCompat aSwitch;
protected Button color;
private int status =0;
public TableViewHolder(View v) {
super(v);
tableTitle = (TextView) v.findViewById(R.id.tableTitle);
cv = (CardView) v.findViewById(R.id.card_view);
aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
color = (Button) v.findViewById(R.id.selectColor);
db = new TimeTableHelper(timeTableList);
cv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
id = (int) v.getTag();
final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);
builder.setTitle("Delete entry")
.setMessage("Are you sure you want to Delete this Time Table?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TimeTable t = new TimeTable();
t = db.getTable(id);
db.deleteTable(t);
Intent i = new Intent(timeTableList,TimeTableList.class);
timeTableList.startActivity(i);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_warning_black_36dp)
.show();
return true;
}
});
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
id = (int) v.getTag();
editMode = true;
Intent i = new Intent(timeTableList, NewTimeTable.class);
i.putExtra("editMode", editMode);
i.putExtra("tableId", id);
timeTableList.startActivity(i);
}
});
//And your other stuff
答案 1 :(得分:0)
我没有使用CardView,而是使用了全部视图来点击监听器,您可以使用cv尝试它,但首先检查视图。
重新安排了您的代码 -
public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> {
public static TimeTableHelper db;
public static TimeTableList timeTableList;
public static int cardId,id;
public static boolean editMode;
private List<TimeTable> tableList;
public TableListAdapter(List<TimeTable> tableList,TimeTableList timeTableList) {
this.tableList = tableList;
this.timeTableList = timeTableList;
}
private Context context;
public TableListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return tableList.size();
}
@Override
public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
cardId++;
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.table_card, viewGroup, false);
return new TableViewHolder(itemView);
}
@Override
public void onBindViewHolder(TableViewHolder contactViewHolder, int i) {
TimeTable ci = tableList.get(i);
contactViewHolder.tableTitle.setText(ci.getTitle());
contactViewHolder.color.setBackgroundColor(ci.getTableColor());
contactViewHolder.view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);
builder.setTitle("Delete entry")
.setMessage("Are you sure you want to Delete this Time Table?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
id = cv.getId();
TimeTable t = new TimeTable();
t = db.getTable(id);
db.deleteTable(t);
notifyDataSetChanged();
Intent i = new Intent(timeTableList,TimeTableList.class);
timeTableList.startActivity(i);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_warning_black_36dp)
.show();
return true;
}
});
contactViewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
id = cv.getId();
editMode = true;
Intent i = new Intent(timeTableList, NewTimeTable.class);
i.putExtra("editMode", editMode);
i.putExtra("tableId", id);
timeTableList.startActivity(i);
}
});
contactViewHolder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
id = cv.getId();
if (isChecked) {
status = 1;
TimeTable t = new TimeTable();
t = db.getTable(id);
t.setStatus(status);
db.updateStatus(t);
Log.d("status", String.valueOf(status));
final List<TimeTable> events = db.getAllTables();
for (TimeTable cn : events) {
String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
"Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
Log.d("Data ", log);
}
} else {
status = 0;
TimeTable t = new TimeTable();
t = db.getTable(id);
t.setStatus(status);
db.updateStatus(t);
final List<TimeTable> events = db.getAllTables();
for (TimeTable cn : events) {
String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
"Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
Log.d("Data ", log);
}
Log.d("status", String.valueOf(status));
}
}
});
contactViewHolder.view.
}
}
public static class TableViewHolder extends RecyclerView.ViewHolder {
protected TextView tableTitle;
protected CardView cv;
protected SwitchCompat aSwitch;
protected Button color;
private int status =0;
private View view;
public TableViewHolder(View v) {
super(v);
tableTitle = (TextView) v.findViewById(R.id.tableTitle);
cv = (CardView) v.findViewById(R.id.card_view);
aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
color = (Button) v.findViewById(R.id.selectColor);
view = v;
db = new TimeTableHelper(timeTableList);
cv.setId(cardId);
}
}