2010-10-07 63 views
5

我有我從Android ContactManager示例中獲得的聯繫人列表視圖。此列表顯示正常,但我無法弄清楚如何從所選項目獲取信息,如「姓名」和「電話號碼」。getItemAtPosition()如何從列表視圖中的選定項目獲取可讀數據

我可以得到選定的位置,但mContactList.getItemAtPosition(position)的結果是ContentResolver $ CursorWrapperInner,這對我來說並沒有任何意義。我無法從中得到正面或反面。

任何人都知道我可以從列表視圖中選擇的項目獲取名稱/編號/電話號碼?

這是我的代碼。

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    Log.v(TAG, "Activity State: onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.choose_contact); 

    // Obtain handles to UI objects 
    mAddAccountButton = (Button) findViewById(R.id.addContactButton); 
    mContactList = (ListView) findViewById(R.id.contactList); 
    mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible); 

    // Initialize class properties 
    mShowInvisible = false; 
    mShowInvisibleControl.setChecked(mShowInvisible); 
    mContactList.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     { 
     addContactAt(position); 
     } 
    }); 
    mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Log.d(TAG, "mShowInvisibleControl changed: " + isChecked); 
      mShowInvisible = isChecked; 
      populateContactList(); 
     } 
    }); 

    // Populate the contact list 
    populateContactList(); 

} 

/** 
* Populate the contact list based on account currently selected in the account spinner. 
*/ 
private SimpleCursorAdapter adapter; 
private void populateContactList() { 
    // Build adapter with contact entries 
    Cursor cursor = getContacts(); 
    String[] fields = new String[] { 
      ContactsContract.Data.DISPLAY_NAME 
    }; 
    adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor, 
      fields, new int[] {R.id.contactEntryText}); 
    mContactList.setAdapter(adapter); 
} 

/** 
* Obtains the contact list for the currently selected account. 
* 
* @return A cursor for for accessing the contact list. 
*/ 
private Cursor getContacts() 
{ 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME 
    }; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 
      (mShowInvisible ? "0" : "1") + "'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
} 

private void addContactAt(int position) 
{ 
Object o = mContactList.getItemAtPosition(position); 
} 

}`

回答

5

BOOM!我想通了。基本上你可以從點擊事件中獲得位置編號,然後在我的addContatAt()中使用該位置在光標內搜索所需的字段。在我的情況下,我想顯示名稱。

我已經習慣了在Flex中做的事情,所以對我來說:)這光標企業是不同的

不管怎麼說,別人這裏是我的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    Log.v(TAG, "Activity State: onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.choose_contact); 

    // Obtain handles to UI objects 
    mAddAccountButton = (Button) findViewById(R.id.addContactButton); 
    mContactList = (ListView) findViewById(R.id.contactList); 
    mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible); 

    // Initialize class properties 
    mShowInvisible = false; 
    mShowInvisibleControl.setChecked(mShowInvisible); 
    mContactList.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     { 
      addContactAt(position); 
     } 
    }); 
    mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Log.d(TAG, "mShowInvisibleControl changed: " + isChecked); 
      mShowInvisible = isChecked; 
      populateContactList(); 
     } 
    }); 

    // Populate the contact list 
    populateContactList(); 

} 

/** 
* Populate the contact list based on account currently selected in the account spinner. 
*/ 
private SimpleCursorAdapter adapter; 
private void populateContactList() { 
    // Build adapter with contact entries 
    contactsCursor = getContacts(); 
    String[] fields = new String[] { 
      ContactsContract.Data.DISPLAY_NAME 
    }; 
    adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, contactsCursor, 
      fields, new int[] {R.id.contactEntryText}); 
    mContactList.setAdapter(adapter); 
} 

/** 
* Obtains the contact list for the currently selected account. 
* 
* @return A cursor for for accessing the contact list. 
*/ 
private Cursor getContacts() 
{ 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME 
    }; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 
      (mShowInvisible ? "0" : "1") + "'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
} 

private void addContactAt(int position) 
{ 
    contactsCursor.moveToPosition(position); 
    String name = contactsCursor.getString(
      contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
} 
} 
4

嗯 - 你搞亂將AdapterView的光標放在背後,這可能並不總是一個好主意。另一種方法是在onItemClick處理程序中調用parent.getItemAtPosition(position)並將結果轉換爲Cursor;它將指向與被點擊的項目對應的行。

+0

這將返回完全相同的'Cursor'對象 – Vasiliy 2015-05-31 12:22:29

14
@Override 
protected void onListItemClick(ListView l, View v, int position, long ida) { 
    super.onListItemClick(l, v, position, ida); 

    Cursor mycursor = (Cursor) getListView().getItemAtPosition(position); 
    showToast("mycursor.getString(1) " + mycursor.getString(1) +" "); 
+4

我知道這是在文檔中 - [ListView控件(HTTP: //developer.android.com/reference/android/app/ListFragment.html) - 但我不明白爲什麼我們應該在相關ListView作爲參數提供時使用'getListView()'。 – losttime 2013-04-02 01:39:23

+0

確實。如果我用'l'替換'getListView()',它似乎對我很好。 – 2014-10-28 20:26:54

4
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{ 
Map<String, Object> map = (Map<String, Object>)_productListView.getItemAtPosition(position); 
String _productCode = (String) map.get("ProductCode"); 
String _productName = (String) map.get("ProjectName"); 
Double _price = (Double) map.get("Price"); 
} 
0

我用下面的代碼由三木Habryn提到

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Cursor client = (Cursor)parent.getItemAtPosition(position); 
    String client_name = client.getString(2); // third column in db 
    Toast.makeText(getBaseContext(), client_name, Toast.LENGTH_SHORT).show(); 
} 
相關問題