2012-03-16 5 views
1

BLOBを含むSQLiteのリストビュー、ベースアダプタ、データがあります。 SQLクエリが変更された場合はどうなりますか? (ボタンを押して新しいSQLクエリを実行した後)adapter.notifyDataSetChangedを使用する必要がありますどこに配置する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; 
} 

}

+0

それはitemsAdapterまだ知らないその時点であなたのgetDataAndPopulate(文字列CMD)法 – waqaslam

+0

でそれを使用しています。私はsetListAdapter(itemsAdapter)とその下の1行の上に1行だけ試してみましたが、動作しません。 –

+0

の最初の行には、ボタンの* onClickListener *イベント – user1273768

答えて

0

まず、ListViewはどこですか? 第2に、アダプターをgetDataAndPopulate()関数に入れないでください。なぜあなたは毎回新しいアダプタを生成していますか?ここで

  1. のListView
  2. カーソル、ここでそれらを置くためである
  3. データ配列[それをしない関数を呼び出しを埋めます。 getDataAndPopulate()]
  4. データ配列をアダプタに設定します。アクティビティクラスファイルでアダプタをグローバルにします。
  5. アダプタをリストに設定します。

ボタンのオンクリック 1.クエリを実行し、データ配列を入力します。 2. adapter.notifyDatasetChanged()を呼び出します。

カーソルアダプターを使用する必要があります。あなたの人生は道に簡単になるだろう;)

+0

notifyDatasetChanged()はここでは必要ありません。 –

+0

それはまったく必要ないという意味ですか? onClick呼び出しに含まれていないかどうか – Shubhayu

+0

はい、アダプタがlistviewに設定された後も変更されないため、この場合は必須ではありません。アダプタの内容が変更され、リストも更新したい場合は、notifyDatasetChanged()を呼び出す必要があります。リストを更新するだけであり、初めてではありません。 –

0

アダプターを記入し、一度ListViewに設定しますので、あなたは、notifyDatasetChanged()を必要としません。の後にアダプタの内容を変更してListViewに設定し、リストを更新する場合は、notifyDatasetChanged()を呼び出してください。

+0

しかし、クエリが変更された後( "select * from plaatjes id = 1")、プログラムがクラッシュします。どのようにして問題を解決できますか? – user1273768

+0

DDMSログを投稿してください。データベースに関するエラーが発生している可能性があります。 –

関連する問題