首页 > 解决方案 > 将两个 JSONArray 与整数值进行比较,如果值相同,则更改该 texview 颜色

问题描述

我的模型类中有两个数组列表,它们包含整数值。我在我的适配器中获取两个数组并比较值。我想要这个,当第一个数组的任何值与第二个数组匹配时,然后在列表中特定的列表行项目文本视图应该为红色。Arraylist 的大小可以增加和减少。

下面我提供代码。请查看并提供解决方案。

public ArrayList<GetDoctorScheduleDetail> getDoctorScheduleDetails;// parent model class which is containing both bottom arraylist
    public ArrayList<GetBlockedTimings> getBlockedTimingses = new ArrayList<>();
    public ArrayList<AvailableTimeSlots> availableTimeSlotses = new ArrayList<>();

     for (int i=0;i<getDoctorScheduleDetails.size();i++)
        {

            all_time_id = pojo.getAvailableTimeSlots().get(position).getTimeSlotId();
            compare.add(all_time_id);
            for (int j=0;j<getBlockedTimingses.size();j++) {
                all_block_id = pojo.getGetBlockedTimings().get(position).getFkTimeId();
                compare_fk.add(all_block_id);
                if (all_time_id==all_block_id)
                {
                   /* if (all_time_id==all_block_id)
                    {*/
                    Toast.makeText(context,"Hi It's matched"+i +""+j,Toast.LENGTH_SHORT).show();
                    Log.e("searching...1",+i+" "+j);
                }
                else  if (all_time_id!=all_block_id)  {
                    Toast.makeText(context,"No matched"+i +""+j,Toast.LENGTH_SHORT).show();
                    Log.e("searching...1",+i+" "+j);
                }
               /* Log.e("found_first_index: ", i + " second_index: " + j);
                Log.e("searching... ",+i+" "+j);*/
            }

        }

第二个 JSONarray 值不在模型类中

public class MyScheduleJSONParser {
    public static final String TAG = "MyScheduleJSONParser";

    static ArrayList<GetDoctorScheduleDetail> doctorScheduleDetailList; // parent model array object
    static List<AvailableTimeSlots> availableTimeSlotses;// child model array to save 1st json array values object
    static List<GetBlockedTimings> getBlockedTimingses;// child model array to save 2nd json array values object

    public static ArrayList<GetDoctorScheduleDetail> parseData(String content, int index) {
        try {
            JSONObject jsonObject = new JSONObject(content);
            JSONArray details_jsonArray = jsonObject.getJSONArray("Detail");

            doctorScheduleDetailList = new ArrayList<>();
            availableTimeSlotses = new ArrayList<>();
            getBlockedTimingses = new ArrayList<>();

            JSONObject detailObject = details_jsonArray.getJSONObject(index);
           // for (int i = 0; i <= detailObject.length(); i++) {
                GetDoctorScheduleDetail slot = new GetDoctorScheduleDetail();//parent model class in this I'm storing other parameters rather then 2 jsonarry(AvailableTimeSlots,GetBlockedTimings)
                slot.setDateOfSlot(detailObject.getString("DateOfSlot"));
                slot.setScheduleId(detailObject.getInt("ScheduleId"));
                slot.setBlockId(detailObject.getInt("BlockId"));
                slot.setFkTimeId(detailObject.getInt("fkTimeId"));
                slot.setDeleted(detailObject.getBoolean("IsDeleted"));
                slot.setFkScheduledId(detailObject.getInt("fkScheduledId"));
                slot.setUtcDateOfSlot(detailObject.getString("utcDateOfSlot"));


                /* this is to fetch AvailableTimeSlots data*/
                JSONArray availableTimeSlots = detailObject.getJSONArray("AvailableTimeSlots");

                /* this loop for fetch AvailableTimeSlots data*/
                for (int j = 0; j < availableTimeSlots.length(); j++) {
                    JSONObject innerDataObject = availableTimeSlots.getJSONObject(j);

                    AvailableTimeSlots timeSlots = new AvailableTimeSlots();//child model to save available array data
                    timeSlots.setTimeOfSlot(innerDataObject.getString("TimeOfSlot"));
                    timeSlots.setTimeSlotId(innerDataObject.getInt("TimeSlotId"));
                    timeSlots.setTimeofSlotDateTime(innerDataObject.getString("TimeofSlotDateTime"));
                    availableTimeSlotses.add(timeSlots);// adding data in child model list
                    slot.setAvailableTimeSlots(availableTimeSlotses);// adding data in parent model   public void setAvailableTimeSlots(List<AvailableTimeSlots> availableTimeSlots){this.availableTimeSlots = availableTimeSlots;}
                    doctorScheduleDetailList.add(slot);// adding in parent list
                }
                /* this loop to fetch GetBlockedTimings data*/

                if ((detailObject.getJSONArray("GetBlockedTimings")) != null) {
                    JSONArray getBlockedTimings = detailObject.getJSONArray("GetBlockedTimings");
                    /* this loop for fetch GetBlockedTimings data*/
                    for (int k = 0; k < getBlockedTimings.length(); k++) {
                        JSONObject getBlockedTimingsJSONObject = getBlockedTimings.getJSONObject(k);
                        GetBlockedTimings blockedTimings = new GetBlockedTimings();
                        blockedTimings.setFkTimeId(getBlockedTimingsJSONObject.getInt("fkTimeId"));

                        getBlockedTimingses.add(blockedTimings);// adding data in child model list
                        slot.setGetBlockedTimings(getBlockedTimingses);//adding data in parent model public void setGetBlockedTimings(List<GetBlockedTimings> getBlockedTimings){this.getBlockedTimings = getBlockedTimings;}
                        doctorScheduleDetailList.add(slot);// adding in parent list
                    }
                    //doctorScheduleDetailList.add(slot);
                    return doctorScheduleDetailList;
                }
          //  }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return doctorScheduleDetailList;
    }
}

标签: javaandroidandroid-studio

解决方案


据我了解你的问题,像这样改变

  all_time_id = pojo.getAvailableTimeSlots().get(position).getTimeSlotId();
  compare.add(all_time_id);
                 all_block_id = pojo.getGetBlockedTimings().get(position).getFkTimeId();
compare_fk.add(all_block_id);
            for (int j=0;j<compare_fk.size();j++) {
                if(all_time_id = compare_fk.get(j)){
                  ...........
                else{
                  ...........
                 }

推荐阅读