【问题标题】:How to group the datas in listview of same date如何对同一日期的列表视图中的数据进行分组
【发布时间】:2017-05-24 18:30:25
【问题描述】:

我正在创建一个 android 应用程序,在该应用程序中我从 sqlite 数据库中获取数据并将其显示在列表视图中,这部分工作正常,但我想将数据分组到具有相同日期的列表视图中。谁能告诉我如何对列表项进行分组基于列表视图中的同一日期。

自定义适配器:

public class NewDaybookAdapter extends BaseAdapter{
Context context;
private LayoutInflater inflater;
private ArrayList<Daybooklist> daybooklists;
DatabaseHandler databaseHandler;
boolean isListScrolling;

public NewDaybookAdapter(Context context, ArrayList<Daybooklist> daybooklists) {
    this.context = context;
    this.daybooklists = daybooklists;
}

@Override
public int getCount() {
    return daybooklists.size();
}

@Override
public Object getItem(int i) {
    return daybooklists.get(i);
}

@Override
public long getItemId(int position) {
    return position;
}



@Override
public View getView(int position, View convertview, ViewGroup viewGroup) {
    if (inflater == null)
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertview == null)
        convertview = inflater.inflate(R.layout.activity_daybook_newitem, null);
    final TextView day_name = (TextView) convertview.findViewById(R.id.tv_daybook_name);
    final TextView day_description = (TextView) convertview.findViewById(R.id.tv_daybook_description);
    final TextView day_type = (TextView) convertview.findViewById(R.id.tv_daybook_type);
    final TextView day_amount = (TextView) convertview.findViewById(R.id.tv_daybook_amount);
    final TextView day_usertype = (TextView) convertview.findViewById(R.id.tv_usertype);
    final TextView day_time = (TextView) convertview.findViewById(R.id.tv_daybook_time);
    final ImageView day_check = (ImageView) convertview.findViewById(R.id.img_doneall);
    final TextView daybook_location = (TextView) convertview.findViewById(R.id.tv_daybook_location);
    final TextView daybook_date = (TextView)convertview.findViewById(R.id.tv_date);
    final LinearLayout linear_date = (LinearLayout)convertview.findViewById(R.id.li_date);

    databaseHandler = new DatabaseHandler(context);
    final Daybooklist m = daybooklists.get(position);
    if (m.getUsertype() != null && !m.getUsertype().isEmpty()) {
        String s = m.getSdate();
        String[] spiliter = s.split("-");
        String year = spiliter[0];
        String month = spiliter[1];
        String date = spiliter[2];
        if (month.startsWith("01")) {
            daybook_date.setText(date + "Jan" + year);
        } else if (month.startsWith("02")) {
            daybook_date.setText(date + "Feb" + year);
        } else if (month.startsWith("03")) {
            daybook_date.setText(date + "Mar" + year);
        } else if (month.startsWith("04")) {
            daybook_date.setText(date + "Apr" + year);
        } else if (month.startsWith("05")) {
            daybook_date.setText(date + "May" + year);
        } else if (month.startsWith("06")) {
            daybook_date.setText(date + "Jun" + year);
        } else if (month.startsWith("07")) {
            daybook_date.setText(date + "Jul" + year);
        } else if (month.startsWith("08")) {
            daybook_date.setText(date + "Aug" + year);
        } else if (month.startsWith("09")) {
            daybook_date.setText(date + "Sep" + year);
        } else if (month.startsWith("10")) {
            daybook_date.setText(date + "Oct" + year);
        } else if (month.startsWith("11")) {
            daybook_date.setText(date + "Nov" + year);
        } else if (month.startsWith("12")) {
            daybook_date.setText(date + "Dec" + year);
        }


        if (m.getUsertype().startsWith("farmer") | m.getUsertype().startsWith("singleworker") | m.getUsertype().startsWith("groupworker") | m.getUsertype().startsWith("payvehicle")) {
            if (m.getUsertype().startsWith("farmer")) {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                String locat = String.valueOf(databaseHandler.getfarmerlocation(m.getMobileno()));
                locat = locat.replaceAll("\\[", "").replaceAll("\\]", "");
                Log.e("farmerlocation", locat);
                daybook_location.setText(locat);
                day_type.setText(m.getType());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }

                day_amount.setText("\u20B9" + m.getExtraamt());
                if (m.getAmountout().startsWith("0.0") | m.getAmountout().startsWith("0")) {
                    //     day_amount.setTextColor(activity.getResources().getColor(R.color.green));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.INVISIBLE);
                } else {
                    //    day_amount.setTextColor(activity.getResources().getColor(R.color.album_title));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.VISIBLE);
                }

                day_time.setText(m.getCtime());
            } else {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                daybook_location.setText(m.getType());
                day_type.setText(m.getType());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }

                day_amount.setText("\u20B9" + m.getExtraamt());
                if (m.getAmountout().startsWith("0.0") | m.getAmountout().startsWith("0")) {
                    //  day_amount.setTextColor(activity.getResources().getColor(R.color.green));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.INVISIBLE);
                } else {
                    //  day_amount.setTextColor(activity.getResources().getColor(R.color.album_title));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.VISIBLE);
                }

                day_time.setText(m.getCtime());
            }


        } else if (m.getUsertype().startsWith("advancefarmer") | m.getUsertype().startsWith("workeradvance") | m.getUsertype().startsWith("kgroupadvance") | m.getUsertype().startsWith("otherexpense") | m.getUsertype().startsWith("vehicle")) {
            if (m.getUsertype().startsWith("advancefarmer")) {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                day_type.setText(m.getType());
                String locat = String.valueOf(databaseHandler.getfarmerlocation(m.getMobileno()));
                locat = locat.replaceAll("\\[", "").replaceAll("\\]", "");
                Log.e("farmerlocation", locat);
                daybook_location.setText(locat);
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }
                Log.e("amountout", m.getAmountout());
                day_amount.setText("\u20B9" + m.getAmountout());
                day_time.setText(m.getCtime());
            } else {
                day_name.setText(m.getName());
                day_description.setText(m.getType());
                day_type.setText(m.getType());
                daybook_location.setText(m.getDescription());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }
                Log.e("amountout", m.getAmountout());
                day_amount.setText("\u20B9" + m.getAmountout());
                day_time.setText(m.getCtime());
            }


        } else if (m.getUsertype().startsWith("buyer")) {
            day_name.setText(m.getName());
            day_description.setText(m.getDescription());
            day_amount.setText("\u20B9" + m.getAmountin());
            day_type.setText(" ");
            day_time.setText(m.getCtime());
            daybook_location.setText(m.getType());
        }

    }


    return convertview;
}   

}

数据库查询:

public ArrayList<Daybooklist> getAlldaybookitemsentries() {
    ArrayList<Daybooklist> daybooklists = new ArrayList<Daybooklist>();
    String selectquery = "SELECT daybookusertype,amountin,amountout,otheramount,daybookname,daybookdescription,daybooktype,mobileno,date,daybooktime FROM daybookdetails  ORDER BY date DESC";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectquery, null);
    if (cursor.moveToFirst()) {
        do {
            Daybooklist daybooklistentries = new Daybooklist();               
            daybooklistentries.setUsertype(cursor.getString(0));
            daybooklistentries.setAmountin(cursor.getString(1));
            daybooklistentries.setAmountout(cursor.getString(2));
            daybooklistentries.setExtraamt(cursor.getString(3));
            daybooklistentries.setName(cursor.getString(4));
            daybooklistentries.setDescription(cursor.getString(5));
            daybooklistentries.setType(cursor.getString(6));
            daybooklistentries.setMobileno(cursor.getString(7));
            daybooklistentries.setSdate(cursor.getString(8));
            daybooklistentries.setCtime(cursor.getString(9));
            daybooklists.add(daybooklistentries);

        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return daybooklists;
}

我得到的输出:

【问题讨论】:

  • 组是什么意思?你想显示一次相同的日期吗?
  • 是的,我想显示一次相同的日期
  • 我认为 listview 上没有直接功能可以按某个值对项目进行分组。但是,想法是使用 SQL 查询中的concat -> 按日期分组 -> 选择连接值和日期 -> 存储值 -> 从适配器拆分值 -> 并使用可扩展列表视图处理你的子项目developer.android.com/reference/android/widget/…

标签: android sqlite listview


【解决方案1】:

试试看……

TextView messagedate = (TextView) convertView.findViewById(R.id.datedetailstext);

if(position == 0) {
    messagedate.setVisibility(View.VISIBLE);
    messagedate.setText(messageslist.getDate());
} else {
    if(messageslist.get(position - 1).getDate().equals(messageslist.get(position).getDate())) {
        messagedate.setVisibility(View.GONE);
    } else {
        messagedate.setVisibility(View.VISIBLE);
        messagedate.setText(messageslist.getDate());
    }
}

【讨论】:

    【解决方案2】:

    你可以试试:

    1. 按日期排序数据,通常是时间戳
    2. 如果您不需要对组进行文件夹,只需添加另一个显示日期的项目
    3. 如果您想文件夹/展开组,您需要尝试添加自定义项目组

    【讨论】:

      【解决方案3】:

      存储previousDate,然后与当前的比较。如果匹配,请不要设置日期!

      String previousDate=""; //make it global
      
      
              String fullDate="";
              if (month.startsWith("01")) {
                  fullDate =date + "Jan" + year;
              } else if (month.startsWith("02")) {
                  fullDate =date + "Feb" + year;
              } else if (month.startsWith("03")) {
                  fullDate =date + "Mar" + year;
              } else if (month.startsWith("04")) {
                  fullDate =date + "Apr" + year;
              } else if (month.startsWith("05")) {
                  fullDate =date + "May" + year;
              } else if (month.startsWith("06")) {
                  fullDate =date + "Jun" + year;
              } else if (month.startsWith("07")) {
                  fullDate =date + "Jul" + year;
              } else if (month.startsWith("08")) {
                  fullDate =date + "Aug" + year;
              } else if (month.startsWith("09")) {
                  fullDate =date + "Sep" + year;
              } else if (month.startsWith("10")) {
                  fullDate =date + "Oct" + year;
              } else if (month.startsWith("11")) {
                  fullDate =date + "Nov" + year;
              } else if (month.startsWith("12")) {
                  fullDate =date + "Dec" + year;
              }
      
              if (!fullDate.equals(previousDate)){
                  previousDate =fullDate;
                  daybook_date.setText(date + "Dec" + year);
              }
      

      【讨论】:

      • 当列表视图滚动时,日期不可见
      • 不应该是这样吗?!!仅限第一项的日期
      • 每一项我都要检查一下
      • 这是什么意思?这设置了任何一天的第一个条目的日期。如果你想要每个项目,那怎么叫组? 您是否将前一个日期作为全局变量?
      • 是的,我已将上一个日期作为全局变量
      【解决方案4】:

      使用 HashMap 或 ArrayMap,将字符串日期作为键,并将值放入数组列表。 因为这对新手来说可能很难,但您可以将此数据与分段回收视图一起使用。

      有关分段回收站视图的更多信息,请查看this。对于带有 recyclerview 的 Hashmap,请检查 this

      【讨论】:

        猜你喜欢
        • 2017-12-20
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多