2017-01-10 15 views
-1

我正在創建一個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; 
} 

輸出我得到: enter image description here

+0

你是什麼意思的組?你想顯示一次相同的日期? –

+0

是的,我想要顯示相同的日期一次 –

+0

我認爲沒有直接的功能在列表視圖按項目分組的特定值。然而,想法是使用SQL查詢中的'concat'->按日期對其進行分組 - >選擇連接值並將日期 - >存儲值 - >從適配器分割值 - >並使用可擴展列表視圖處理你的子項目 https://developer.android.com/reference/android/widget/ExpandableListView.html – Roljhon

回答

0

使用HashMap的或字符串日期ArrayMap關鍵在價值放列表。 由於這對於新手來說可能很難,但是您可以將這些數據與分區的回收站視圖一起使用。

欲瞭解更多關於分段回收再檢查this。以及用於回收查看的Hashmap檢查this

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); 
     } 
+0

當列表視圖滾動日期不可見 –

+0

是不是應該是?!!僅限第一件商品的日期 –

+0

對於每一件商品我都要檢查它 –

0

你可以試試:

  1. 排序按日期,通常是一個時間戳
  2. 數據如果您不需要文件夾中的組,只需添加另一個項目顯示日期
  3. 如果要將文件夾/文件夾解壓,請嘗試添加自定義項目組
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()); 
    } 
} 
相關問題