Android getView:为什么不调用getView?

时间:2013-04-01 14:08:53

标签: android android-adapter

我添加两个按钮(添加和删除)来控制列表视图,当我删除一个项目时,该项目不会立即消失在列表视图中,只有当我在列表视图上滑动时,新项目才会消失。删除项目后,我的适配器中的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));

2 个答案:

答案 0 :(得分:1)

locationsAdapter似乎未设置为lv_menu.setAdapter()使用的同一适配器,因此locationsAdapter.notifyDataSetChanged()不会使lv_menu无效。直接在lv_menu.invalidate()函数中试用onClick()

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

答案 1 :(得分:0)

请检查转移到inflater的参数(在getView方法中)是指引用适配器时使用的相同参数。如果它们不相同,则不会在没有任何进一步通知的情况下调用getView。 佐哈尔