2012-12-24 77 views
4

我是一名新的android開發人員。我搜索了很多谷歌,但我 沒有得到預期的結果。我也嘗試了我的邏輯,但我沒有得到它。如果有人能幫助解決這個問題,我會很感激。Android Listview標題和部分月份和日期列表

1. My problem is that 

    ______________ 
    November,2012  //This is header 
    ______________ 
    November 12,2012 
    November 15,2012 //This list of date that contain in month 
    _____________ 
    December,2012  //This is header 
    _____________ 
    December 23,2012 
    December 30,2012 //This list of date that contain in month 

我的問題是如何獲得包含同一個月,並列出了在其中 項目我怎樣才能獲得的產品清單這解決了嗎?請提出一些想法。

+0

顯示分隔符適配器和問題is________? –

+0

plz解釋更多關於你的問題 –

+0

@SahilMahajanMj我編輯..和我的問題如何在同一日期內的項目包含.. – QuokMoon

回答

0

如果您希望在列表中有多個標題,並且當您單擊期望的標題列表分支時,您可以使用可展開的ListView,但是如果您希望在列表外選擇您的月份,然後匹配日期,一個月可以使用

youradapter.setFilterQueryProvider 

,或者您可以使用textwatcher與所需月份

3

這裏來過濾列表是做

創建一個分隔符自定義的ListView ROW佈局文件的一種方法或查看。 (頁眉的TextView)

listview_row_with_separator.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <!-- list Separator TextView Style - the list separator Header--> 
    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:id="@+id/textview_header_separator" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:text="November, 2012" 
     android:visibility="gone" 
     /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView_row_value" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="November 12, 2012" 
     android:textAppearance="?android:attr/textAppearanceMedium" > 
    </TextView> 

    </LinearLayout> 
    </LinearLayout> 

Importent!確保您的列表以某種方式排序(日期)

擴展一個持有「Your_List_Item」的ArrayAddapter並實現顯示分隔符的populateView(Your_List_Item),如果下一個列表項不等於上一個列表項。

public class Your_List_Adapter extends ArrayAdapter<Your_List_Item> { 
private Activity mActivity; 
private String mCurrentMonth = ""; 

public Your_Adapter(Activity activity) { 
    super(activity, 0); 
     mActivity = activity; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mActivity.getLayoutInflater().inflate(
       R.layout.listview_row_with_separator, null); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     // the (list row) convertView is already inflated, so we can get 
     // it from the Tag 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    Your_List_Item your_list_item = getItem(position); 

    //Check if the next list item is not equal the previous list item. 
    if (!mCurrentMonth.equalsIgnoreCase(your_list_item.month.toString())) { 
     holder.populateView(your_list_item, true); 
     mCurrentMonth = your_list_item.date; 
    } else{ 
     holder.populateView(your_list_item, false); 
    } 

    return convertView; 
} 
} 

一個ViewHolder類如果需要

class ViewHolder { 

/********** DECLARES ************/ 
private TextView textView_row_value; 
public TextView textview_header_separator; 

public ViewHolder(final View root) { 
    /********** INITIALIZES *************/ 
    textview_header_separator = (TextView) root.findViewById(R.id.textview_header_separator); 
    textView_row_value = (TextView) root.findViewById(R.id.textView_row_value); 
} 

public void populateView(final Your_List_Item your_list_item,final boolean needSeparator) { 
    /* 
    * add Separator 
    */ 

    if (needSeparator) { 
      //set header text: November, 2012 
     textview_header_separator.setText(your_list_item.date); 
      textview_header_separator.setVisibility(View.VISIBLE); 
    } else { 
      textview_header_separator.setVisibility(View.GONE); 
    } 
    //set row value: November 12, 2012 
    textView_row_value.setText(your_list_item.date); 

} 
}