0

我正在为 Android 做一个应用程序,如果我单击“生成”按钮,它会生成一个列表,其中每一行都有一个图像、文本视图和 2 个按钮。列表的数量是随机的,超过 30 个。使用布局充气器,我想在 main.xml 布局中添加一个子视图。但是当我单击“生成”按钮时,会发生 Logcat 错误。

06-08 05:33:19.340: E/AndroidRuntime(853): java.lang.UnsupportedOperationException: addView(View) 在 AdapterView 中不受支持

我不明白什么是“AdapterView”以及如何解决这个错误。

这是我的代码。

package com.android.listtest1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListTest1Activity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    int[] images = new int[50];
    TextView tv1, tv2;
    Button btn_gen, btn_clear;
    Button btn_phone, btn_sms;
    ImageView img;
    ArrayAdapter<String> list;
    LinearLayout new_linear;
    ListView listview;
    Intent intent1;
    int list_cnt = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listview = (ListView)findViewById(R.id.listView1);
        btn_gen = (Button)findViewById(R.id.btn_gen);
        btn_clear = (Button)findViewById(R.id.btn_clear);

        btn_gen.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        if( v == btn_gen){

            int i, j, rand, cnt_rand;
            for(i=0;i<50;i++){
                images[i] = i;
            }
            images[0] = R.drawable.c1992_1;
            images[1] = R.drawable.c1992_2;
            images[2] = R.drawable.c1993;
            images[3] = R.drawable.c1994;
            images[4] = R.drawable.c1995_1;
            images[5] = R.drawable.c1995_2;
            images[6] = R.drawable.c1996_1;
            images[7] = R.drawable.c1996_2;
            images[8] = R.drawable.c1998;
            images[9] = R.drawable.c2000;
            images[10] = R.drawable.c2001;
            images[11] = R.drawable.c2002;
            images[12] = R.drawable.c2004;
            images[13] = R.drawable.c2005;
            images[14] = R.drawable.c2007_1;
            images[15] = R.drawable.c2008;
            images[16] = R.drawable.c2009_1;
            images[17] = R.drawable.c2009_2;
            images[18] = R.drawable.c2009_3;
            images[19] = R.drawable.c2010_1;
            images[20] = R.drawable.d2005_1;
            images[21] = R.drawable.d2005_2;
            images[22] = R.drawable.d2010_2;
            images[23] = R.drawable.d2010_3;
            images[24] = R.drawable.d2010_4;
            images[25] = R.drawable.i01;
            images[26] = R.drawable.i2010_1;
            images[27] = R.drawable.alien_1;
            images[28] = R.drawable.alien_2;
            images[29] = R.drawable.balloon;
            images[30] = R.drawable.bear;
            images[31] = R.drawable.beaver;
            images[32] = R.drawable.birthdaycake;
            images[33] = R.drawable.chocolatecake;
            images[34] = R.drawable.david;
            images[35] = R.drawable.davinci;
            images[36] = R.drawable.dragon;
            images[37] = R.drawable.earth;
            images[38] = R.drawable.fireworks1;
            images[39] = R.drawable.fireworks2;
            images[40] = R.drawable.fish;
            images[41] = R.drawable.frog1;
            images[42] = R.drawable.frog2;
            images[43] = R.drawable.hand;
            images[44] = R.drawable.leaf;
            images[45] = R.drawable.monkey1;
            images[46] = R.drawable.women;
            images[47] = R.drawable.sun;
            images[48] = R.drawable.rose;
            images[49] = R.drawable.rabbit;

            String str_name, str_desc;          
            LayoutInflater inflater = getLayoutInflater();
            new_linear = (LinearLayout)inflater.inflate(R.layout.list, null);           
            img = (ImageView)new_linear.findViewById(R.id.img);
            tv1 = (TextView)new_linear.findViewById(R.id.name);
            tv2 = (TextView)new_linear.findViewById(R.id.desc);
            str_name = tv1.getText().toString();
            str_desc = tv2.getText().toString();    
            //List<Integer> list = new ArrayList<Integer>();
            for(i = 0; i < 50; )
            {
                rand = ((int)(Math.random() * 50)) + 1;             

                cnt_rand = ((int)(Math.random() * 100)) + 30;
                for(j = 0; j < cnt_rand; j++){
                    img.setImageResource(images[rand]);
                    tv1.setText(str_name+Integer.toString(j));
                    tv2.setText(str_desc+Integer.toString(j));
                    listview.addView(new_linear);
                }
            }

        }else if( v == btn_clear){

        }
    }
}

这是 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_info"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/list_info"

        android:layout_gravity="center" android:textSize="20dp"/>
    <ListView
        android:id="@+id/listView1
        android:layout_width="fill_parent"
        android:layout_height="0dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_gen"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/btn_gen" 
            android:layout_marginLeft="30dp" 
            android:layout_marginRight="20dp"/> 

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/btn_clear" 
            android:layout_marginLeft="20dp"/>
    </LinearLayout>

</LinearLayout>    

这是 add.xml 我要添加为列表的每一行的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/c1992_1" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"             
            android:layout_marginTop="30dp" 
            android:textSize="25dp"
            android:text="11"
        />

        <TextView
            android:id="@+id/desc"
            android:layout_width="120dp"
            android:layout_height="wrap_content"            
            android:layout_marginTop="20dp" 
            android:textSize="20dp"
            android:text="11"
        />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btn_phone"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/phone" android:layout_marginTop="25dp"/>
        <Button
            android:id="@+id/btn_sms"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/sms" />
    </LinearLayout>

</LinearLayout> 

请详细解释一下,我能理解。(我是真正的第一个程序员)

感谢您提供的一切!

4

1 回答 1

2

AnAdapterView是一个视图,其子级由 确定AdapterListView是它的子类,由AdapterViewa 填充ListAdapter。您可以查看“Hello, Views: List View”教程来了解如何使用ListView.

在您的情况下,您需要创建一个自定义适配器,它将覆盖该getView方法并返回包含膨胀 add.xml 实例的自定义视图。

于 2012-06-08T08:42:38.887 回答