它可以是几天前我问过的同一个问题的副本: this。我在那里收到一个答案,几乎满足了我的要求但并非完全符合我我再次发布这个是因为我想引起你的注意。对不起。 在Activity类中。
try {
training.setTime(new SimpleDateFormat("MM-dd-yyyy HH:mm")
.parse(strDateTime));
long difference = training.getTimeInMillis()
- now.getTimeInMillis();
if (difference < 15 * 60 * 1000) { // less than 15 minutes
TrainingAdapter.ViewHolder.status = 1;
trainingAdapter.notifyDataSetChanged();
Intent intent = new Intent(getApplicationContext(),
TraineeListActivity.class);
MainActivity.this.startActivity(intent);
finish();
} else {
TrainingAdapter.ViewHolder.status = 1;
Toast.makeText(
getApplicationContext(),
"The training starts on" + " " + trainingStartDate
+ " at " + trainingStartTime,
Toast.LENGTH_SHORT).show();
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
在Adapter类中;
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int tmp_status = 0;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.trainingadapter, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.training_title);
holder.trainingDetails = (TextView) convertView
.findViewById(R.id.training_dtls);
holder.lockImage = (ImageView) convertView
.findViewById(R.id.lockImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
tmp_status = ViewHolder.status;
}
holder.txtName.setText(trainingsList.get(position).getTitle());
holder.trainingDetails.setText("In "
+ trainingsList.get(position).getLocation() + " on "
+ trainingsList.get(position).getDate() + " at "
+ trainingsList.get(position).getStartTime() + " - "
+ trainingsList.get(position).getEndTime() + " by "
+ trainingsList.get(position).getTrainer());
if (tmp_status == 0) {
System.out.println(tmp_status);
holder.lockImage.setBackgroundResource(R.drawable.unlock);
} else if (tmp_status == 1) {
System.out.println(tmp_status);
holder.lockImage.setBackgroundResource(R.drawable.lock);
}
return convertView;
}
static class ViewHolder {
TextView trainingDetails;
TextView txtName;
ImageView lockImage;
public static int status;
}
此处,图像根据 int tmp_status = 0; 的值而变化。如果我将值初始化为1,则只有它给出了另一个图像,但是给了所有列表视图。在Activity类中,您可以看到每个列表的条件不同,即对另一个活动的意图。你能弄明白问题在哪里吗?
答案 0 :(得分:0)
从ViewHolder中删除状态变量。 ViewHolder仅适用于视图。 你必须为每一行保持状态。它可以是整数列表或Pojo列表。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int tmp_status = 0;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.trainingadapter, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.training_title);
holder.trainingDetails = (TextView) convertView
.findViewById(R.id.training_dtls);
holder.lockImage = (ImageView) convertView
.findViewById(R.id.lockImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
tmp_status = listOfStatuses.get(position);
holder.txtName.setText(trainingsList.get(position).getTitle());
holder.trainingDetails.setText("In "
+ trainingsList.get(position).getLocation() + " on "
+ trainingsList.get(position).getDate() + " at "
+ trainingsList.get(position).getStartTime() + " - "
+ trainingsList.get(position).getEndTime() + " by "
+ trainingsList.get(position).getTrainer());
if (tmp_status == 0) {
System.out.println(tmp_status);
holder.lockImage.setBackgroundResource(R.drawable.unlock);
} else if (tmp_status == 1) {
System.out.println(tmp_status);
holder.lockImage.setBackgroundResource(R.drawable.lock);
}
return convertView;
}
static class ViewHolder {
TextView trainingDetails;
TextView txtName;
ImageView lockImage;
}
答案 1 :(得分:0)
好的,我设法以这种方式做到了;
Im model class;
public boolean isLocked() {
boolean returnCode = true;
// current date & time
Calendar now = Calendar.getInstance();
// parse date & time from database
String trainingStartDate = this.getDate();
String trainingStartTime = this.getStartTime();
String strDateTime = trainingStartDate + " " + trainingStartTime;
Calendar training = Calendar.getInstance();
try {
training.setTime(new SimpleDateFormat("MM-dd-yyyy HH:mm")
.parse(strDateTime));
long difference = training.getTimeInMillis()
- now.getTimeInMillis();
if (difference < 15 * 60 * 1000) { // less than 15 minutes
returnCode = false;
}
} catch (java.text.ParseException e) {
e.printStackTrace();
//returnCode = false;
}
return returnCode;
}
在Activity类中;
private void startTraining() {
// parse date & time from database
String trainingStartDate = SharedMemory.getInstance()
.getCurrentTraining().getDate();
String trainingStartTime = SharedMemory.getInstance()
.getCurrentTraining().getStartTime();
if ((currentTraining).isLocked()){
Toast.makeText(
getApplicationContext(),
"The training starts on" + " " + trainingStartDate
+ " at " + trainingStartTime,
Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent(getApplicationContext(),
TraineeListActivity.class);
MainActivity.this.startActivity(intent);
finish();
}
}
在Adapter类中;
// If training is locked, then display the lock picture
if (trainingsList.get(position).isLocked()) {
holder.lockImage.setBackgroundResource(R.drawable.lock);
// else display the unlock picture
} else {
holder.lockImage.setBackgroundResource(R.drawable.unlock);
}