3

我添加了两个按钮(添加和删除)来控制列表视图,当我删除一个项目时,该项目不会立即在列表视图中消失,只有当我在列表视图上滑动时,新项目才会消失。除非我触摸屏幕或在列表视图上滑动,否则在我删除项目后不会调用适配器中的 getView()。有人能告诉我为什么吗?

我的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/locationlist_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >"

    <RelativeLayout
        android:id="@+id/locationlisttitle_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" 
        android:background="#ADD8E6">

        <ImageView
            android:id="@+id/iv_menu2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:src="@drawable/img_menu" />

        <TextView
            android:id="@+id/txtv_locationmanagetitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Manage Location List"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/iv_addlocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/txtv_locationmanagetitle"
            android:src="@drawable/addlocation" />

        <ImageView
            android:id="@+id/iv_deletelocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/iv_addlocation"
            android:src="@drawable/deletelocation" />

    </RelativeLayout>

    <ListView
        android:id="@+id/lv_locations"
        android:layout_height="500dp"
        android:layout_width="fill_parent" />
</LinearLayout>

我的按钮单击事件处理程序

ivbtn_deleteLocation.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //remove selected items from the locationList
            Collections.sort(listStr);
            for (int i = listStr.size()-1; i >= 0; i--) {
                Log.i("zhijianw", "remove"+listStr.get(i));
                Log.i("zhijianw", "remove"+locationList.get(Integer.parseInt(listStr.get(i))).get("txtv_locationitem"));
                String remLocation = locationList.get(Integer.parseInt(listStr.get(i))).get("txtv_locationitem").toString();
                locationList.remove(Integer.parseInt(listStr.get(i)));
                removeLocation(remLocation);
            } 
            listStr = new ArrayList<String>();
            locationsAdapter.notifyDataSetChanged();
        }
});

我的适配器

  public MyAdapter(Context context, List<HashMap<String, Object>> list, int resource, String[] from, int[] to) {
            this.context = context;
            this.list = list;
            keyString = new String[from.length];
            idValue = new int[to.length];
            System.arraycopy(from, 0, keyString, 0, from.length);
            System.arraycopy(to, 0, idValue, 0, to.length);
            inflater = LayoutInflater.from(context);
            init();
            Log.i("zhijianw", "my adapter called"); 
    }

    public void init() {
            isSelected = new HashMap<Integer, Boolean>();
            for (int i = 0; i < list.size(); i++) {
                    isSelected.put(i, false);
            }
    }

    @Override
    public int getCount() {
        Log.i("zhijianw", "get count called");  
            return list.size(); 
    }

    @Override
    public Object getItem(int arg0) {
        Log.i("zhijianw", "get item called");
            return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
            return arg0;
    }

    @Override
    public View getView(int position, View view, ViewGroup arg2) {
        init();
        Log.i("zhijianw", "get view called");           
        ViewHolder holder = null;
        if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.location_item, null);
                holder.tv = (TextView) view.findViewById(R.id.txtv_locationitem);
                holder.cb = (CheckBox) view.findViewById(R.id.cb_locationitem);
                view.setTag(holder);
        } else {
                holder = (ViewHolder) view.getTag();
        }
        HashMap<String, Object> map = list.get(position);
        if (map != null) {
                itemString = (String) map.get(keyString[0]);
                holder.tv.setText(itemString);
        }
        holder.cb.setChecked(isSelected.get(position));
        return view;
    }

}

设置列表视图适配器

lv_menu = (ListView) menu_view.findViewById(R.id.lv_menu);
lv_menu.setAdapter(new ArrayAdapter<String>(this, R.layout.menu_item,   R.id.txtv_menuItem, menuChoices));
4

2 回答 2

1

locationsAdapter似乎没有设置为使用的相同适配器,lv_menu.setAdapter()因此不会locationsAdapter.notifyDataSetChanged()失效lv_menulv_menu.invalidate()直接在你的onClick()函数中尝试。

每当删除项目时,我个人都会从头开始重新加载我的列表适配器,尤其是在CheckBox涉及项目时,以防止事情不同步。

于 2013-04-01T16:27:20.357 回答
0

请检查传递给充气器的参数(在 getView 方法中)是否引用了启动适配器时使用的相同参数。如果它们不相同,则不会在没有任何进一步通知的情况下调用 getView。佐哈尔

于 2013-11-10T09:03:49.957 回答