Question

EDITED

I am working on a contact list and trying to implement sectioned listview. When the app gets started, only one of the Header (Section) starts displaying itself after some chunks of items repeatedly and changing its position on scrolling up/down.

Here is my code :

    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;
        }

    }

During Debugging, After the ending line return view ; , it enters in CursorAdapter.class and passes by through the lines:

if (!mCursor.moveToPosition(position)) {
  throw new IllegalStateException("couldn't move cursor to position " + position);
}

and enters in if condtion, but app doesn't crash.

Was it helpful?

Solution

Finally Got my answer my own after two weeks...my if-else conditions were too complex , so i make comparison on some different criteria.

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)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top