2017-08-29 61 views
1

我想訪問帶有名稱的電話號碼。但是當我這樣做時,我的通話不符合我選擇的號碼。我有一個帶有複選框的列表視圖。用戶的選擇名稱並且這些名字轉到次數第二個活動。我的問題是第二個活動中的數字和名字選擇在第一個活動。當我打電話給任何名字,我的應用程序調用不同的數字。我可以如何解決它?聯繫人名稱不匹配在列表視圖中的數字

 ArrayList<String> listte = new ArrayList<String>(); 

     ArrayList<String> selectedlist = new ArrayList<>(); 
     ListView chosinglist; 
     Button kaydet; 
     ArrayList<String> listtearama = new ArrayList<>(); 
     ArrayList<String> selectedlistarama = new ArrayList<>(); 
     @Override 
      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); 
       while (phones.moveToNext()) { 

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


        if (!listte.contains(name)){ // it doesn"t work.But if It doesn"t exist, listview is repeating yourself. 
        listte.add(name); 


        } 

        if (!listtearama.contains(phonenumber)){// it doesn"t work.But if It doesn"t exist, listview is repeating yourself. 
         listtearama.add(phonenumber); } 

       } 
       phones.close();// close cursor 

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


    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); 
       int arkaplandakinumaraposition = parent.getPositionForView(view); 


       String aramakicinliste = listtearama.get(arkaplandakinumaraposition); 


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


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

       } else selectedlist.add(selecteditem); 

enter image description here

+0

爲您的問題添加一個android,java標籤,以供更多人表現出興趣。此外,你的代碼不是很具描述性,所以你很難掌握你在做什麼,嘗試使用描述性變量名稱或評論 –

+0

好的我添加了android標籤。 –

+0

ok.I編輯我的codo一點點。如果你是thinging.just說 –

回答

0
ArrayList<String> listte = new ArrayList<String>(); 

    ArrayList<String> selectedlist = new ArrayList<>(); 
    ListView chosinglist; 
    Button kaydet; 
    ArrayList<String> listtearama = new ArrayList<>(); 
    ArrayList<String> selectedlistarama = new ArrayList<>(); 

    @Override 
    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); 
     //first of all, check if the cursor has data 
     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); 


       if (!listte.contains(name)) { // it doesn"t work.But It doesn"t exist listview repeating yourself. 
        listte.add(name); 
       } 

       if (!listtearama.contains(phonenumber)) {// it doesn"t work.But It doesn"t exist listview repeating yourself. 
        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); 


     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); 
       //int arkaplandakinumaraposition = parent.getPositionForView(view); irrelevant to me 


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

     }); 
    } 

    /*From what I see, it seems you rather need to implement multiple selection because the checkbox 
    * has an onclick event of its own which is different from the one for the entire list item*/ 

此更新後的答案可以確保所選擇的項目是正確的,你的傳送數據的實施將是現在的決定因素。

+0

它不工作。程序仍然調用不同的數字 –

+0

看起來你是點擊複選框,而不是實際列表項目,我會盡快寫下你想要做的事情的完整實現,以便你可以進行比較。 –

相關問題