Search code examples
androidandroid-contactssmsmanager

Android duplicate listview item when i am scrolling. how do i fix it?


I am trying to fetch contact numbers from the contact list. hi, my code troubleshooting duplicate item on scroll how I fix this problem? when I scroll my contact list in listview it is showing the duplicate item on scroll up or down. keep repeating. Here is my code:

Fragment class:

public class Contacts_Numbers extends ContactsFragment implements OnItemClickListener{

ListView            m_lvNumber;
Context             m_fragParent;
Contact_Details     m_frag;

List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
ContactListAdapter adapter;
Button select;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View rootView = inflater.inflate(R.layout.contact_number_list,
            container, false);
    m_lvNumber = (ListView) rootView.findViewById(R.id.lvContactNumber);
    select = (Button) rootView.findViewById(R.id.btnSelectContact);
    getContactDetails(getActivity().getContentResolver());
    adapter = new ContactListAdapter(getActivity(), name1, phno1);
    m_lvNumber.setAdapter(adapter);
    m_lvNumber.setOnItemClickListener(this);
    m_lvNumber.setItemsCanFocus(false);
    m_lvNumber.setTextFilterEnabled(true);

    select.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            StringBuilder checkedcontacts = new StringBuilder();
            System.out.println(".............."
                    + adapter.mCheckStates.size());
            for (int i = 0; i < name1.size(); i++)

            {
                if (adapter.mCheckStates.get(i) == true) {
                    checkedcontacts.append(name1.get(i).toString());
                    checkedcontacts.append("\n");

                } else {
                    System.out.println("Not Checked......"
                            + name1.get(i).toString());
                }

            }

            Toast.makeText(getActivity(), checkedcontacts, 1000).show();
        }

    });

    return rootView;
}

private void getContactDetails(ContentResolver contentResolver) {
    // TODO Auto-generated method stub




    try { 
        Uri simUri = Uri.parse("content://icc/adn");
        Cursor cursorSim = contentResolver.query(simUri, null,
                null, null, null); 

        while (cursorSim.moveToNext()) {
            String Name = cursorSim.getString(cursorSim
                    .getColumnIndex("name"));
            String PhoneNo = cursorSim.getString(cursorSim
                    .getColumnIndex("number"));
            PhoneNo.replaceAll("\\D", "");
            PhoneNo.replaceAll("&", "");
            Name = Name.replace("|", "");

            /* 
             * add contact from phone to array phone array and total array 
             */ 

            name1.add(Name);
            phno1.add(PhoneNo);

        } 

        cursorSim.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 


     } 


@Override
public void Contacts() {
    // TODO Auto-generated method stub

}

@Override
public void setCParentFrag(Context f) {
    // TODO Auto-generated method stub
    m_fragParent = f;
}

public void setCFrag(Contact_Details conectDetail) {
    // TODO Auto-generated method stub
    m_frag = conectDetail;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

}
 }

Here is my adapter class:

 public class ContactListAdapter extends BaseAdapter implements OnCheckedChangeListener{

SparseBooleanArray mCheckStates;
Context mContext;
LayoutInflater mInflater;
 TextView tv1,tv;
 CheckBox cb;
 List<String> Name = new ArrayList<String>();
 List<String> PhoneNumber = new ArrayList<String>();



 public ContactListAdapter(Context context,List<String> name1, List<String> number1) {

    // TODO Auto-generated constructor stub
      mContext = context;
      PhoneNumber = number1;
      Name = name1;
      mCheckStates = new SparseBooleanArray(Name.size());
      mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Name.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    Holder holder = new Holder();
    if(convertView== null){
        convertView = mInflater.inflate(R.layout.custom_contact_number, null);
         holder.t1= (TextView) convertView.findViewById(R.id.tvContactName);
         holder.t2= (TextView) convertView.findViewById(R.id.tvPhoneNumber);
         holder.chkBox = (CheckBox) convertView.findViewById(R.id.checkBoxNumber);

         holder.t1.setText("Name :"+ Name.get(position) );
         holder.t2.setText("Phone No :"+PhoneNumber.get(position));

       //  holder.chkBox.setTag(position);
         holder.chkBox.setChecked(mCheckStates.get(position,false));
         holder.chkBox.setOnCheckedChangeListener(this);
         convertView.setTag(holder);

    }else{
        convertView.getTag();
    }
    return convertView;
}

public class Holder{
    TextView t1,t2;
    CheckBox chkBox;

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}

public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
} 

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);
    System.out.println("hello...........");
    notifyDataSetChanged();
} 

public void toggle(int position) {
    setChecked(position, !isChecked(position));
} 

 }

Solution

  • Change the getView() method like this

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
    
                Holder holder = null;
                if(convertView== null){
                    convertView = mInflater.inflate(R.layout.custom_contact_number, null);
                    holder=new Holder()
    
                     holder.t1= (TextView) convertView.findViewById(R.id.tvContactName);
                     holder.t2= (TextView) convertView.findViewById(R.id.tvPhoneNumber);
                     holder.chkBox = (CheckBox) convertView.findViewById(R.id.checkBoxNumber);
    
                     convertView.setTag(holder);
    
                }else{
                    holder=(Holder)convertView.getTag();
                }
    
                holder.chkBox.setChecked(mCheckStates.get(position,false));
                holder.chkBox.setOnCheckedChangeListener(this);
                holder.t1.setText("Name :"+ Name.get(position) );
                holder.t2.setText("Phone No :"+PhoneNumber.get(position));
                return convertView;
            }