Android 将数据排序到数组中 - ArrayAdapter JSON Listview
Android Sorting data into arrays - ArrayAdapter JSON Listview
我有一个MySQL table 叫做foods,其中包含饮食信息。我目前 运行 php 脚本调用以下 sql 命令 Select * 来自 Food where dietid = 1 which returns 下面的图片。
day 栏是节食的日子,所以第 1 天、第 2 天等。时间指的是一天中的某个时间点,所以时间 1 是早餐时间 2 是点心时间 3 是午餐时间 4 是下午点心时间 5 是晚餐。
我想在列表视图中按以下方式显示数据 diet.Description 的每一天都是由数量度量和项目组成
目前我在 log-cat 中有以下输出。
更新 - 编辑新的 LOGCAT
使用 Jims approact,我已经能够按照我需要的格式按日期和时间对这些数据进行分组,但是我现在面临着我之前遇到的问题,列表被夸大了,它将信息放在每个当我需要每 1 天排成 1 行时排。
问题是现在它会像这样膨胀视图,从而创建一个视图并将其放入每个项目。
更新了阵列适配器#
我想每天将这些数据分组到数组中,所以它是第 1 天 - 早餐 - 项目,零食 - 项目等......这样我就可以将它们全部放在一个列表中供用户使用查看。
我所有的数据都存储在 2 模型中 类。下面是我的适配器型号 类.
谢谢 :) 到达那里..
ArrayAdapter Class 已更新
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;
}
}
食品信息模型
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;
}
}
您需要为您的对象添加自定义 Comparator
,然后您可以使用 Arrays.sort
方法进行任何您喜欢的排序。
这里有一份关于如何制作和使用Comparator
的参考:
http://examples.javacodegeeks.com/core-java/util/comparator/java-comparator-example/
编辑:
除了排序之外,您可能还应该对现有数据集使用 Adapter
。换句话说,你的 Adapter
中的这些行不应该存在:
Collections.sort(daylist, new DayComparator());
daylist.add(dayfoodmodel);
首先,您正在排序 - 然后 添加数据。您所有的数据都应该在数组中,然后对其进行排序。
其次,您应该将所有数据传递到 Adapter
,这样排序应该发生在 之前 您在 Adapter
中执行 getView
我有一个MySQL table 叫做foods,其中包含饮食信息。我目前 运行 php 脚本调用以下 sql 命令 Select * 来自 Food where dietid = 1 which returns 下面的图片。
day 栏是节食的日子,所以第 1 天、第 2 天等。时间指的是一天中的某个时间点,所以时间 1 是早餐时间 2 是点心时间 3 是午餐时间 4 是下午点心时间 5 是晚餐。
我想在列表视图中按以下方式显示数据 diet.Description 的每一天都是由数量度量和项目组成
目前我在 log-cat 中有以下输出。
更新 - 编辑新的 LOGCAT
使用 Jims approact,我已经能够按照我需要的格式按日期和时间对这些数据进行分组,但是我现在面临着我之前遇到的问题,列表被夸大了,它将信息放在每个当我需要每 1 天排成 1 行时排。
问题是现在它会像这样膨胀视图,从而创建一个视图并将其放入每个项目。
更新了阵列适配器#
我想每天将这些数据分组到数组中,所以它是第 1 天 - 早餐 - 项目,零食 - 项目等......这样我就可以将它们全部放在一个列表中供用户使用查看。
我所有的数据都存储在 2 模型中 类。下面是我的适配器型号 类.
谢谢 :) 到达那里..
ArrayAdapter Class 已更新
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;
}
}
食品信息模型
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;
}
}
您需要为您的对象添加自定义 Comparator
,然后您可以使用 Arrays.sort
方法进行任何您喜欢的排序。
这里有一份关于如何制作和使用Comparator
的参考:
http://examples.javacodegeeks.com/core-java/util/comparator/java-comparator-example/
编辑:
除了排序之外,您可能还应该对现有数据集使用 Adapter
。换句话说,你的 Adapter
中的这些行不应该存在:
Collections.sort(daylist, new DayComparator());
daylist.add(dayfoodmodel);
首先,您正在排序 - 然后 添加数据。您所有的数据都应该在数组中,然后对其进行排序。
其次,您应该将所有数据传递到 Adapter
,这样排序应该发生在 之前 您在 Adapter
中执行 getView