2012-06-08 35 views
0

我正在为Android做一个应用程序,如果我点击一个“Generate”按钮,它会生成一个列表,每行都有一个图像,textView和2个按钮。列表的数量是随机的,超过30个。使用布局inflater,我想添加一个子视图到main.xml布局。但是当我点击'生成'按钮时,发生Logcat错误。Logcar错误:AdapterView不支持addView(查看)

五月六日至8日:33:19.340:E/AndroidRuntime(853):java.lang.UnsupportedOperationException:addView(查看)不支持的适配器视图

我不明白什么是“ 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> 

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

感谢一切先进的!

回答

2

一个AdapterView是一个视图,其子视频由Adapter决定。 ListViewAdapterView的一个子类,由ListAdapter填充。您可以查看"Hello, Views: List View"教程,了解如何使用ListView

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

+0

谢谢Rajesh,但我可以使用哪种类型的listView?实际上在主屏幕上没有列表之前点击“生成”按钮。而add.xml有3种类型的视图。我不知道如何创建ListAdapter。 – yoori

+0

有很多关于使用自定义适配器的教程(如[this](http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter)和[这](http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/)),搜索,你会发现。您可以使用'images'数组作为适配器的后备存储,并将其填充到“生成”按钮的'onClick'中。做你的研究并尝试一些事情。 – Rajesh

相关问题