1
Cursor c = managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,cols,views);
listview.setAdapter(adapter);

我正在使用此代码段将我ListView的与Cursor.

我想问什么

String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};

确实如此?

并请解释构造函数所需的参数SimpleCursorAdapter

4

2 回答 2

0

list_item.xml参考这个链接

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

HelloListView.java

public class HelloListView extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, getNames()));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });
}
private ArrayList<String> getNames(){
        ArrayList<String> namesArray = new ArrayList<String>();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
        Cursor names = getContentResolver().query(uri, projection, null, null, null);

        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        names.moveToFirst();
        do {
            namesArray.add(names.getString(indexName));
         } while (names.moveToNext());
       return namesArray;
    }
}
于 2012-05-27T09:59:00.833 回答
0

它是一张地图,告诉适配器使用光标中的哪些列来填充布局中的哪些小部件。

它们按给定的顺序使用。from 数组中列出的第一列中的数据(称为 cols)将进入 to 数组中列出的第一个 id(称为 views),依此类推。

其他参数是包含您在 to 数组中指定的视图 ID 的布局和包含要在数组中使用的数据的光标。

于 2012-05-27T15:03:13.453 回答