如何解析 android 中的列表?

How to parse list in android?

列表有以下声明

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

如何使用模型 class

打印列表数据

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

下面是适配器 class 的代码,我在其中使用 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());


    }

我用的是 Gson:

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

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

改变你的

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

进入

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

试试这个..

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

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

GitHub 链接 - Gson , Jackson , Moshi