正如我在之前提出的问题中所看到的,在自定义适配器类(例如,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)