无法使用LayoutInflater在自定义适配器 [英] Unable to use LayoutInflater in custom adapter

查看:225
本文介绍了无法使用LayoutInflater在自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在计算器的第一个问题就这样类型:)

This is my first question on StackOverflow so be kind :)

我在找到编写自定义适配器来填充列表视图,每行3 textviews。我发现相当多的examplte code要做到这一点,但一个似乎最好是在:的 http://www.anddev.org/custom_widget_adapters-t1796.html

I'm looking into writing a custom adapter to populate a listview with 3 textviews per line. I've found quite a bit of examplte code to do this, but the one that seemed the best was at: http://www.anddev.org/custom_widget_adapters-t1796.html

在一些小的调整,修复了最新的Andr​​oid SDK中一些编译器的问题,我得到它运行时,只得到了异常:

After a few minor tweaks to fix some compiler issues with the latest android sdk, i got it running, only to get the exception:

ERROR / AndroidRuntime(281):java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)不支持适配器视图

ERROR/AndroidRuntime(281): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

所以,我做了很多的研究,发现许多可能的原因,并修复了这一点。其中没有改变的事情。我的适配器code是当前:

So I did a lot of research and found lots of possible reasons and fixes for this. None of which changed a thing. My adapter code is currently:

public class WeatherAdapter extends BaseAdapter {

private Context context;
private List<Weather> weatherList;

public WeatherAdapter(Context context, int rowResID,
                    List<Weather> weatherList ) { 
    this.context = context;
    this.weatherList = weatherList;
}

public int getCount() {                        
    return weatherList.size();
}

public Object getItem(int position) {     
    return weatherList.get(position);
}

public long getItemId(int position) {  
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) { 
    Weather weather = weatherList.get(position);

    //LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LayoutInflater inflater = LayoutInflater.from(context);

    View v = inflater.inflate(R.layout.weather_row, null, true);
    TextView cityControl = (TextView)v.findViewById( R.id.city );
    TextView temperatureControl = (TextView)v.findViewById( R.id.temperature );
    ImageView skyControl = (ImageView)v.findViewById( R.id.sky );
    return v;
}

}

所以,我试图让充气的注释掉的方式,和当前的注释去掉了。我曾尝试过父膨胀,以及空,并通过真,假和ommitting完全的最后一个参数。他们都没有工作,我已经发现迄今已经从2008年,我得到的感觉所有的例子都有点过时了。

So I have tried the commented out way of getting the inflater, and the currently uncommented out. I have tried passing "parent" to inflate as well as null, and passing "true", "false" and ommitting completely the last parameter. None of them have worked, and all examples I've found so far have been from 2008 which I get the feeling are a bit outdated.

如果有人可以帮助这个话,我很愿意解决这个问题。一些看似微不足道首先引起挫折感好几个小时!

If anyone could help with this then I would love to resolve the issue. Something seemingly trivial at first has caused me several hours of frustration!

推荐答案

我是个初学者也因此采取这种复用少许盐 - 如果它不工作,继续前进,谷歌多一些。不熟悉适配器视图,因为我通常有一个的ListView 的GridView 和一个自定义适配器扩展掀起了 BaseAdapter 然后 listView.setAdapter(myCustomAdapter)

I'm a beginner also so take this answer with a pinch of salt - if it doesn't work, move on and Google some more. Not familiar with AdapterView since I traditionally have a ListView or GridView and a custom Adapter extended off a BaseAdapter and then listView.setAdapter(myCustomAdapter).

您可以尝试做这样的事了 WeatherAdapter 类中:

You could try making something like this inside the WeatherAdapter class:

public void addToList(Weather mWeather) {
    weatherList.add(mWeather);
}

然后在调用 WeatherAdapter 类:

weatherAdapter.addToList(weatherToAdd);
weatherAdapter.notifyDataSetChanged();

此外,你需要更多的优化它在getView方法:

Also you need to optimize it more in the getView method:

http://www.youtube.com/watch?v=wDBM6wVEO70

这篇关于无法使用LayoutInflater在自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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