1

来自网络开发背景,我正在寻找的东西应该很简单。我已经从安卓手机中提取了所有联系人。

他们的名字和号码在一个名为contacts(number = key)的HashMap中,我正在遍历他们,我正在寻找一个他们的列表供用户查看。密钥(电话号码)必须在那里可用但看不到,并且列表必须滚动。所以类似的东西<option value="phone">Name</option>将是完美的。我被困住了。有任何想法吗?

4

1 回答 1

1

使用适配器将列表膨胀到 ListView。

喜欢:

public class SimpleListView extends ListActivity {

private String[] lv_arr = {};
private ListView mainListView = null;
final String SETTING_TODOLIST = "todolist";

private ArrayList<String> selectedItems = new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);


    // Prepare an ArrayList of todo items
    ArrayList<String> listTODO = [INSERT THE VALUES FROM THE CONTACTS HERE];

    this.mainListView = getListView();

    // Bind the data with the list
    lv_arr = listTODO.toArray(new String[0]);
    mainListView.setAdapter(new ArrayAdapter<String>(SimpleListView.this,
            android.R.layout.simple_list_item_2, lv_arr));

}

}

您还需要一个布局:simple.xml:

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

        <ListView
            android:id="@+id/mainListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/size"
            android:layout_below="@+id/editText1"
            android:gravity="fill_vertical|fill_horizontal"
            android:horizontalSpacing="15dp"
            android:isScrollContainer="true"
            android:numColumns="1"
            android:padding="5dp"
            android:scrollbars="vertical"
            android:smoothScrollbar="true"
            android:stretchMode="columnWidth" >

</ListView>

</RelativeLayout> 
于 2012-09-13T13:51:52.667 回答