I have a listview, baseadapter and data from SQLite, including a BLOB. What if the SQL-query changes? (After pressing a button to execute a new SQL-query) I have to use adapter.notifyDataSetChanged
Where can I place that line of code?
/** 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;
}
}
First of all where is the ListView?
Secondly, don’t put the adapter in the getDataAndPopulate() function. Why are you generating a new adapter everytime?
Here is the order in which to put them,
In your onClick of the Button
1. execute the query and fill up the data array.
2. call adapter.notifyDatasetChanged().
You ought to use a Cursor Adapter. Would make your life way simpler 😉