我试图用阵列适配器显示一个列表视图。我从数据库中获取的数组。
Am trying to display a listview using array adapter. I get the array from the database.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, ArrayofName);
ListView myListView = (ListView) ll.findViewById(R.id.list1);
myListView.setAdapter(adapter);
现在我想用头对其进行分类。我尝试添加另一个阵列适配器。但它不工作的头。
Now i want to categorize them using the headers. I tried to add another array adapter. But it doesnt work for the headers.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, ArrayofName);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, ArrayofName);
ListView myListView = (ListView) ll.findViewById(R.id.list1);
myListView.addHeaderView(adapter1);
myListView.setAdapter(adapter);
我怎样才能得到这个工作?
How can i get this to work?
PS:我使用的是片段。
PS : I am using a fragment.
排序要与在项目之间的报头(SectionItem)显示它们在你的顺序适配器的项目。
Sort the items in your adapter in the order you want to display them with the headers (SectionItem) in between the items.
创建了一个Person类和SectionItem类。
Create a Person class and a SectionItem class.
每个名字的第一个字母人员和部分适配器的例子:
Example of an adapter with persons and sections per first letter of the name:
- A (SectionItem)
- Adam (Person)
- Alex (Person)
- Andre (Person)
- B (SectionItem)
- Ben (Person)
- Boris (Person)
...
在adapter.getViewTypeCount回报2。 在adapter.getItemViewType(位置)返回0 SectionItems和1人。 在getView(...)返回一个SectionItem或个人视图。
In the adapter.getViewTypeCount return 2. In the adapter.getItemViewType(position) return 0 for SectionItems and 1 for Persons. In the getView(...) return a view for a SectionItem or a Person.
例如:
public class SectionedAdapter extends BaseAdapter {
....
@Override
public int getViewTypeCount() {
return 2; // The number of distinct view types the getView() will return.
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof SectionItem){
return 0;
}else{
return 1;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object item = getItem(position);
if (item instanceof SectionItem) {
if (convertView == null) {
convertView = getInflater().inflate(R.layout.section, null);
}
// Set the section details.
} else if (item instanceof Person) {
if (convertView == null) {
convertView = getInflater().inflate(R.layout.person, null);
}
// Set the person details.
}
return convertView;
}
}