Android的ArrayList中的ListView / TextView的 - 你必须提供一个资源ID为一个TextView [英] Android ArrayList ListView/Textview - You must supply a resource ID for a textview

查看:220
本文介绍了Android的ArrayList中的ListView / TextView的 - 你必须提供一个资源ID为一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个点上这个工作,所以我认为我只是引用一些错误。 _regionListView似乎被返回行。我想ArrayList的内部数组是问题,因为它包含额外的空的索引,但我觉得我没有连接到ListView和TextView的正确。

任何帮助将是AP preciated。

Main.java

 字符串_region = inRegion;
    ParserRegion _parserRegion =新ParserRegion();
    InputStream的_inputStream = getResources()openRawResource(R.raw.regions)。

    //分析输入流
    _parserRegion.Parse(_inputStream,_region);

    //获取地区
    名单< PropertiesRegion> _regionList = _parserRegion.GetList();

    //创建一个ArrayAdapter
    ArrayAdapterRegion _arrayAdapter =新ArrayAdapterRegion(getApplicationContext(),R.layout.search_list,_regionList);

    //获取提到的ListView架
    ListView控件_regionListView =(ListView控件)this.findViewById(R.id.regionListView);

    //设置的ListView适配器
    _regionListView.setAdapter(_arrayAdapter);
 

Search.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:adjustViewBounds =真
    机器人:背景=@可绘制/ search_background
    机器人:重力=中心
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:scaleType =centerCrop>

    <的TextView
        机器人:ID =@ + ID / selectRegionTextView
        机器人:cacheColorHint =#00000000
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =30dp
        机器人:layout_marginLeft =10dip
        机器人:layout_marginRight =10dip
        机器人:layout_marginTop =150dip
        机器人:重力=顶
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =20dip
        机器人:TEXTSTYLE =黑体
        机器人:字体=SANS/>

    <的ListView
        机器人:ID =@ +安卓ID / regionListView
        机器人:cacheColorHint =#00000000
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_marginBottom =5dip
        机器人:layout_marginLeft =10dip
        机器人:layout_marginRight =10dip
        机器人:填充=10dp/>

    <按钮
        机器人:ID =@ + ID / mainMenuButton
        机器人:背景=@可绘制/ button_black
        机器人:layout_width =match_parent
        机器人:layout_height =50dp
        机器人:layout_marginBottom =70dip
        机器人:layout_marginLeft =20dip
        机器人:layout_marginRight =20dip
            机器人:字体=SANS
            机器人:TEXTSIZE =18dip
            机器人:TEXTSTYLE =黑体
            机器人:文字颜色=@机器人:彩色/白/>

< / LinearLayout中>
 

Search_list.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>

    <的TextView
        机器人:ID =@ + ID / regionTextView
        机器人:cacheColorHint =#00000000
        机器人:重力=中心
        机器人:layout_gravity =左
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =60dp
        机器人:以下属性来=10dip
        机器人:文字颜色=#000000
        机器人:字体=SANS
        机器人:TEXTSIZE =20dip
        机器人:TEXTSTYLE =黑体/>

< / LinearLayout中>
 

解决方案

从search_list.xml删除的LinearLayout,只使用TextView的,并把这个TextView的本身

 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 

无需LinearLayou那里,你也可以设置机器人:layout_height =WRAP_CONTENT。然后它会奏效。 :)

I had this working at one point, so I think I'm just referencing something wrong. _regionListView seems to be returning OK. I thought the array inside the ArrayList was the problem as it contained extra null indexes but I think I'm not connecting to the ListView and TextView correctly.

Any help would be appreciated.

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>

解决方案

Remove LinearLayout from search_list.xml and use only textview and put this in textview itself

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

There is no need of LinearLayou there and also you can set android:layout_height="wrap_content" .Then it will work. :)

这篇关于Android的ArrayList中的ListView / TextView的 - 你必须提供一个资源ID为一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆