如何在同一日期的列表视图中对数据进行分组

时间:2017-01-10 09:54:35

标签: android sqlite listview

我正在创建一个Android应用程序,其中我从sqlite数据库中获取数据并在listview中显示它这部分工作正常,但我想将listview中的数据分组有相同的日期。任何人都可以告诉我如何对listitems进行分组基于listview中的相同日期。

CustomAdapter:

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

我得到的输出:  enter image description here

4 个答案:

答案 0 :(得分: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());
    }
}

答案 1 :(得分:0)

使用带字符串日期的HashMap或ArrayMap作为键,并使用值put arraylist。 因为这对新手来说很难,但是您可以将这些数据用于分段的recyclerview。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以尝试:

  1. 按日期订购数据,通常是时间戳
  2. 如果您不需要对该组进行归档,只需添加另一个显示日期的项目
  3. 如果要对组进行文件夹/取消存档,则需要尝试添加自定义项目组