小编dar*_*ryl的帖子

在Android中以编程方式创建包含自定义列表项的ListView - 无xml列表项布局

正如我在之前提出的问题中所看到的,在自定义适配器类(例如,MyAdapter扩展ArrayAdapter)中,它们总是使用膨胀的xml列表项布局.我希望做的是完全使用Java创建所有内容,而不是使用XML ...

// for example
String[] wordlist = new String[] {a, b, c};

LinearLayout list_item_layout = new LinearLayout(this);
list_item_layout.setId(5000);

TextView listText = new TextView(this);
listText.setId(5001);

listLayout.addView(listText);

ListView list = new ListView(this);

// ** QUESTION ** do I declare a programmatic .setAdapter() like this?
// ** TAKE NOTE ** I passed 'wordlist' here..
list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist));
Run Code Online (Sandbox Code Playgroud)

然后为MyAdapter ......

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
        super(context, resource, textViewResourceId, strings);
    } …
Run Code Online (Sandbox Code Playgroud)

java android listview listitem

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

java ×1

listitem ×1

listview ×1