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

2022-09-04 08:05:39

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

任何人都可以解释如何以编程方式执行此操作?


* 已编辑 *十月 9, 2012


好吧,由于我仍然陷入这个问题,我认为我已经做了一些改进,但仍然收到错误消息。改进后的代码如下所示。

//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)

答案 1

ListView扩展,而扩展又扩展视图组。因此,所有子项都将添加到 。因此,要设置布局参数,可以在为 设置 布局参数时使用 ViewGroup.LayoutParamAbsListViewAdapterViewListViewViewGrouplistLayout

或者尝试设置布局参数AbsListView.LayoutParamslistLayout

请尝试一下,并让我知道它是否有效。

以下代码工作正常。

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.LayoutParamsAbsListView.LayoutParamsgetView()

请检查并让我知道它是如何进行的。


答案 2

要添加上述答案,使用常量数字设置id不是一个好主意。

替换,替换为,listLayout.setId(5000);listLayout.setId(View.generateViewId());

如果您的目标是 API 16 及更低版本,请参阅此处。


推荐