IT

“ArrayAdapter를 사용하려면 리소스 ID가 TextView 여야합니다.”xml 문제

lottoking 2020. 5. 27. 07:51
반응형

“ArrayAdapter를 사용하려면 리소스 ID가 TextView 여야합니다.”xml 문제


표시 ListView하려는 파일 (텍스트 파일) 을 표시하도록보기를 설정하려고 할 때 오류가 발생 합니다. XML과 관련이 있다고 확신합니다. 의 정보를 표시하고 싶습니다 this.file = fileop.ReadFileAsList("Installed_packages.txt");. 내 코드 :

public class Main extends Activity {
    private TextView tv;
    private FileOperations fileop;
    private String[] file;

    /** Called when the activity is first created. */       
    @Override
    public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState); 
        this.fileop = new FileOperations(); 
        this.file = fileop.ReadFileAsList("Installed_packages.txt"); 
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);
        ListView lv = new ListView(this);
        lv.setTextFilterEnabled(true); 
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file)); 
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

              public void onItemClick(AdapterView<?> parent, View view,     int position, long id) { 
                    // When clicked, show a toast with the TextView text 
                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
              } 
        });         
        setContentView(lv);
    }

}

list_item.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" 
    android:padding="10dp"   
    android:textSize="16sp"   
    android:textColor="#000">

</LinearLayout>

main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="true">
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="5sp"
        android:id="@+id/TextView01"
        android:text="@string/hello"/>
    </ScrollView>

</LinearLayout>

ArrayAdapter와는 텍스트 뷰의 XML로 자원 ID가 필요 당신이 무엇을 제공하지 않는 예외 수단 ArrayAdapter이 기대합니다. 이 생성자를 사용할 때 :

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)

R.Layout.a_layout_file만 포함하는 XML 레이아웃 파일의 ID 여야 TextView합니다 (이 TextView 는 A, 다른 레이아웃으로 감싸 LinearLayout, RelativeLayout이런! 등), 뭔가를 :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    // other attributes of the TextView
/>

If you want your list row layout to be something a little different then a simple TextView widget use this constructor:

new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)

where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.


Soution is here

listitem.xml

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

     <TextView
         android:id="@+id/textview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >
     </TextView>
</LinearLayout>

Java code :

 String[] countryArray = {"India", "Pakistan", "USA", "UK"};
 ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
 ListView listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(adapter);

If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:

    //Pass in the resource id:  R.id.text_view
    SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
            R.id.text_view,
            new ArrayList<>());

Adapter:

public class SpinnerAdapter extends ArrayAdapter<MyEntity> {

    private Context context;
    private List<MyEntity> values;

    public SpinnerAdapter(Context context, int textViewResourceId,
                          List<MyEntity> values) {

        //Pass in the resource id:  R.id.text_view
        super(context, textViewResourceId, values);

        this.context = context;
        this.values = values;
    }

참고URL : https://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems

반응형