根据Main Activity类的条件在Adapter类的listview中插入图像

时间:2014-02-14 10:22:23

标签: java android listview android-listview android-adapter

我的头衔似乎不清楚。

由于Java中的新功能和缺乏逻辑,我想问你简单的事情。

我有一个Activity类,其中包含两个文本视图和一个图像视图的培训列表。 OnClick到listView我想在某些条件下转到下一个活动。您可以在下面的代码中看到这种情况。

活动类;

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    currentTraining = (Training) arg0.getAdapter().getItem(arg2);

    SharedMemory.getInstance().setCurrentTraining(currentTraining);

    startTraining();

}

@SuppressLint("SimpleDateFormat")
private void startTraining() throws ParseException {

    // current date & time
    Calendar now = Calendar.getInstance();

    // parse date & time from database
    String trainingStartDate = SharedMemory.getInstance()
            .getCurrentTraining().getDate();

    String trainingStartTime = SharedMemory.getInstance()
            .getCurrentTraining().getStartTime();

    String strDateTime = trainingStartDate + " " + trainingStartTime;

    Calendar training = Calendar.getInstance();

    try {
        training.setTime(new SimpleDateFormat("MM-dd-yyyy HH:mm")
                .parse(strDateTime));
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    // find difference in milliseconds
    long difference = training.getTimeInMillis() - now.getTimeInMillis();

    if (difference < 15 * 60 * 1000) { // less than 15 minutes

        Intent intent = new Intent(getApplicationContext(),
                TraineeListActivity.class);
        MainActivity.this.startActivity(intent);

        finish();

    } else {
        Toast.makeText(
                getApplicationContext(),
                "The training starts on" + " " + trainingStartDate + " at "
                        + trainingStartTime, Toast.LENGTH_SHORT).show();
    }

}

要获得这些培训列表,我使用的是扩展TrainingAdapter的BaseAdapter。

我的TrainingAdapter课程;

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    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();
    }
    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());

    **HERE-->** holder.lockImage.setBackgroundResource(R.drawable.unlock);

    return convertView;
}

static class ViewHolder {

    TextView trainingDetails;
    TextView txtName;
    ImageView lockImage;

}

在imageView(lockImage)中我想将一个图像设置为listView,培训有时间开始,另一个图像是培训未准备好开始的时候。训练开始条件可以在活动课上看到。感谢您的帮助..

3 个答案:

答案 0 :(得分:1)

在您的活动中,您必须检查列表中的所有数据。

假设您的数据在dataArrayList中,遍历dataArrayList并相应地设置图像:

for(int i=0 ; i< data_size;i++)
{
        if(ur condition)
        {
            //set image 1

           obj.setImage(image1);
        }
        else
        {
            // set image 2
            obj.setImage(image2);
        }
    dataArrayList.add(obj);
}

设置完所有数据映像后,将此数据传递给适配器:

TrainingAdapter adapter = new TrainingAdapter(context, dataArrayList) // whtever is ypur argumnets
listView.setAdapter(adapter);

答案 1 :(得分:0)

  

这只适用于listview中您必须传递的一个项目   项目

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    currentTraining = (Training) arg0.getAdapter().getItem(arg2);

    SharedMemory.getInstance().setCurrentTraining(currentTraining);

   startTraining()

}

@SuppressLint("SimpleDateFormat")
private void startTraining() throws ParseException {

    // current date & time
    Calendar now = Calendar.getInstance();

    // parse date & time from database
    String trainingStartDate = SharedMemory.getInstance()
            .getCurrentTraining().getDate();

    String trainingStartTime = SharedMemory.getInstance()
            .getCurrentTraining().getStartTime();

    String strDateTime = trainingStartDate + " " + trainingStartTime;

    Calendar training = Calendar.getInstance();

    try {
        training.setTime(new SimpleDateFormat("MM-dd-yyyy HH:mm")
                .parse(strDateTime));
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    // find difference in milliseconds
    long difference = training.getTimeInMillis() - now.getTimeInMillis();

    if (difference < 15 * 60 * 1000) { // less than 15 minutes
        /**
          training start
        **/       adapter.ViewHolder.status=0;        adapter.notifydatasetchanged();
        Intent intent = new Intent(getApplicationContext(),
                TraineeListActivity.class);
        MainActivity.this.startActivity(intent);

        finish();

    } else {
        Toast.makeText(
                getApplicationContext(),
                "The training starts on" + " " + trainingStartDate + " at "
                        + trainingStartTime, Toast.LENGTH_SHORT).show();      adapter.ViewHolder.status=1;
  adapter.notifydatasetchanged();
      /**
          training remain
        **/
    }

}



   public View getView(int position, View convertView, ViewGroup parent) {
      int tmp_status;  
        ViewHolder holder;
        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=holder.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)
        **HERE-->** holder.lockImage.setBackgroundResource(R.drawable.unlock);
    else
             something
        return convertView;
    }

    static class ViewHolder {

        TextView trainingDetails;
        TextView txtName;
        ImageView lockImage;
        int status;
    }

答案 2 :(得分:0)

好的,我设法以这种方式回答; 在模型类中;

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;  

在活动课程中;

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();

} }

在适配器类中;

// 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);
}