检查列表视图项是否包含指定数据然后开始下一个 activity

Check if listview items contains specified data then start next activity

我有一个 Listview of custom adapter 其中有五个字段。 NameCnicNoDesginationattendance。现在我想做的是我必须检查所有列表项是否都在 attendance column 之后写了一些值。如果 attendance column 上有任何内容,则用户可以移至下一个 activity,否则会提示用户标记出勤。

这是我在 adapter.

中获取值和设置的代码
  for (int p = 0; p < teacherList.size(); p++) {
        details = new DetailsTeacherwebservice();
        //ArrayList<Object> baris = data.get(p);
        details.setId(teacherList.get(p).getId());
        details.setTeachername(teacherList.get(p).getTeachername());
        details.setTeachercnic(teacherList.get(p).getTeachercnic());
        details.setTeacherno(teacherList.get(p).getTeacherno());
        details.setTeachergender(teacherList.get(p).getTeachergender());
        details.setAttendance(teacherList.get(p).getAttendance());

        addas.add(details);
    }
    cusadapter = new CustomAdapterTeacherWebservice(TeacherWebserviceMainList.this, addas);
    listcontent.setAdapter(cusadapter);

这是移动到下一个的代码activity

next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent view_order_intent = new Intent(TeacherList.this, M_TeacherPresenceList.class);
            startActivity(view_order_intent);
            finish();
        }
    });

如果 attendanceemptyNull,则用户无法移动到下一个 activity,因此用户必须为列表中的所有教师标记出勤,然后才能去下一步。 请帮忙

//试试这个

boolean attendance = false;

    for (int p = 0; p < teacherList.size(); p++) {
        details = new DetailsTeacherwebservice();

        if (teacherList.get(p).getAttendance() != null ||
                teacherList.get(p).getAttendance().equal("")) {
            attendance = true;
        } else {
            attendance = false;
        }
    }

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (attendance) {
                Intent view_order_intent = new
                        Intent(TeacherList.this, M_TeacherPresenceList.class);
                startActivity(view_order_intent);
                finish();
            } else {//Toast mark attendance}

            }
        });
    }

AdaptergetView() 方法上使用您的 next.setOnClickListener(new View.OnClickListener()。喜欢 -

private boolean checkAttendance(){
    boolean hasAll = true;

 for (int i = 0; i < teacherList.size(); i++) {
    if(att.empty() || att == null){
        hasAll = false;
        break;
    }
  return hasAll ;
}

getView() 方法上 -

next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          if(checkAttendance()){
            Intent view_order_intent = new Intent(TeacherList.this, M_TeacherPresenceList.class);
            startActivity(view_order_intent);
            finish();
           }else{
             showYourCustomAlert()  // your alert dialog
           }
        }
    });

写一个返回boolean值的方法来检测是否缺考勤,当发现缺考勤时,立即退出循环,确保考勤状态准确。

private boolean hasAllAttendance(){
    boolean attendanceStatus= true;

 for (int p = 0; p < teacherList.size(); p++) {
    details = new DetailsTeacherwebservice();

    if(teacherList.get(p).getAttendance() == null || 
           teacherList.get(p).getAttendance().equal("") ){
        attendanceStatus = false;
        break;
    }
  return attendanceStatus;
}

现在,使用此方法控制您的 activity 转换或 toast 消息

next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    if(hasAllAttendance()){           
        Intent view_order_intent = new 
        Intent(TeacherList.this, M_TeacherPresenceList.class);
        startActivity(view_order_intent);
        finish();
    } else //Toast message

  }
});

试试这个

    ArrayList<Boolean> isAllMarked = new ArrayList<>();
    int attentednceMarkedCount = 0;
    for (int p = 0; p < teacherList.size(); p++) {
        if (!TextUtils.isEmpty(teacherList.get(p).getAttendance())) {
            isAllMarked.add(true);
        } else {
            isAllMarked.add(false);
        }
    }

    for (int i = 0; i < isAllMarked.size(); i++) {
        if (isAllMarked.get(i)) {  //if marked true
            attentednceMarkedCount = attentednceMarkedCount + 1;
        }
    }

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (attentednceMarkedCount ==teacherList.size()) {
                Intent view_order_intent = new Intent(TeacherList.this, M_TeacherPresenceList.class);
                startActivity(view_order_intent);
                finish();
            } else {
                showAlert()
            }

        }
    });