这是我跟着使用自定义列表视图适配器的教程。我遇到的问题是,当我尝试清除该适配器,应用程序崩溃并引发 java.lang.UnsupportedOperationException
如果(适配器!= NULL){
adapter.clear();
}
更新时间是code:
私人无效setListViewAdapterToDate(INT月,年整型,INT DV)
{
如果(summaryAdapter!= NULL){
summaryAdapter.clear();
}
setListView(月,年,DV);
summaryList.addAll(Arrays.asList(summary_data));
summaryAdapter =新SummaryAdapter(this.getActivity()getApplicationContext(),R.layout.listview_item_row,summaryList。);
summaryAdapter.notifyDataSetChanged();
calendarSummary.setAdapter(summaryAdapter);
}
解决方案
环顾了一下,又好像与初始化数组的适配器的问题。请参阅UnsupportedOperationException与ArrayAdapter.remove 和无法修改ArrayAdapter中的ListView
尝试使用的ArrayList
而不是一个阵列
像这样
的ArrayList<天气及GT; weather_data =新的ArrayList<天气>()
weather_data.add(新气象(R.drawable.weather_cloudy,雨));
//继续为您的天气项目的其余部分。
如果你感觉懒惰,您可以将您的阵列
到的ArrayList
这样
的ArrayList<天气及GT; weatherList =新的ArrayList<天气>();
weatherList.addAll(Arrays.asList(weather_data));
要完成转换到的ArrayList
在 WeatherAdapter
类,你会想删除气象数据[] = NULL;
和它的所有的引用(如在构造函数中),因为 ArrayAdapter
保存数据,你,你可以与的getItem
所以,你的 getView
函数中你会改变天气预报气象=数据[位置]
到天气预报气象=的getItem(位置);
更新 修改您的udated code与
私人无效setListViewAdapterToDate(INT月,年整型,INT DV)
{
setListView(月,年,DV);
如果(summaryAdapter!= NULL){
summaryAdapter.clear();
summaryAdapter.addAll(summaryList);
summaryAdapter.notifyDataSetChanged();
} 其他 {
summaryList.addAll(Arrays.asList(summary_data));
summaryAdapter =新SummaryAdapter(this.getActivity()getApplicationContext(),R.layout.listview_item_row,summaryList。);
}
calendarSummary.setAdapter(summaryAdapter);
}
This is the tutorial that I followed to use a custom Listview Adapter. The problem I am having is that when I try to clear the adapter, the app crashes and throws java.lang.UnsupportedOperationException
if(adapter != null) {
adapter.clear();
}
UPDATED CODE:
private void setListViewAdapterToDate(int month, int year, int dv)
{
if(summaryAdapter != null) {
summaryAdapter.clear();
}
setListView(month, year, dv);
summaryList.addAll(Arrays.asList(summary_data));
summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summaryList);
summaryAdapter.notifyDataSetChanged();
calendarSummary.setAdapter(summaryAdapter);
}
解决方案
Looking around a bit, it would seem that initializing the adapter with an array is the problem. See UnsupportedOperationException with ArrayAdapter.remove and Unable to modify ArrayAdapter in ListView
Try using an ArrayList
instead of an array
like so
ArrayList<Weather> weather_data = new ArrayList<Weather>()
weather_data.add( new Weather(R.drawable.weather_cloudy, "Cloudy") );
// continue for the rest of your Weather items.
If you're feeling lazy, you can convert your array
to an ArrayList
this way
ArrayList<Weather> weatherList = new ArrayList<Weather>();
weatherList.addAll(Arrays.asList(weather_data));
To finish the conversion to ArrayList
in your WeatherAdapter
class you will want to remove the Weather data[] = null;
and all of it's references (such as inside the constructor) because ArrayAdapter
holds the data for you and you can access it with getItem
So inside of your getView
function you would change Weather weather = data[position];
to Weather weather = getItem(position);
Update Modify your udated code with
private void setListViewAdapterToDate(int month, int year, int dv)
{
setListView(month, year, dv);
if(summaryAdapter != null) {
summaryAdapter.clear();
summaryAdapter.addAll( summaryList );
summaryAdapter.notifyDataSetChanged();
} else {
summaryList.addAll(Arrays.asList(summary_data));
summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summaryList);
}
calendarSummary.setAdapter(summaryAdapter);
}
相关推荐
最新文章