【发布时间】: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/…