我有一个扩展ListActivity的活动。它有很多东西,但其中显示了用户用适配器购买的文章。我有一个方法,用户可以从列表中删除项目。问题是当只有一个项目时。如果我尝试删除最后一个应用程序崩溃。这是我的代码:
public class Ventas extends ListActivity {
...
lv = getListView();
...
protected void confirmRemoval(final int arg2) {
// TODO Auto-generated method stub
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(getResources().getString(R.string.ventas));
alertDialog.setMessage(getResources().getString(R.string.confirmacion2));
alertDialog.setButton("Si",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(adapter2.mEvents.size()>=1){
adapter2.mEvents.remove(arg2);
} else {
//doesn't work
/*adapter2=null;
adapter2.notifyDataSetInvalidated();
lv.setVisibility(View.GONE);*/
}
}
});
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialog.show();
}
这是适配器和包装器:
private class EventAdapter2 extends BaseAdapter {
public ArrayList<Articulo> mEvents = null;
public EventAdapter2(Context c, ArrayList<Articulo> clientes) {
mContext = c;
mEvents = clientes;
}
public int getCount() {
return mEvents.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
EventEntryView2 btv;
if (convertView == null) {
btv = new EventEntryView2(mContext, mEvents.get(position));
} else {
btv = (EventEntryView2) convertView;
String title1 = mEvents.get(position).getCantidad() + "";
if (title1 != null) {
btv.setText1Title(title1);
}
String title2 = mEvents.get(position).getDescripcion();
if (title2 != null) {
btv.setText2Title(title2);
}
String title3 = mEvents.get(position).getpVenta() + "0";
if (title3 != null) {
btv.setText3Title(title3);
}
String title4 = (mEvents.get(position).getCantidad() * mEvents
.get(position).getpVenta()) + "0";
if (title4 != null) {
btv.setText4Title(title4);
}
}
return btv;
}
private Context mContext;
}
private class EventEntryView2 extends LinearLayout {
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private View inflatedView;
public EventEntryView2(Context context, Articulo resp) {
super(context);
this.setOrientation(VERTICAL);
inflatedView = View.inflate(context, R.layout.results, null);
text1 = (TextView) inflatedView.findViewById(R.id.textView1);
text2 = (TextView) inflatedView.findViewById(R.id.textView2);
text3 = (TextView) inflatedView.findViewById(R.id.textView3);
text4 = (TextView) inflatedView.findViewById(R.id.textView4);
String t = resp.getCantidad() + "";
text1.setText(t);
String t1 = resp.getDescripcion();
text2.setText(t1);
String t2 = resp.getpVenta() + "0";
text3.setText(t2);
String t3 = (resp.getCantidad() * resp.getpVenta()) + "0";
text4.setText(t3);
addView(inflatedView, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
public void setText4Title(String title4) {
text4.setText(title4);
}
public void setText3Title(String title3) {
text3.setText(title3);
}
public void setText2Title(String title2) {
text2.setText(title2);
}
public void setText1Title(String title1) {
text1.setText(title1);
}
}
正如你所看到的,当我只有一个项目时,我试图将适配器设置为null或者adapter.notifyDataSetInvaliadted甚至使listview不可见,没有任何效果。当我点击确定没有任何变化然后当我第二次点击全部崩溃
时会发生什么我想要的是当适配器为空时listView消失但我现在没有想法,它甚至可能吗? 有什么想法吗?
修改: 谢谢大家的答案,但问题是我在内部匿名类中修改列表。它实际上非常简单,创建一个方法并从对话框中调用它,一旦数组为空,列表就会自动消失:
protected void removeFromList(int arg2) {
adapter2.mEvents.remove(arg2);
adapter2.notifyDataSetChanged();
}
答案 0 :(得分:1)
从您添加到适配器中的arraylist中删除项目,然后调用此方法。
youradapter.notifyDataSetChanged();
以及您为单项
所做的一切adapter2 = null;
adapter2.notifyDataSetInavlidated();
这显然会使它崩溃,因为adapter2对象为null所以null对象如何通知其数据
答案 1 :(得分:0)
尝试在lv.invalidate()
之后调用remove()
,看看是否有任何区别。
答案 2 :(得分:0)
你应该检查你的适配器类是否为null然后你不应该从它获取值......这是你从null变量获取值时获得异常的主要原因。请检查那里
答案 3 :(得分:0)
让setVisibility
工作:
添加一个ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/sherif.android.deedz"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/myListView"
android:divider="#ffa500" android:dividerHeight="1px"
android:background="@drawable/somedrawable_xml"
android:choiceMode="singleChoice"></ListView>
</ListView>
现在你可以GONE
如果你想要这个的全部细节: 查看my answer