簡體   English   中英

Android ArrayList ListView / Textview-您必須為textview提供資源ID

[英]Android ArrayList ListView/Textview - You must supply a resource ID for a textview

我曾經在某一時刻進行過這項工作,所以我認為我只是在引用錯誤的內容。 _regionListView似乎返回確定。 我以為ArrayList中的數組是問題所在,因為它包含額外的空索引,但我認為我沒有正確連接到ListView和TextView。

任何幫助,將不勝感激。

Main.java

String _region = inRegion;
    ParserRegion _parserRegion = new ParserRegion();
    InputStream _inputStream = getResources().openRawResource(R.raw.regions);

    // Parse the Input Stream
    _parserRegion.Parse(_inputStream, _region);

    // Get Regions
    List<PropertiesRegion> _regionList = _parserRegion.GetList();

    // Create the ArrayAdapter
    ArrayAdapterRegion _arrayAdapter = new ArrayAdapterRegion(getApplicationContext(), R.layout.search_list, _regionList);

    // Get reference to ListView holder
    ListView _regionListView = (ListView) this.findViewById(R.id.regionListView);

    // Set the ListView adapter
    _regionListView.setAdapter(_arrayAdapter);

Search.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:adjustViewBounds="true"
    android:background="@drawable/search_background"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:scaleType="centerCrop" >

    <TextView
        android:id="@+id/selectRegionTextView"
        android:cacheColorHint="#00000000"
        android:layout_width="wrap_content"
        android:layout_height="30dp" 
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="150dip"
        android:gravity="top"
        android:textColor="#000000"
        android:textSize="20dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <ListView 
        android:id="@+android:id/regionListView"
        android:cacheColorHint="#00000000" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:padding="10dp" />

    <Button
        android:id="@+id/mainMenuButton"
        android:background="@drawable/button_black"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="70dip"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
            android:typeface="sans"
            android:textSize="18dip"
            android:textStyle="bold"
            android:textColor="@android:color/white"/>

</LinearLayout>

Search_list.xml

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

    <TextView
        android:id="@+id/regionTextView"
        android:cacheColorHint="#00000000"
        android:gravity="center"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="60dp" 
        android:paddingLeft="10dip"
        android:textColor="#000000"
        android:typeface="sans"
        android:textSize="20dip"
        android:textStyle="bold"/>

</LinearLayout>

像這樣寫你的search_list.xml

<?xml version="1.0" encoding="utf-8"?>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:cacheColorHint="#00000000"
        android:gravity="center"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="60dp" 
        android:paddingLeft="10dip"
        android:textColor="#000000"
        android:typeface="sans"
        android:textSize="20dip"
        android:textStyle="bold"/>

從search_list.xml中刪除LinearLayout並僅使用textview並將其放入textview本身

xmlns:android="http://schemas.android.com/apk/res/android"

那里不需要LinearLayou,也可以設置android:layout_height =“ wrap_content” 。然后它將起作用。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM