gpt4 book ai didi

java - ArrayAdapter 中的 android UnsupportedOperationException

转载 作者:行者123 更新时间:2023-11-29 05:20:58 24 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一个 onDisimiss 监听器,因为现在这就是我所拥有的

Activity

public class listview_test extends Activity {
ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Windows",
} ;
Integer[] imageId = {
R.drawable.ic_launcher,
R.drawable.icon,
R.drawable.ic_launcher,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_test);
final CustomList adapter = new
CustomList(listview_test.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(listview_test.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
list,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.remove(adapter.getItem(position));
}
adapter.notifyDataSetChanged();
}
}
);
list.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
list.setOnScrollListener(touchListener.makeScrollListener());
}
}

和自定义适配器

public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.weather_item, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.weather_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.city);
ImageView imageView = (ImageView) rowView.findViewById(R.id.info_image);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}

出于某种原因,每次我滑动一个项目以关闭它时,它都会强制关闭并给我这个异常

    java.lang.UnsupportedOperationException

在这一行

adapter.remove(adapter.getItem(position));

最佳答案

java.lang.UnsupportedOperationException

当您通过数组或不可修改的 List 支持 Adapter 时抛出。由于您无法更改它们的大小,因此无法删除。

相反,修改您的适配器,使其接受List 而不是数组,并确保您传递的List 是完全灵活的.

有点像

List <String> web = new ArrayList <String> ();
web.add ("Google Plus");
web.add ("Twitter");
//etc.

足以确保一个灵活的列表。

这意味着您的 CustomList 适配器也应该调用同样接受 List 的父类(super class)构造函数,在本例中是

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

有关更多信息,请参阅 ArrayAdapter文档。

关于java - ArrayAdapter<String> 中的 android UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com