在我的应用中,我有一个待办事项列表。当用户将任务检查为重要时,它的背景变为红色; 它以某种方式工作,但在返回mainActivity后,它会改变随机列表项的颜色!
here一切都很好。
But here任务4未被检查为重要,任务5被检查为重要!
这是我的recyclerViewAdaptor代码:
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
if (!cursor.moveToPosition(position))
return;
final long id = cursor.getLong(cursor.getColumnIndex(DBContracts.Tasks._ID));
time = cursor.getInt(cursor.getColumnIndex(DBContracts.Tasks.T_TIME));
hour = String.valueOf(time / 60);
min = String.valueOf(time % 60);
if (Integer.parseInt(hour) < 10){
hour = "0" + hour;
}
if (Integer.parseInt(min) < 10){
min = "0" + min;
}
//!!!!!!!!!!!!here!!!!!!!!!!!!!
//change color if isImp is true
isImp = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_IS_IMP)));
if (isImp){
card.setBackgroundColor(context.getResources().getColor(R.color.isImpColor));
}else {
card.setBackgroundColor(context.getResources().getColor(R.color.isNotImpColor));
}
holder.name.setText(cursor.getString(cursor.getColumnIndex(DBContracts.Tasks.T_NAME)));
holder.time.setText(hour + ":" + min);
holder.itemView.setTag(id);
holder.isDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vb.vibrate(100);
DBOps.Ops.done(id,"true");
swap(DBOps.Ops.getEmAll(false));
}});
}
以下是添加新任务的代码:
public class AddNewTaskActivity extends AppCompatActivity {
EditText taskName;
TimePicker taskTime;
Switch aSwitch;
boolean isImp = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_task);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
setTitle("New task");
taskName = (EditText) findViewById(R.id.taskName);
taskTime = (TimePicker) findViewById(R.id.timePicker);
aSwitch = (Switch) findViewById(R.id.isImp);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (aSwitch.isChecked()) {
isImp = true;
} else {
isImp = false;
}
}
});
taskTime.setIs24HourView(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_new_task_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.addNew :
addNewTaskToDB();
break;
case android.R.id.home :
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private void addNewTaskToDB() {
if (taskName.length() == 0){
Toast.makeText(this,"Type something as title.",Toast.LENGTH_SHORT).show();
}else {
int time = taskTime.getHour() * 60 + taskTime.getMinute();
DBOps.Ops.addNewTaskOp(
taskName.getText().toString().trim(),
time,
String.valueOf(isImp));
setAlarm();
finish();
}
}
public void setAlarm(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,taskTime.getHour());
calendar.set(Calendar.MINUTE,taskTime.getMinute());
calendar.set(Calendar.SECOND,0);
Intent intent = new Intent(this,NotificationReciever.class);
intent.putExtra("name",taskName.getText());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
答案 0 :(得分:0)
以下是示例代码,在查看代码后,您必须在ViewHolder中定义卡
var initialData =
[{
Year: "2018",
Months: [{
Month: "January",
News: [
{Title: "News 18 January 2018"},
{Title: "News 23 January 2018"}]
},
{Month: "February",
News: [{...}]},
]
},{ Year: "2017", Months: [{...}]
}]
答案 1 :(得分:0)
RecyclerView回收其物品 ...因此,直接更改项目颜色可能会在滚动时更改其他项目的颜色..为实现此目的,您需要应用一个小技巧(通过采用{{ 1}}模型类中的变量。
供参考检查。
How to implement multi-select in RecyclerView?
修改
将您的boolean isChecked
更改为card