EDITED
我正在处理联系人列表并尝试实施分段列表视图。当应用程序启动时,只有一个Header(Section)在重复一些项目后开始显示自己,并在向上/向下滚动时改变其位置。
这是我的代码:
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
Holder holder = null;
LayoutInflater layoutInflater;
// String keyWord = "empty";
public MySimpleCursorAdapter(Context context, int layout, Cursor cur,
String[] from, int[] to, int flag) {
super(context, layout, cur, from, to, flag);
}
public String getTitle(String contName) {
return contName.substring(0, 1);
}
@Override
public View newView(Context context, Cursor mCursor, ViewGroup parent) {
holder = new Holder();
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String cont_Name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
View view = null;
if ( ! keyWord.equalsIgnoreCase(getTitle(cont_Name)) || keyWord.equals(null))
{
view = layoutInflater.inflate(R.layout.section_header, null);
TextView sectionTitle = (TextView) view.findViewById(R.id.title2);
sectionTitle.setText(getTitle(cont_Name));
keyWord = getTitle(cont_Name);
Log.d("KeyWord", keyWord);
Log.d("Contact Name", cont_Name);
}
else if(keyWord.equalsIgnoreCase(getTitle(cont_Name))) {
view = layoutInflater.inflate(R.layout.pm_fragment, null);
holder.contactTitle= (TextView)view.findViewById(R.id.textView1);
holder.contactDetail = (TextView)view.findViewById(R.id.textView2);
holder.contactTitle.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactDetail.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Log.d("KeyWord", keyWord);
Log.d("Contact Name", cont_Name);
String contactId_String = ""+mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
long contactId = Long.parseLong(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
DatabaseHandler handler = new DatabaseHandler(getActivity());
Contact matchedContact = handler.getContact(contactId) ;
String dbContactId= "";
if(matchedContact != null){
if(matchedContact.getID() != 0 && ""+matchedContact.getID() != null){
dbContactId= ""+matchedContact.getID();
}
if(dbContactId.equals(contactId_String)){
holder.myImage = (ImageView) view.findViewById(R.id.imageView1);
holder.myImage.getLayoutParams().height = 100;
holder.myImage.getLayoutParams().width = 100;
holder.myImage.setBackgroundResource(R.drawable.person_empty_online);
}
}else{
holder.myImage = (ImageView) view.findViewById(R.id.imageView1);
holder.myImage.getLayoutParams().height = 100;
holder.myImage.getLayoutParams().width = 100;
holder.myImage.setBackgroundResource(R.drawable.person_empty_offline);
}
handler.close();
keyWord = getTitle(cont_Name);
}
return view;
}
}
在调试期间,在结束行return view ;
之后,它进入CursorAdapter.class
并通过以下行传递:
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
并输入if condtion
,但应用不会崩溃。
答案 0 :(得分:0)
最后两周后得到了我自己的答案......我的if-else
条件过于复杂,所以我根据一些不同的标准进行比较。
if (mCursor.getPosition() > 0 && mCursor.moveToPrevious())
{
preName = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
mCursor.moveToNext();
}
else if(mCursor.getPosition() == 0)
{
preName = null;
}
else{
preName = null;
}
if(preName != null){
preTitle = getTitle(preName);
}
//===============================================================================
/*
* Setting Header And Contact Details
*/
//===============================================================================
if(mCursor.isFirst()){
holder.titleText.setVisibility(View.VISIBLE);
holder.titleText.setText(itemTitle);
}
else if(preName != null){
if(! itemTitle.equalsIgnoreCase(preTitle))
{
holder.titleText.setVisibility(View.VISIBLE);
holder.titleText.setText(itemTitle);
}else{
holder.titleText.setVisibility(View.GONE);
}
}
holder.contactTitle.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactDetail.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));