13

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

2 回答 2

22

ListViewextends AbsListViewwhich 反过来又扩展AdapterView了视图组。所以所有的孩子ListView都会被添加到ViewGroup. 因此,要设置布局参数,您可以在设置布局参数时使用ViewGroup.LayoutParamlistLayout

或尝试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.LayoutParamsAbsListView.LayoutParams需要用于设置从getView()方法返回的视图的布局参数不兼容的导入。

请检查并告诉我进展如何..

于 2012-10-09T08:05:14.817 回答
2

要添加到上述答案,将 id 设置为常数并不是一个好主意。

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

如果您的目标是 API 16 及以下版本,请参阅此。

于 2015-03-23T19:35:21.633 回答