【问题标题】:How to parse list in android?如何在android中解析列表?
【发布时间】:2016-09-27 04:33:42
【问题描述】:

列表有以下声明

private ArrayList<HashMap<String,String>> result=new ArrayList<HashMap<String, String>>();

样本数据:-

[{Minutes=25, Catagory=Morning, Hour=11, medName=hdodu, ImgPath=/storage/emulated/0/H/hdodu.jpg},
 {Minutes=25, Catagory=Night, Hour=3, medName=hdodu, ImgPath=/storage/emulated/0/H/hdodu.jpg}, 
 {Minutes=33, Catagory=Afternoon, Hour=16, medName=jsindj, ImgPath=/storage/emulated/0/H/jsindj.jpg}]

如何使用模型类打印列表数据

Model.java

public class ListMedicine  {

private String Medicine_name;
private String Reminder_hour;
private String Reminder_min;
private String Reminder_catagory;
private String Medicine_image_path;


public void setMedicine_name(String medicine_name)
{
 this.Medicine_name=medicine_name;
}

public String getMedicine_name()
{
    return Medicine_name;
}

public void setReminder_hour(String reminder_hour)
{
    this.Reminder_hour=reminder_hour;
}

public String getReminder_hour()
{
    return Reminder_hour;
}

public void setReminder_min(String reminder_min)
{
    this.Reminder_min=reminder_min;
}

public String getReminder_min()
{
    return Reminder_min;
}

public void setReminder_catagory(String reminder_catagory)
{
    this.Reminder_catagory=reminder_catagory;
}

public String getReminder_catagory()
{
    return Reminder_catagory;
}

public void setMedicine_image_path(String medicine_image_path)
{
    this.Medicine_image_path=medicine_image_path;
}

public String getMedicine_image_path()
{
    return Medicine_image_path;
}

}

MedicineAdaptor.java

下面是适配器类的代码,我在其中使用 recyclerView 列出数据。

public class MedicineAdaptor extends RecyclerView.Adapter<MedicineAdaptor.ViewHolder> {

    List<ListMedicine> items;
    DbHelper dbHelper;
    Context context;

    public MedicineAdaptor(Context context, List<ListMedicine> items)
    {
        super();
        dbHelper=new DbHelper(context);
        Log.i("In the MedicineAdaptor","Constructor");
        this.context=context;
        this.items=items;

    }



    @Override
    public MedicineAdaptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Log.i("Entering","onCreateViewHolder");

        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.medicine,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MedicineAdaptor.ViewHolder holder, int position) {

        Log.i("Entering","onBindViewHolder");

        ListMedicine listMedicine=items.get(position);

        Log.i("Medicine Name", listMedicine.getMedicine_name());
        Log.i("Medicine Hour", listMedicine.getReminder_hour());
        Log.i("Medicine Min",listMedicine.getReminder_min());
        Log.i("Medicine Catagory", listMedicine.getReminder_catagory());

        holder.MedicineName.setText(listMedicine.getMedicine_name());
        holder.ReminderHour.setText(listMedicine.getReminder_hour());
        holder.ReminderMin.setText(listMedicine.getReminder_min());
        holder.ReminderCatagory.setText(listMedicine.getReminder_catagory());

    }

    @Override
    public int getItemCount() {
        Log.i("Size is", Integer.toString(items.size()));
        return items.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView MedicineName, ReminderHour,ReminderMin, ReminderCatagory;
        public ImageView MedicineThumbnail;


        public ViewHolder(View itemView) {
            super(itemView);

            MedicineName=(TextView) itemView.findViewById(R.id.medicine_name);
            ReminderHour=(TextView) itemView.findViewById(R.id.reminder_hour);
            ReminderMin=(TextView) itemView.findViewById(R.id.reminder_hour);
            ReminderCatagory=(TextView) itemView.findViewById(R.id.medicine_catagory);
            MedicineThumbnail=(ImageView) itemView.findViewById(R.id.medicine_icon);
        }
    }
}

数据库获取代码:

public ArrayList<ListMedicine> fetchReminder(String date)
    {

        ArrayList<ListMedicine> array_list=new ArrayList<ListMedicine>();

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery("select * from reminders",null);
        c.moveToFirst();

        try {

            while(c.isAfterLast() == false){
                Log.i("Inside the fetch query","yes");

                ListMedicine listMedicine=new ListMedicine();

                listMedicine.setMedicine_name(c.getString(c.getColumnIndex("medName")));

                array_list.add(listMedicine);

                c.moveToNext();
            }



        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return array_list;
    }

调用Activity的代码

private void showData() {
        ArrayList<ListMedicine> list=new ArrayList<ListMedicine>();

        Log.i("Show Data function","Yes");


        try {
            JSONArray arr = new JSONArray(result);

            Log.i("Length is ",Integer.toString(arr.length()));

            for(int i=0;i<arr.length(); i++){

                ListMedicine bean = new ListMedicine();

                bean.setMedicine_name(bean.getMedicine_name());



                // at last add this into your list
                Log.i("Bean ans", bean.toString());
                list.add(bean);

            }


        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        adapter=new MedicineAdaptor(this,list);
        reminderList.setAdapter(adapter);


    }

    private void getData(String fetchDate) {

        //ArrayList<String> result=new ArrayList<String>();

        result=db.fetchReminder(fetchDate);

        Log.i("Result",result.toString());


    }

【问题讨论】:

  • How to parse JSON in Android 的可能副本,尽管坦率地说它可能是关于任何事情...
  • 你可以参考这个答案来回答你的问题List example
  • 从您的问题看来您已经知道如何打印数据...
  • 我认为您的示例数据 JSON 格式不正确。
  • 样本数据是正确的,但我需要更改我的模型类。我不知道需要改变什么。 @RoShanShan

标签: java android model adaptor


【解决方案1】:

对我来说,我正在使用 Gson:

YourModel obj = gson.fromJson(yourStringJson, YourModel.class);   

您可以在此处阅读有关 Gson 的更多信息: https://sites.google.com/site/gson/gson-user-guide

【讨论】:

    【解决方案2】:

    或许,您可以使用 Gson、Jackson、Moshi 等... 他们可能正在解析 Java 模型。

    GitHub 链接 - GsonJacksonMoshi

    【讨论】:

      【解决方案3】:

      更改您的

      ArrayList&lt;HashMap&lt;String,String&gt;&gt; result=new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

      进入

      ArrayList<ListMedicine> result=new ArrayList<ListMedicine>();
      

      这样做

      try {
           JSONArray arr = new JSONArray(result);
           for(int i=0;i<arr.length(); i++){
              JSONObject obj = arr.getJSONObject(i);
              ListMedicine bean = new ListMedicine();
              bean.setMedicine_name(obj.getString("medName"));
              .
              .
              .
             // at last add this into your list
             result.add(bean);
           }
      
      
      }catch (Exception e) {
           // TODO Auto-generated catch block
          e.printStackTrace();
       }
      

      【讨论】:

      • Result arraylist 是 hashmap 类型而不是 String 类型。我需要更改我的模型类但不知道需要更改什么
      • 所以你希望键和值进入 hashMap 列表?
      • 我想将给定的数据解析为单个数组列表,以便使用模型类我可以在回收视图中列出每一行
      • 那么为什么你需要一个数组列表,你可以直接解析它。数组列表用于多个对象而不是单个对象
      • 那我该如何解析呢。
      【解决方案4】:

      试试这个..

      try {
      
               for(int i=0;i<list.size(); i++){        
          ListMedicine bean = new ListMedicine();
          bean.setMedicine_name(list.get(i).getString("medName"));
          .
          .
          .
          // at last add this into your list
          result.add(bean);
               }
      
      
          }catch (Exception e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
           }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 2016-10-15
        • 2019-06-03
        • 2013-11-29
        • 1970-01-01
        • 2021-09-06
        相关资源
        最近更新 更多