Ankur Khandelwal Ankur Khandelwal - 2 months ago 7
Java Question

How to parse list in android?

List have below declaration

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


Sample data:-

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


How do I print data of list using model 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

Below is the code for adaptor class in which I am using recyclerView to list down the data.

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


Database Fetch Code:

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


Code of the calling 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());


}

Answer

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

into

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

Do it like this

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