放置adapter.notifyDataSetChanged的位置

时间:2012-03-16 10:42:56

标签: android listview

我有一个listview,baseadapter和来自SQLite的数据,包括一个BLOB。如果SQL查询发生了变化怎么办? (按下按钮执行新的SQL查询后)我必须使用adapter.notifyDataSetChanged

我可以在哪里放置这行代码?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.example);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    placeData = new DatabaseSQL(this);

    String cmd = "select * from plaatjes ";

    Cursor cursors = getRawEvents(cmd);

    if (cursors.moveToNext()) {
        getDataAndPopulate(cmd);
    }

}

private void getDataAndPopulate(String cmd) {
    id = new ArrayList<String>();
    image = new ArrayList<byte[]>();
    caption = new ArrayList<String>();
    description = new ArrayList<String>();
    Cursor cursor = getRawEvents(cmd); 
    while (cursor.moveToNext()) {
        String temp_id = cursor.getString(0);
        byte[] temp_image = cursor.getBlob(1);
        String temp_caption = cursor.getString(2);
        String temp_description = cursor.getString(3);
        id.add(temp_id);
        image.add(temp_image);
        caption.add(temp_caption);
        description.add(temp_description);
    }
    String[] captionArray = (String[]) caption.toArray(
            new String[caption.size()]);

    ItemsAdapter itemsAdapter = new ItemsAdapter(
            Example.this, R.layout.item,
            captionArray);

    setListAdapter(itemsAdapter);

}


private class ItemsAdapter extends BaseAdapter {
    String[] items;

    public ItemsAdapter(Context context, int textViewResourceId,
            String[] items) {
        this.items = items;
    }

    @Override
    public View getView(final int POSITION, View convertView,
            ViewGroup parent) {
        TextView desc;
        TextView cap;
        View view = convertView;
        ImageView img;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.item, null);

        } 
        img = (ImageView) view.findViewById(R.id.image);
        cap = (TextView) view.findViewById(R.id.caption);
        desc = (TextView) view.findViewById(R.id.description);

        cap.setText(caption.get(POSITION));
        desc.setText(description.get(POSITION));
        img.setImageBitmap(BitmapFactory.decodeByteArray(image.get(POSITION), 0, image.get(POSITION).length));


        return view;
    }

    public int getCount() {
        return items.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
}

private Cursor getRawEvents(String sql) {
    SQLiteDatabase db = (placeData).getReadableDatabase();
    Cursor cursor = db.rawQuery(sql, null);

    startManagingCursor(cursor);
    return cursor;
}

private Cursor getEvents(String table) {
    SQLiteDatabase db = (placeData).getReadableDatabase();
    Cursor cursor = db.query(table, null, null, null, null, null, null);

    startManagingCursor(cursor);
    return cursor;
}

}

2 个答案:

答案 0 :(得分:0)

您不需要notifyDatasetChanged(),因为您填充适配器并将其设置为ListView一次。更改适配器的内容 >将其设置为notifyDatasetChanged()并且您想要更新列表时,需要调用ListView

答案 1 :(得分:0)

首先,ListView在哪里? 其次,不要将适配器放在getDataAndPopulate()函数中。你为什么每次都生成一个新的适配器?

以下是放置它们的顺序,

  1. 的ListView
  2. 光标
  3. 填充数据数组[调用执行此操作的函数。 getDataAndPopulate()]
  4. 将数据数组设置为适配器。使适配器在Activity类文件中全局化。
  5. 将适配器设置为列表。
  6. 在按钮的onClick中 1.执行查询并填充数据数组。 2.调用adapter.notifyDatasetChanged()。

    你应该使用光标适配器。会让你的生活变得更简单;)