【问题标题】:create an Android Scroll List programmatically以编程方式创建 Android 滚动列表
【发布时间】:2012-09-06 15:10:34
【问题描述】:

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

他们的姓名和号码在名为contacts (number = key) 的HashMap 中。我正在遍历它们,并希望创建一个列表供用户查看。密钥(电话号码)必须在那里可用但看不到,并且列表必须滚动。所以像<option value="phone">Name</option> 这样的东西会很完美。我被困住了。有什么想法吗?

【问题讨论】:

标签: java android hashmap iteration


【解决方案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));

}

}

您还需要一个布局: 简单的.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-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多