2017-08-30 37 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方法使用基座適配器。 –

相關問題