如何将联系人加载到listview正确?

时间:2017-08-30 12:49:02

标签: android listview android-contentprovider

我正在尝试将联系人加载到我的列表视图但是每次(不仅是这个项目)Cursor加载错误.Listview有一个用户从列表中选择的复选框,所有值都转到第二个活动。另一方面;我的应用程序无法识别错误的项目,并且所选项目与其电话号码不匹配。我该如何解决这个问题?

注意:我试过

if (!listtearama.contains(phonenumber)){         
   listtearama.add(phonenumber);
}

它没有用。

enter image description here

这就是我的应用程序加载错误的原因。

    public class MainActivity extends AppCompatActivity {
 ArrayList<String> selectedlist = new ArrayList<>();
    ListView chosinglist;
    Button kaydet;

    ArrayList<String> selectedlistarama = new ArrayList<>();
     ArrayList<String> listte = new ArrayList<String>();
    ArrayList<String> listtearama = new ArrayList<>();
     protected void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



         chosinglist = (ListView) findViewById(R.id.chosing);
                chosinglist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

                getNumber(this.getContentResolver());



            }

            private void getNumber(ContentResolver contentResolver) {
                Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

                if (phones != null && phones.getCount() > 0) {
                    //move to the first element, the cursor might be at an invalid location
                    phones.moveToFirst();
                    do {

                        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phonenumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        System.out.println(".................." + phonenumber);



                            listte.add(name);



                            listtearama.add(phonenumber);


                    } while (phones.moveToNext());
                    phones.close();// close cursor
                }




                final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.checkrow,
                        R.id.checkedTextView2, listte);


                kaydet.setEnabled(false);

     chosinglist.setAdapter(adapter);

            chosinglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
String selecteditem = (String) parent.getAdapter().getItem(position);

   String aramakicinliste = listtearama.get(position);



                if (selectedlistarama.contains(aramakicinliste)) {
                    selectedlistarama.remove(aramakicinliste);
                } else
                    selectedlistarama.add(aramakicinliste);


                if (selectedlist.contains(selecteditem)) {
                    selectedlist.remove(selecteditem);

                } else selectedlist.add(selecteditem);

}

1 个答案:

答案 0 :(得分:0)

您必须为适配器中的所选项目设置标记。

只需按照适配器类中的更改进行操作。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if ((convertView == null) || (convertView.getTag() == null)) {
        LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    convertView.setTag(holder);

    return convertView;
}