2017-08-30 38 views
0

我想将联系人加载到我的列表视图,但每次(不仅此项目)光标加载错误。列表视图有一个复选框,用户从列表中选择,所有值转到第二个活动。我的应用程序不能识别错误的项目和所选项目不要“T匹配其电话号码,我怎样才能解决这个重复如何将联系人加载到列表视图是否正确?

注:?我想

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

} 
+0

您是否尝试过适配器的notifyDataSetChanged()方法? – Danger

+0

是的,我现在试过了,它在第一个活动中不起作用 –

+0

你如何更新适配器?通常情况下,流程如下:首先更新ArrayList,然后使用该ArrayList更新适配器,然后将适配器设置为你的ListView。notifyDataSetChanged()被使用,如果Arraylist的元素有任何变化,Adapter会得到通知并且会更新ListView。 – Danger

回答

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

我不使用baseadapter.I使用arrayadapter.These方法使用基座适配器。 –

相关问题