Insert images in listview of Adapter class according to the condition of Main Activity class

一笑奈何 提交于 2019-12-25 07:29:04

问题


My title might seems unclear to you.

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

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

}

To get these lists of training I am using BaseAdapter that extends 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;

}

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..


回答1:


In your activity, you have to check for all data in the list.

Suppose your data is in dataArrayList, iterate through dataArrayList and set image accordingly :

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

After all data image is set ,pass this data to adapter :

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



回答2:


this will work only for one item in listview you have to pass selected item

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



回答3:


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;  

In activity class;

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

} }

In adapter class;

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


来源:https://stackoverflow.com/questions/21776669/insert-images-in-listview-of-adapter-class-according-to-the-condition-of-main-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!