Android 根据过帐日期显示列表项
我正在创建一个自定义列表。在列表中,我显示了用户上载的文档。我的列表如下所示 我想要的是列表标题应该包含该月,并且该月上载的所有文档都应该在该标题下。 例如,我喜欢这份清单 Xml代码Android 根据过帐日期显示列表项,android,listview,Android,Listview,我正在创建一个自定义列表。在列表中,我显示了用户上载的文档。我的列表如下所示 我想要的是列表标题应该包含该月,并且该月上载的所有文档都应该在该标题下。 例如,我喜欢这份清单 Xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_docs_image"
android:adjustViewBounds="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="File Name"
android:id="@+id/tv_file_name"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tv_posted_on" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_weight="2.6"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/share_list"
android:layout_gravity="end"
android:focusable="false"
android:id="@+id/bt_share" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
备注-所有列表内容均来自本地创建的数据库请查看此项目: 致以最良好的祝愿 这里是一个来自上面链接的项目源代码的使用示例
public class SectionListActivity extends Activity {
private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> {
private final SectionListItem[] items;
public StandardArrayAdapter(final Context context,
final int textViewResourceId, final SectionListItem[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View view = convertView;
if (view == null) {
final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.example_list_view, null);
}
final SectionListItem currentItem = items[position];
if (currentItem != null) {
final TextView textView = (TextView) view
.findViewById(R.id.example_text_view);
if (textView != null) {
textView.setText(currentItem.item.toString());
}
}
return view;
}
}
SectionListItem[] exampleArray = { // Comment to prevent re-format
new SectionListItem("Test 1 - A", "A"), //
new SectionListItem("Test 2 - A", "A"), //
new SectionListItem("Test 3 - A", "A"), //
new SectionListItem("Test 4 - A", "A"), //
new SectionListItem("Test 5 - A", "A"), //
new SectionListItem("Test 6 - B", "B"), //
new SectionListItem("Test 7 - B", "B"), //
new SectionListItem("Test 8 - B", "B"), //
new SectionListItem("Test 9 - Long", "Long section"), //
new SectionListItem("Test 10 - Long", "Long section"), //
new SectionListItem("Test 11 - Long", "Long section"), //
new SectionListItem("Test 12 - Long", "Long section"), //
new SectionListItem("Test 13 - Long", "Long section"), //
new SectionListItem("Test 14 - A again", "A"), //
new SectionListItem("Test 15 - A again", "A"), //
new SectionListItem("Test 16 - A again", "A"), //
new SectionListItem("Test 17 - B again", "B"), //
new SectionListItem("Test 18 - B again", "B"), //
new SectionListItem("Test 19 - B again", "B"), //
new SectionListItem("Test 20 - B again", "B"), //
new SectionListItem("Test 21 - B again", "B"), //
new SectionListItem("Test 22 - B again", "B"), //
new SectionListItem("Test 23 - C", "C"), //
new SectionListItem("Test 24 - C", "C"), //
new SectionListItem("Test 25 - C", "C"), //
new SectionListItem("Test 26 - C", "C"), //
};
private StandardArrayAdapter arrayAdapter;
private SectionListAdapter sectionAdapter;
private SectionListView listView;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view,
exampleArray);
sectionAdapter = new SectionListAdapter(getLayoutInflater(),
arrayAdapter);
listView = (SectionListView) findViewById(getResources().getIdentifier(
"section_list_view", "id",
this.getClass().getPackage().getName()));
listView.setAdapter(sectionAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.standard_list:
arrayAdapter = new StandardArrayAdapter(this,
R.id.example_text_view, exampleArray);
sectionAdapter = new SectionListAdapter(getLayoutInflater(),
arrayAdapter);
listView.setAdapter(sectionAdapter);
return true;
case R.id.empty_list:
arrayAdapter = new StandardArrayAdapter(this,
R.id.example_text_view, new SectionListItem[] {});
sectionAdapter = new SectionListAdapter(getLayoutInflater(),
arrayAdapter);
listView.setAdapter(sectionAdapter);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public类SectionListActivity扩展活动{
私有类StandardArrayAdapter扩展了ArrayAdapter{
私人最终部分清单项目[]项目;
公共标准ArrayAdapter(最终上下文,
final int textViewResourceId,final SectionListItem[]项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
@凌驾
公共视图getView(最终整型位置,最终视图转换视图,
最终视图组(父视图){
视图=转换视图;
如果(视图==null){
final LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=vi.充气(R.layout.example\u list\u视图,空);
}
最终章节列表项目当前项目=项目[位置];
如果(currentItem!=null){
最终文本视图文本视图=(文本视图)视图
.findViewById(R.id.example\u text\u view);
if(textView!=null){
setText(currentItem.item.toString());
}
}
返回视图;
}
}
SectionListItem[]exampleArray={//防止重新格式化的注释
新章节列表项(“测试1-A”、“A”)//
新章节列表项(“测试2-A”、“A”)//
新章节列表项(“测试3-A”、“A”)//
新章节列表项(“测试4-A”、“A”)//
新的章节列表项(“测试5-A”、“A”)//
新章节列表项(“试验6-B”、“B”)//
新章节列表项(“测试7-B”、“B”)//
新章节列表项(“测试8-B”、“B”)//
新章节列表项目(“测试9-长”、“长章节”)//
新章节列表项目(“测试10-长”、“长章节”)//
新章节列表项目(“测试11-长”、“长章节”)//
新章节列表项目(“测试12-长”、“长章节”)//
新章节列表项目(“试验13-长”、“长章节”)//
新的章节列表项(“测试14-再次“A”)//
新的章节列表项(“测试15-再次“A”)//
新的章节列表项(“测试16-再次“A”)//
新的章节列表项(“测试17-B再次出现”,“B”)//
新的章节列表项(“再次测试18-B”,“B”)//
新的章节列表项(“测试19-B再次出现”,“B”)//
新的章节列表项(“再次测试20-B”,“B”)//
新的章节列表项(“测试21-B再次出现”,“B”)//
新的章节列表项(“测试22-B再次出现”,“B”)//
新章节列表项(“测试23-C”、“C”)//
新章节列表项(“测试24-C”、“C”)//
新章节列表项目(“测试25-C”、“C”)//
新章节列表项(“试验26-C”、“C”)//
};
专用标准阵列适配器阵列适配器;
私有部分列表适配器部分适配器;
私有部分列表视图列表视图;
@凌驾
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrayAdapter=新标准arrayAdapter(此,R.id.example\u text\u视图,
示例(阵列);
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
listView=(SectionListView)findViewById(getResources().getIdentifier(
“节列表视图”、“id”,
这个.getClass().getPackage().getName());
setAdapter(sectionAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。测试菜单,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.标准清单:
arrayAdapter=新标准arrayAdapter(此,
R.id.example_text_view,exampleArray);
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
setAdapter(sectionAdapter);
返回true;
案例R.id.empty\u列表:
arrayAdapter=新标准arrayAdapter(此,
R.id.example_text_view,新的SectionListItem[]{});
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
setAdapter(sectionAdapter);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
这里是使用的布局XML文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<pl.polidea.sectionedlist.SectionListView android:id="@+id/section_list_view" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</pl.polidea.sectionedlist.SectionListView>
</FrameLayout>
尝试按日期对数据进行排序,并在列表中显示日期分组的数据。避免只链接答案!