我正在显示带有标题标题的手机通讯录 但在添加节标题后,我的位置错了 这是我的代码
mycode的
MainActivity.java
public class MainActivity extends Activity {
private AlphabetListAdapter adapter = new AlphabetListAdapter();
private List<Object[]> alphabet = new ArrayList<Object[]>();
private HashMap<String, Integer> sections = new HashMap<String, Integer>();
private List<ContactBean> list = new ArrayList<ContactBean>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_alphabet);
listView = (ListView) findViewById(R.id.listView);
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactBean objContact = new ContactBean();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
list.add(objContact);
}
phones.close();
Collections.sort(list, ContactBean.StuNameComparator);
List<Row> rows = new ArrayList<Row>();
int start = 0;
int end = 0;
String previousLetter = null;
Object[] tmpIndexItem = null;
Pattern numberPattern = Pattern.compile("[0-9]");
// for (String country : list) {
for (int i = 0; i <= list.size() - 1; i++) {
String contactName= list.get(i).getName();
String firstLetter = contactName.substring(0, 1).toUpperCase();
// Group numbers together in the scroller
if (numberPattern.matcher(firstLetter).matches()) {
firstLetter = "#";
}
// If we've changed to a new letter, add the previous letter to the
// alphabet scroller
if (previousLetter != null && !firstLetter.equals(previousLetter)) {
end = rows.size() - 1;
tmpIndexItem = new Object[3];
tmpIndexItem[0] = previousLetter.toUpperCase(Locale.UK);
tmpIndexItem[1] = start;
tmpIndexItem[2] = end;
alphabet.add(tmpIndexItem);
start = end + 1;
}
// Check if we need to add a header row
if (!firstLetter.equals(previousLetter)) {
rows.add(new Section(firstLetter));
sections.put(firstLetter, start);
}
// Add the contactNameto the list
rows.add(new Item(contactName));
previousLetter = firstLetter;
}
if (previousLetter != null) {
// Save the last letter
tmpIndexItem = new Object[3];
tmpIndexItem[0] = previousLetter.toUpperCase(Locale.UK);
tmpIndexItem[1] = start;
tmpIndexItem[2] = rows.size() - 1;
alphabet.add(tmpIndexItem);
}
adapter.setRows(rows);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//facing problem here
Log.v("position", "position-->" + list.get(position).getName());
}
});
}
}
ContactBean.java
public class ContactBean {
private String name;
private String phoneNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
/*Comparator for sorting the list by Student Name*/
public static Comparator<ContactBean> StuNameComparator = new Comparator<ContactBean>() {
@Override
public int compare(ContactBean lhs, ContactBean rhs) {
String contactName1 = lhs.getName().toUpperCase();
String contactName2 = rhs.getName().toUpperCase();
//ascending order
return contactName1.compareTo(contactName2);
}};
}
AlphabetListAdapter.java
public class AlphabetListAdapter extends BaseAdapter {
public static abstract class Row {
}
public static final class Section extends Row {
public final String text;
public Section(String text) {
this.text = text;
}
}
public static final class Item extends Row {
public final String text;
public Item(String text) {
this.text = text;
}
}
private List<Row> rows;
public void setRows(List<Row> rows) {
this.rows = rows;
}
@Override
public int getCount() {
return rows.size();
}
@Override
public Row getItem(int position) {
return rows.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof Section) {
return 1;
} else {
return 0;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (getItemViewType(position) == 0) { // Item
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.row_item, parent, false);
}
Item item = (Item) getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(item.text);
} else { // Section
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.row_section, parent, false);
}
Section section = (Section) getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(section.text);
}
return view;
}
}
我被困在这里
答案 0 :(得分:-1)
请看下面的示例,它很好地描述了如何在列表视图中实现Sectioned Headers。
1)https://w2davids.wordpress.com/android-sectioned-headers-in-listviews/
2)http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
3)https://github.com/beworker/pinned-section-listview
4)http://codetheory.in/android-dividing-listview-sections-group-headers/