根据主要活动类的条件Adapter类的列表视图中插入图像 [英] Insert images in listview of Adapter class according to the condition of Main Activity class

查看:138
本文介绍了根据主要活动类的条件Adapter类的列表视图中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标题可能似乎不清楚你。

My title might seems unclear to you.

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

Due to new in Java and lack of logic I want to ask you simple thing.

我有一个Activity类,那里是培训的有两个文本视图和一个图像视图列表。的OnClick到ListView我想要去到下一个活动在某些条件下。这种情况你可以看到下面的code。

I have a Activity class, where there is list of trainings with two text views and one image view. OnClick to the listView I want to go to the next activity in some conditions. That condition you can see below in code.

Activity类;

Activity class;

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培训这些列表。

To get these lists of training I am using BaseAdapter that extends TrainingAdapter.

我TrainingAdapter类;

My TrainingAdapter class;

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和另一个图像时,训练还没有准备好开始。训练开始条件可以看到蜜蜂在活动课。感谢您的帮助提前..

In imageView (lockImage) I want to set one image to the listView that the training has time to start and another image when the training is not ready to start. The training start condition can bee seen in activity class. Thanks for your help in advance..

推荐答案

确定我设法回答这个方式;
在模型类;

ok I managed to answer this way; In 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;  

在活动课;

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

这篇关于根据主要活动类的条件Adapter类的列表视图中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆