Java 如何在android中解析列表?

Java 如何在android中解析列表?,java,android,model,adaptor,Java,Android,Model,Adaptor,名单上有以下声明 private ArrayList<HashMap<String,String>> result=new ArrayList<HashMap<String, String>>(); 如何使用模型类打印列表数据 Model.java MedicineAdapter.java 下面是适配器类的代码,我在其中使用recyclerView列出数据 public class MedicineAdaptor extends Recycler

名单上有以下声明

private ArrayList<HashMap<String,String>> result=new ArrayList<HashMap<String, String>>();
如何使用模型类打印列表数据

Model.java

MedicineAdapter.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;
    }
调用活动的代码

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的更多信息:

更改您的 ArrayList结果=新建ArrayList

进入

像这样做

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

也许,你可以用格森,杰克逊,莫西等。。。 他们可能正在解析Java模型


GitHub链接-,

可能重复,尽管坦率地说,它可能是关于任何内容的…您可以从您的问题中参考此答案。看起来您已经知道如何打印数据…我认为您的示例数据格式不正确JSON。示例数据正确,但我需要更改我的模型类。我不知道需要改变什么@RoShanShanResult arraylist是hashmap类型而不是字符串类型。。我需要更改我的模型类,但不知道需要更改什么,所以您希望将键和值转换为hashMap列表?我希望将给定的数据解析为单个数组列表,以便使用模型类可以在recycle View中列出每一行。所以,为什么您需要单个数组列表,您可以直接解析它。数组列表用于多个对象,而不是单个对象,所以我将如何解析它。
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();
     }