在我的listView中我试图将我的数据划分(分类)到不同的部分说我有一个状态列表,我想根据他们的国家对它们进行分类,但我现在所拥有的是按字母顺序对我的数据进行分类不知道它是如何发生的,我怎样才能实现我想要的。谁能在这里指导我?
我的适配器:
public class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {
private String[] countries;
private LayoutInflater inflater;
private ArrayList<States> statesArray;
public MyAdapter(Context context , ArrayList<States> states) {
inflater = LayoutInflater.from(context);
satesArray = states;
countries = context.getResources().getStringArray(R.array.all_countries);
}
@Override
public int getCount() {
return statesArray.size();
}
@Override
public Object getItem(int position) {
return statesArray.get(position).getName();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_view_row, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.listViewTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(statesArray.get(position).getName());
return convertView;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = inflater.inflate(R.layout.sticky_header, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
//set header text as first char in name
String headerText = "" + countries[position];
holder.text.setText(headerText);
return convertView;
}
@Override
public long getHeaderId(int position) {
// return the first character of the country as ID because this is what headers are based upon
return satesArray.get(position).getName().subSequence(0, 1).charAt(0);
}
class HeaderViewHolder {
TextView text;
}
class ViewHolder {
TextView text;
}
}
更新:
我想要的就是这里,但是在部分的字母表中会有国家名称,而在行中会有各自国家的状态: