错误:ArrayAdapter需要的资源ID是一个TextView [英] Error: ArrayAdapter requires the resource ID to be a TextView

查看:172
本文介绍了错误:ArrayAdapter需要的资源ID是一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思?我一直在争取这个的ListView ,我已经使用了 ArrayAdapter 。我写了这个漂亮的对话框片段,我已经回调到的updateUI听者的原因我想更新这个的ListView 我从 DialogFragment <我的片段/ code>起初 ArrayAdapter 是一个复杂类型的类我创建的:

  ArrayAdapter&LT;地点&gt; theLocations;
...
//在oncreateview
theLocations =新的ArrayAdapter&LT;地点&gt;(mContext,R.layout.location_row,地点);
//这里的位置是一个ArrayList&LT;地点&gt;
 

然后,我有:

 公共无效onUIUpdate(位置L){//从对话片段称为
 locations.add(升);
 theLocations.notifyDataSetChanged();
}
 

那么让我切换到只使用了上述误差

 的String [] locationNames =新的String [sizeofLocations]
theLocations =新的ArrayAdapter&LT;字符串&GT;(mContext,R.layout.location_Row,locationNames);

公共无效onUIUpdate(位置L){
    locations.add(升);
            locationNames =新的String [locations.size()];
            对于(locations.size()等...){
               locationNames [I] =新字符串(locations.get(ⅰ).getName());
            }
    theLocations.notifyDataSetChanged();
}
 

这没有错误在我的更新方法(即更新UI),但它并没有任何更新。所以,我很失落,如何更新此ArrayAdapter,我还以为有NotifyChange应该做的,但它要么什么也不做,或抛出上述错误。

我在我的节目在其他地方我SimpleCursorAdapters任何问题(只要我的游标保持打开状态,我呼吁重新查询他们)。

任何见解什么,我做错了什么?

根据要求,这里是我的布局为R.layout.location_row

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT机器人:layout_width =FILL_PARENT机器人:方向=横向&GT;
&LT; TextView的Andr​​oid版本:文本=@ + ID / TextView01机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / LOCATION_NAME
    机器人:TEXTSIZE =15px的机器人:layout_marginTop =10px的机器人:layout_width =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; TextView的Andr​​oid版本:文本=|机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =15px的机器人:layout_marginTop =6px机器人:layout_width =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; TextView的Andr​​oid版本:文本=@ + ID / TextView01机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / location_lat
    机器人:TEXTSIZE =15px的机器人:layout_marginTop =6px机器人:layout_width =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; TextView的Andr​​oid版本:文本=| $机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =15px的机器人:layout_marginTop =6px机器人:layout_width =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; TextView的Andr​​oid版本:文本=@ + ID / TextView01机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / location_lon
    机器人:TEXTSIZE =15px的机器人:layout_marginTop =6px机器人:layout_width =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;
 

解决方案

正如我们在<读href="http://developer.android.com/reference/android/widget/ArrayAdapter.html">http://developer.android.com/reference/android/widget/ArrayAdapter.html ArrayAdapter只是工作与textviews字符串。您可以使用此构造

  ArrayAdapter(上下文的背景下,INT资源,INT textViewResourceId,名单,其中,T&GT;对象)
 

specifyng一个TextView的id布局或覆盖

里面

  getView(廉政,查看,ViewGroup中)
 

返回您的自定义类型的视图。

What does this mean? I have been fighting with this ListView that I have using an ArrayAdapter. I wrote this nice dialog fragment that I have calling back to an updateUI listner cause I want to update this ListView I have in my fragment from the DialogFragment at first the ArrayAdapter was a complex type of a class i Created:

ArrayAdapter<Location> theLocations;
...
//in oncreateview
theLocations = new ArrayAdapter<Location>(mContext, R.layout.location_row, locations);
//here locations is an ArrayList<Location>

then I have:

public void onUIUpdate(Location l) { //called from dialog fragment      
 locations.add(l);                  
 theLocations.notifyDataSetChanged();       
}

then that gave the above error so I switched it to use just a

String[] locationNames = new String[sizeofLocations];
theLocations=new ArrayAdapter<String>(mContext, R.layout.location_Row, locationNames );

public void onUIUpdate(Location l) {        
    locations.add(l);                   
            locationNames = new String[locations.size()];
            for(locations.size() etc...) { 
               locationNames [i] = new String(locations.get(i).getName());
            }
    theLocations.notifyDataSetChanged();        
}

this didn't error out in my update method (That updates the ui) but it didn't update anything. So I am lost as to how to update this ArrayAdapter, I thought notifyChange was supposed to do it, but it either does nothing or throws the above error.

I have no issues with my SimpleCursorAdapters elsewhere in my program (As long as my cursors remain open and i call a requery on them).

Any insights into what I am doing wrong?

Upon request here is my layout for the R.layout.location_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"     android:orientation="horizontal">   
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_name"
    android:textSize="15px" android:layout_marginTop="10px" android:layout_width="wrap_content"></TextView>
<TextView android:text="|" android:layout_height="wrap_content" 
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_lat"
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
<TextView android:text="|$" android:layout_height="wrap_content" 
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>  
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_lon"
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
</LinearLayout> 

解决方案

As we can read in http://developer.android.com/reference/android/widget/ArrayAdapter.html ArrayAdapter just works with strings on textviews. You can use this constructor

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

specifyng the id of a TextView inside your layout or override

getView(int, View, ViewGroup) 

returning a view of your custom type.

这篇关于错误:ArrayAdapter需要的资源ID是一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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