gpt4 book ai didi

java - 如何在android中解析列表?

转载 作者:行者123 更新时间:2023-12-01 16:51:14 24 4
gpt4 key购买 nike

列表有以下声明

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

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

模型.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());


}

最佳答案

对我来说,我使用 Gson:

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

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

关于java - 如何在android中解析列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com