我正在創建一個Android應用程序,我從sqlite數據庫中獲取數據並將其顯示在列表視圖中此部分工作正常,但我想在具有相同日期的列表視圖中對數據進行分組任何人都可以告訴我如何根據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;
}
你是什麼意思的組?你想顯示一次相同的日期? –
是的,我想要顯示相同的日期一次 –
我認爲沒有直接的功能在列表視圖按項目分組的特定值。然而,想法是使用SQL查詢中的'concat'->按日期對其進行分組 - >選擇連接值並將日期 - >存儲值 - >從適配器分割值 - >並使用可擴展列表視圖處理你的子項目 https://developer.android.com/reference/android/widget/ExpandableListView.html – Roljhon