2012-06-08 1 views
0

Android 용 응용 프로그램을 만들고 있고 "Generate"버튼을 클릭하면 각 행에 이미지, textView 및 2 개의 버튼이있는 목록이 생성됩니다. 목록의 수는 무작위이며 30 개를 초과합니다. 레이아웃 인플레이터를 사용하여 main.xml 레이아웃에 하위보기를 추가하려고합니다. 하지만 '생성'버튼을 클릭하면 로그캣 오류가 발생합니다.Logcar Error : addView (View)가 AdapterView에서 지원되지 않습니다.

06-08 05 : 33 : 19.340 : E/AndroidRuntime (853) : java.lang.UnsupportedOperationException가 : addView (보기)

내가 무엇인지 이해하지 못하는 어댑터 뷰 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> 

자세히 이해할 수 있도록 설명해주십시오. (저는 진짜 프로그래머입니다)

진보 된 모든 것에 감사드립니다!

답변

2

은 자식이 Adapter에 의해 결정되는보기입니다. ListView은 으로 채워지는 AdapterView의 하위 클래스입니다. ListView을 사용하는 방법을 이해하려면 "Hello, Views: List View" 자습서를 살펴보세요.

경우에 따라 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) 및 [this] (http://www.softwarepassion.com/android-series-custom-listview-items-and-apters/))를 검색하면 찾을 수 있습니다. 'images' 배열을 어댑터의 지원 저장소로 사용하고 "generate"버튼의'onClick'에 그것을 채울 수 있습니다. 당신의 연구를하고 사물을 시험해보십시오. – Rajesh