Android Sorting data into arrays - ArrayAdapter JSON Listview

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 01:05:41

问题


I have a MySQL table called foods which contain diet information. I am currently running the php script which calls the following sql command Select * from Food where dietid = 1 which returns the image below.

The column day is the day of the diet so day 1, day 2 etc.. the time refers to a point during the day so time 1 is breakfast time 2 is snack time 3 is lunch time 4 is afternoon snack and time 5 is dinner.

I would like to display the data in the following way in a listview for each day of the diet.Description is the made out of qty measure and item

I have the following output in the log-cat at the moment.

UPDATE - EDIT NEW LOGCAT

Using Jims approact i have been able to group this data by day and time in the format that i require however i am now facing the problem which i was facing before, the list gets inflated and it puts the information in each row when i need it all to be in 1 row per 1 day.

The problem is that now it inflates the views like this which creates one view and puts it in for each item.

UPDATED THE ARRAYADAPTER#

I would like to group this data in arrays for each day, so it would be day 1 - breakfast - item, snack - item etc... so that i can put it all in one list for the user to view.

All my data is stored in 2 model classes. Below is my adapter with the model classes.

Thanks :) getting there..

ArrayAdapter Class UPDATED

public class DietAdapterNew extends ArrayAdapter<FoodInfoModel>{

private List<FoodInfoModel> items;



private TextView diet_day, breakfast_data, snach1_data, lunch_data, snack2_data, dinner_data ;


private LinearLayout ratingCntr;
private TextView followersText;



public DietAdapterNew(Context context, List<FoodInfoModel> items) {
    super(context, R.layout.list_diet_single_day);


    this.items = items;

}


public int getCount() {
    return items.size();
}


@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.list_diet_single_day, null);            
    }

    FoodInfoModel infomodel = items.get(position);


    if(infomodel != null) {



        diet_day = (TextView)v.findViewById(R.id.diet_day);
        breakfast_data = (TextView)v.findViewById(R.id.breakfast_data);
        snach1_data  = (TextView)v.findViewById(R.id.snack1_data);
        lunch_data = (TextView)v.findViewById(R.id.lunch_data);
        snack2_data = (TextView)v.findViewById(R.id.snack2_data);
        dinner_data =  (TextView)v.findViewById(R.id.dinner_data);




              List<DayFoodModel> daylist = new ArrayList<DayFoodModel>();

            DayFoodModel dayfoodmodel = new DayFoodModel();
            dayfoodmodel.setDay(infomodel.getDay());
            dayfoodmodel.setTime(infomodel.getTime());
            dayfoodmodel.setFoodData(infomodel.getItem() + infomodel.getMeasure());

            diet_day.setText("Current day "  );


            Collections.sort(daylist, new DayComparator());
            daylist.add(dayfoodmodel);




            Log.v("Logging", "Info" +  daylist);



        //      };



             if (dayfoodmodel.getTime() == 1 ) {

                  if( breakfast_data != null)  breakfast_data.setText(dayfoodmodel.getFoodData());

             }

             if (dayfoodmodel.getTime() == 2) {

                 if( snach1_data != null)  snach1_data.setText(dayfoodmodel.getFoodData());

         }

             if (dayfoodmodel.getTime() == 3) {

                 if( lunch_data != null)  lunch_data.setText(dayfoodmodel.getFoodData());

         }

             if (dayfoodmodel.getTime() == 4) {

                  if( snack2_data != null)  snack2_data.setText(dayfoodmodel.getFoodData());

         }

             if (dayfoodmodel.getTime() == 5) {

                 if( dinner_data != null)  dinner_data.setText(dayfoodmodel.getFoodData()); 

         }



        //  


         }


//   }

    return v;
}
}

DayFoodModel.java

public class DayFoodModel {

int _day;
int _qty;
int _time;
String _item;
String _measure;
String _food_data;




public DayFoodModel()
{

}



public DayFoodModel(int day, int qty, int time,  String item, String measure, String food_data){


    this._day = day;
    this._qty = qty;
    this._item = item;
    this._time = time;
    this._measure = measure;
    this._food_data = food_data;



}

public DayFoodModel(String item, String measure, String food_data)

{
  this._item = item;
  this._measure = measure;
  this._food_data = food_data;

}


public int getDay(){
    return this._day;
}

public void setDay(int day){
    this._day = day;

}

public int getQty(){
    return this._qty;
}

public void setQty(int qty){
    this._qty = qty;

}

public int getTime(){
    return this._time;
}

public void setTime(int time){
    this._time = time;

}

public String getItem(){
    return this._item;
}

public void setItem(String item){
    this._item = item;
}

public String getMeasure(){
    return this._measure;
}

public void setMeasure(String measure){
    this._measure = measure;
}

public String getFoodData(){
    return this._food_data;
}

public void setFoodData(String food_data){
    this._food_data = food_data;
}


}

FoodInfoModel

public class FoodInfoModel
    {

int _dietid;
int _id;
int _day;
int _qty;
int _time;
String _item;
String _measure;
public String breakfast_data_string;


public FoodInfoModel()
{

}



    public FoodInfoModel(int dietid, int id, int day, int qty, int time,  String item, String measure){

    this._id = id;
    this._dietid = dietid;
    this._day = day;
    this._qty = qty;
    this._item = item;
    this._measure = measure;

    this.breakfast_data_string = measure + item;


    }

public FoodInfoModel(String item, String measure, String breakfast_data_string)

{
  this._item = item;
  this._measure = measure;
    this.breakfast_data_string = measure + item;

}



public int getID(){
    return this._id;
}

public void setID(int id){
    this._id = id;

}

public int getDietID(){
    return this._dietid;
}

public void setDietID(int dietid){
    this._dietid = dietid;

}

public int getDay(){
    return this._day;
}

public void setDay(int day){
    this._day = day;

}

public int getQty(){
    return this._qty;
}

public void setQty(int qty){
    this._qty = qty;

}

public int getTime(){
    return this._time;
}

public void setTime(int time){
    this._time = time;

}

public String getItem(){
    return this._item;
}

public void setItem(String item){
    this._item = item;
}

public String getMeasure(){
    return this._measure;
}

public void setMeasure(String measure){
    this._measure = measure;
}

public String getBreakfastData(){
    return this.breakfast_data_string;
}

public void setBreakfastData(String breakfast_data_string){
    this.breakfast_data_string = breakfast_data_string;
}
}

回答1:


You need to add a custom Comparator to your object, then you can use Arrays.sort method to do sorting of whatever kind you like.

Here is one reference on how to make and use a Comparator:

http://examples.javacodegeeks.com/core-java/util/comparator/java-comparator-example/

EDIT:

Besides the sorting, you should probably use an Adapter against an existing data set. In other words, these lines in your Adapter should not exist:

        Collections.sort(daylist, new DayComparator());
        daylist.add(dayfoodmodel);

First, you are sorting - then adding data. All your data should be in the array, and then sort it.

Second, you should pass all the data into the Adapter so the sort should occur before you do getView in the Adapter



来源:https://stackoverflow.com/questions/28344387/android-sorting-data-into-arrays-arrayadapter-json-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!