在 Android 中以编程方式创建带有自定义列表项的 ListView - 没有 xml 列表项布局
Posted
技术标签:
【中文标题】在 Android 中以编程方式创建带有自定义列表项的 ListView - 没有 xml 列表项布局【英文标题】:Creating a ListView with custom list items programmatically in Android - no xml list item layout 【发布时间】:2012-09-28 21:05:52 【问题描述】:正如我在之前提出的问题中看到的那样,在自定义适配器类(例如,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));
然后对于 MyAdapter...
private class MyAdapter extends ArrayAdapter<String>
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings)
super(context, resource, textViewResourceId, strings);
@Override
public View getView(int position, View convertView, ViewGroup parent)
View v = convertView; //is this the list_item_layout that I passed??
TextView tv = (TextView) v.findViewById(5001);
// ** QUESTION ** do I pass 'wordlist' again here?
tv.setText( wordlist[position] );
return v;
当我在我的设备上运行它时会发生以下错误...
10-08 23:11:19.775: E/androidRuntime(18276): FATAL EXCEPTION: main
10-08 23:11:19.775: E/AndroidRuntime(18276): android.content.res.Resources$NotFoundException: String resource ID #0x0
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.content.res.Resources.getText(Resources.java:222)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.widget.TextView.setText(TextView.java:3011)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.turista.client.TuristaClientMain.onClick(TuristaClientMain.java:113)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View.performClick(View.java:2538)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View$PerformClick.run(View.java:9152)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.handleCallback(Handler.java:587)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Looper.loop(Looper.java:123)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-08 23:11:19.775: E/AndroidRuntime(18276): at dalvik.system.NativeStart.main(Native Method)
谁能解释一下如何以编程方式做到这一点?
* 已编辑 * 2012 年 10 月 9 日
好的,由于我仍然陷入这个问题,我想我已经做了一些改进,但仍然收到错误消息。改进后的代码如下..
//wordlist is a global variable
String[] wordlist = new String[] a, b, c;
// ..
// .. inside onCreate...
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, R.layout.listitem, R.id.mLargeTextView, wordlist));
// since the ArrayAdapter class needs XML parameters to inflate, I created a dummy layout
现在在 MyAdapter 类中..
private class MyAdapter extends ArrayAdapter<String>
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings)
super(context, resource, textViewResourceId, strings);
@Override
public View getView(int position, View convertView, ViewGroup parent)
LinearLayout listLayout = new LinearLayout(Main.this);
listLayout.setLayoutParams(new LayoutParams(wrapContent, wrapContent));
listLayout.setId(5000);
TextView listText = new TextView(Main.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(wordlist[position]);
return listLayout;
如您所见,我认为我必须重写 getView 才能显示我的自定义视图,所以这就是我所做的。不幸的是,我想我误解了它。以下是错误..
10-09 09:24:10.095: E/AndroidRuntime(2517): FATAL EXCEPTION: main
10-09 09:24:10.095: E/AndroidRuntime(2517): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureScrapChild(ListView.java:1183)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureHeightOfChildren(ListView.java:1266)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.onMeasure(ListView.java:1175)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-09 09:24:10.095: E/AndroidRuntime(2517): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
我能问一下您为什么要使用这种编程风格而不是使用 XML 吗? 您已经设置了 id 为什么要找到具有该 id 的新 TextView?使用 tv.getID()。 @Kumar 你是什么意思?我使用了 v.findViewById(5001)。 @Zerkz 我希望创建动态内容 尝试删除这一行 listLayout.setLayoutParams(new LayoutParams(wrapContent, wrapContent)); 【参考方案1】:ListView
扩展 AbsListView
进而扩展 AdapterView
扩展视图组。所以ListView
的所有孩子都将被添加到ViewGroup
。所以要设置布局参数,你可以
在为listLayout
设置布局参数时使用ViewGroup.LayoutParam。
或尝试AbsListView.LayoutParams
为listLayout
设置布局参数
请尝试一下,如果有效,请告诉我。
以下代码运行良好..
public class MainActivity1 extends Activity
String[] wordlist = new String[] "a", "b", "c" ;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, wordlist));
setContentView(list);
private class MyAdapter extends ArrayAdapter<String>
public MyAdapter(Context context, String[] strings)
super(context, -1, -1, strings);
@Override
public View getView(int position, View convertView, ViewGroup parent)
LinearLayout listLayout = new LinearLayout(MainActivity1.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(MainActivity1.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
问题在于默认情况下 Eclipse 导入 ViewGroup.LayoutParams
与 AbsListView.LayoutParams
不兼容,AbsListView.LayoutParams
需要用于设置从 getView()
方法返回的视图的布局参数。
请检查并告诉我进展如何..
【讨论】:
刚刚用 AbsListView.LayoutParams 对其进行了测试,它工作正常。相应地更新了我的答案.. 哇它好用。我不知道在这个 getView() 方法中 LayoutParams 的处理方式不同,并且严重依赖 Eclipse 的自动导入并不是一件好事。 谢谢。我几乎放弃了以编程方式完成所有事情的想法。最后一个问题,我可以看到您在 onCreate 中的 setAdapter(new MyAdapter()) 中传递了数组“wordlist”。那么这意味着它必须已经在 MyAdapter 的某个地方......如果没有全局声明“wordlist”,你怎么称呼它?再次感谢 Praful。你完全搞定了。 那只是为了检查错误。由于我们对 'ArrayAdaptor' 进行子类化,我们可以使用 'super.getItem(position)' 来获取该位置的字符串值。我已经更新了使用这个的答案。 @PrafulBhatnagar 你能看看***.com/questions/28040288/…【参考方案2】:为了补充上述答案,将 id 设置为常数并不是一个好主意。
将listLayout.setId(5000);
替换为listLayout.setId(View.generateViewId());
如果您的目标是 API 16 及以下版本,请参考 this.
【讨论】:
以上是关于在 Android 中以编程方式创建带有自定义列表项的 ListView - 没有 xml 列表项布局的主要内容,如果未能解决你的问题,请参考以下文章