2013-02-05 63 views
0

我將聯繫人加載到列表視圖與複選框現在我想返回只有檢查聯繫人的詳細信息,如名稱和電話號碼。我將如何獲取詳細信息?詳細信息存儲在列表視圖中的位置,身份證號碼等.Browow代碼是如何只在ListView中查看聯繫人的詳細信息並保留未經檢查的聯繫人?

String[] from = { "Name", "Phone","chkbox" }; 
     int[] to = { R.id.txtContactName, R.id.txtContactNumber,R.id.checkBox1 }; 
     ArrayList<Map<String,String>> list=buildData(); 
     SimpleAdapter adapter = new SimpleAdapter(this, list, 
       R.layout.main, from, to); 
     setListAdapter(adapter); 

private ArrayList<Map<String,String>> buildData() { 
     ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
     list.clear(); 
     Cursor people = getContentResolver().query(
       ContactsContract.Contacts.CONTENT_URI, null, null, null, 
       "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC"); 
     while (people.moveToNext()) { 
      String contactName = people.getString(people 
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      String contactId = people.getString(people 
        .getColumnIndex(ContactsContract.Contacts._ID)); 
      String hasPhone = people 
        .getString(people 
          .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
      if ((Integer.parseInt(hasPhone) > 0)) { 
       Cursor phones = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = " + contactId, 
         null, 
         "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME 
           + ") ASC"); 
       while (phones.moveToNext()) { 
        String phoneNumber = phones 
          .getString(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        Map<String, String> NamePhoneType = new HashMap<String, String>(); 
        NamePhoneType.put("Name", contactName); 
        NamePhoneType.put("Phone", phoneNumber); 
        list.add(NamePhoneType); 
        aa=contactName; 
        bb=phoneNumber; 
       } 
       phones.close(); 
      } 
     } 
     people.close(); 
     return list; 
    } 


@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

     super.onListItemClick(l, v, position, id); 
     CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 

     if (checkbox.isChecked() == false) { 
      checkbox.setChecked(true); 
      int aaa=l.getCheckedItemPosition(); 

     } else { 
      checkbox.setChecked(false); 
     } 

    } 

XML代碼

主要XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingBottom="5.0px" 
android:paddingLeft="5.0px" 
android:paddingTop="5.0px" > 

<TextView 
android:id="@+id/txtContactName" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginLeft="15.0dip" 
android:layout_toLeftOf="@+id/checkBox1" 
android:layout_alignParentLeft="true" 
android:text="Medium Text" 
android:textAppearance="?android:textAppearanceMedium" /> 

<TextView 
android:id="@+id/txtContactNumber" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/txtContactName" 
android:layout_alignParentLeft="true" 
android:layout_marginLeft="15.0dip" 
android:layout_toLeftOf="@+id/checkBox1" 
android:text="Small Text" 
android:textAppearance="?android:textAppearanceSmall" /> 

<CheckBox 
android:id="@+id/checkBox1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true" 
android:layout_marginRight="10dp" 
android:clickable="false" 
android:focusable="false" 
android:focusableInTouchMode="false" /> 

</RelativeLayout> 

activit_cont XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
android:id="@+id/btnShow" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Show Selected" /> 

<ListView 
android:id="@android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:clickable="true" 
android:layout_below="@+id/btnShow" > 
</ListView> 
</RelativeLayout> 
+0

您可以包含xml嗎?另外,你什麼時候調用你的'buildData'函數? –

+0

我編輯了我的代碼看看它 – zyonneo

+0

我的回答對你有幫助嗎? –

回答

0

首先,我想建議你應該擴展列表適配器的getView方法(請參閱上面的內容),並在其中對我所建議的內容進行一些修改,但只是爲了讓你放棄,在這裏是一種將這些內容合併到代碼中的方法。我肯定會推薦使用更具描述性的變量名以及:)

其間:

如果你希望所有的人的名單(和它們的數量)已在任何時間給定點檢查,維護一個單獨的實例變量是有意義的,這個變量是一個ArrayList - 類似於mCheckedContacts,它會被添加到複選框中。我會做一個叫做ContactInformation的簡單類,放入ArrayList

public class ContactInformation { 
    private String mName; 
    private String mPhoneNumber; 

    //make some getters and setters, a constructor, and an equals method! 
} 

確保您實現.equals方法,因爲下面的代碼依賴於它(參見:http://developer.android.com/reference/java/util/ArrayList.html#remove(java.lang.Object)僅供參考)。現在你可以有以下ArrayList

ArrayList<ContactInformation> mCheckedContactsInformation = new ArrayList<ContactInformation>(); 

這可以被添加到您的代碼,你可以從這個列表中添加和刪除聯繫人/。

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 
    String name = (TextView) v.findViewById(R.id.txtContactName); 
    String phoneNumber = (TextView) v.findViewById(R.id.txtContactNumber); 

    checkbox.setOnCheckedChangedListener(
    new OnCheckedChangedListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     ContactInformation curContactInfo = new ContactInformation(name, phoneNumber); 
     if (isChecked) mCheckedContactsInformation.add(curContactInfo); 
     //And this is why you need to implement a .equals method! 
     else mCheckedContactsInformation.remove(curContactInfo); 
     } 
    } 

    checkbox.setChecked(! checkbox.isChecked()); 
} 

現在您應該可以利用該ArrayList來獲取所需的信息。


編輯:根據您自己的答案,它看起來像你可能需要對一些主題進一步解釋。

:A類必須有一個構造函數。構造函數是您實例化對象的方式。每當你做出這樣的呼籲:

Object o = new Object(int thisCouldBeAnyTypeOfParameter); 

您呼叫Object的類,它看起來像這樣內的方法:

public Object(int thisCouldBeAnyTypeOfParameter) { 
    //here is some initialization code that someone wrote, or nothing... 
    //but this method NEEDS to exist! 

    //And if a parameter is passed, like the int above, then it is probably used to init something 
} 

更多信息(以及可能的一個更好的解釋)check out this info, straight from the source。所以,當你收到此錯誤:

The constructor ContactInformation(String, String) is undefined

出現這種情況有可能上線:

ContactInformation curContactInfo = new ContactInformation(name, phoneNumber); 

爲什麼? 因爲您還沒有在需要兩個字符串的類中創建構造函數。你得到的錯誤實際上幾乎就是發生的問題。我會建議你谷歌這些類型的錯誤,並花時間閱讀你找到的結果。不幸的是,這些東西需要時間來學習,雖然有些人會幫助你,但大部分工作必須通過來完成,你只需花費數小時閱讀和編碼,閱讀和編​​碼。

你得到下一個錯誤:

Type mismatch: cannot convert from TextView to String

想想的代碼行,這是發生:

String name = (TextView) v.findViewById(R.id.txtContactName); 

左手邊是一個字符串,並期望一個字符串被退回。在右側,你沒有返回一個字符串。對於這類問題,查看Android文檔很有幫助。例如,findViewById()函數...它返回什麼?您正在將結果類型(它是View)投射到TextView,該對象是TextView類型的對象。這不是字符串,因此您會收到錯誤消息,因爲您正嘗試將TextView對象指定給String。這是合理的,這是行不通的,因爲它們是不同的類型。在這種情況下,您需要做一些額外的工作才能將String(存儲在TextView對象中)移出對象,以便將其分配給String。我將爲你確定這些額外的工作,但要做一個谷歌搜索「textview android」,點擊第一個鏈接,並查看可以在TextView上調用的所有方法。我敢打賭,你會發現一個返回字符串:)

希望這可以幫助。對不起,這麼長時間囉嗦/教誨,但我只是想幫助。

-1
 String[] from = { "Name", "Phone","chkbox" }; 
     int[] to = { R.id.txtContactName, R.id.txtContactNumber,R.id.checkBox1 }; 
     ArrayList<Map<String,String>> list=buildData(); 
     SimpleAdapter adapter = new SimpleAdapter(this, list, 
       R.layout.main, from, to); 
     setListAdapter(adapter); 




    } 

    private ArrayList<Map<String,String>> buildData() { 
     ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
     list.clear(); 
     Cursor people = getContentResolver().query(
       ContactsContract.Contacts.CONTENT_URI, null, null, null, 
       "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC"); 
     while (people.moveToNext()) { 
      String contactName = people.getString(people 
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      String contactId = people.getString(people 
        .getColumnIndex(ContactsContract.Contacts._ID)); 
      String hasPhone = people 
        .getString(people 
          .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
      if ((Integer.parseInt(hasPhone) > 0)) { 
       Cursor phones = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = " + contactId, 
         null, 
         "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME 
           + ") ASC"); 
       while (phones.moveToNext()) { 
        String phoneNumber = phones 
          .getString(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        Map<String, String> NamePhoneType = new HashMap<String, String>(); 
        NamePhoneType.put("Name", contactName); 
        NamePhoneType.put("Phone", phoneNumber); 
        list.add(NamePhoneType); 
        aa=contactName; 
        bb=phoneNumber; 
       } 
       phones.close(); 
      } 
     } 
     people.close(); 
     return list; 
    } 

    ArrayList<ContactInformation> mCheckedContactsInformation = new ArrayList<ContactInformation>(); 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

     super.onListItemClick(l, v, position, id); 


     CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 
     // String name = (TextView) v.findViewById(R.id.txtContactName); 
     TextView tvv1=(TextView) findViewById(R.id.txtContactName); 
     TextView tvv2=(TextView) findViewById(R.id.txtContactNumber); 
     final String name=tvv1.getText().toString(); 
     final String phoneNumber=tvv2.getText().toString(); 

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    ContactInformation curContactInfo = new ContactInformation(name, phoneNumber); 
    if (isChecked) mCheckedContactsInformation.add(curContactInfo); 
    //And this is why you need to implement a .equals method! 
    else mCheckedContactsInformation.remove(curContactInfo); 

} 
}); 

Contactinformation類代碼

private String name; 
private String phoneNo; 
//private boolean selected; 

public String getName() { 
    return name; 

} 
public void setName(String name) { 
    this.name = name; 
    //selected = false; 
} 
public String getPhoneNo() { 
    return phoneNo; 
} 
public void setPhoneNo(String phoneNo) { 
    this.phoneNo = phoneNo; 
} 

/*public boolean isSelected() { 
    return selected; 
    } 

    public void setSelected(boolean selected) { 
    this.selected = selected; 
    }*/ 

我在這裏得到的錯誤-The構造ContactInformation(字符串,字符串)是undefined..also當我使用String name = (TextView) v.findViewById(R.id.txtContactName); 我得到的錯誤 - 類型不匹配:不能從TextView轉換爲字符串...我也可以使用布爾選擇的方法,我已經評論,而不是你建議的.equals方法?

相關問題