Reputation: 1446
I got this ListView which is populated from a JSON data on the web. But when I updated the JSON entry, for example adding a new entry the ListView isn't updated. It doesn't show the new entry on the list even though I've already called the notifyDataSetChanged().
Here's my code:
public class ProjectsList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projects_list);
Intent serviceIntent = new Intent(this, LooserSync.class);
startService(serviceIntent);
ListView listView = (ListView) findViewById(R.id.lstText);
MySimpleCursorAdapter projectAdapter = new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(LooserProvider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE, Database.Project.C_SMALLIMAGE }, null, null,
null), new String[] { Database.Project.C_PROJECTTITLE, Database.Project.C_SMALLIMAGE }, new int[] {
R.id.txt_title, R.id.image });
listView.setAdapter(projectAdapter);
projectAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(ProjectsList.this, DetailsActivity.class);
i.setData(Uri.withAppendedPath(Uri.withAppendedPath(
LooserProvider.CONTENT_URI, Database.Project.NAME), Long
.toString(id)));
i.putExtra("spendino.de.ProjectDetail.position",position);
startActivity(i);
}
});
}
class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
loader = new ImageLoaderCache(context);
this.context = context;
}
Activity activity= ProjectsList.this;
Context context=null;
ImageLoaderCache loader = null;
public void setViewImage(ImageView v, String value) {
v.setTag(value);
loader.displayImage(value, activity, v);
}
}
}
UPDATED here's the LooserSync.java
public class LooserSync extends IntentService {
public LooserSync() {
super("LooserSyncService");
}
@Override
protected void onHandleIntent(Intent intent) {
Database.OpenHelper dbhelper = new Database.OpenHelper(getBaseContext());
SQLiteDatabase db = dbhelper.getWritableDatabase();
DefaultHttpClient httpClient = new DefaultHttpClient();
db.beginTransaction();
HttpGet request = new HttpGet(
"http://liebenwald.spendino.net/admanager/dev/android/projects.json");
try {
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream instream = response.getEntity().getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(
instream), 8000);
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
instream.close();
String bufstring = total.toString();
JSONArray arr = new JSONArray(bufstring);
Database.Tables tab = Database.Tables.AllTables.get(Database.Project.NAME);
tab.DeleteAll(db);
for (int i = 0; i < arr.length(); i++) {
tab.InsertJSON(db, (JSONObject) arr.get(i));
}
db.setTransactionSuccessful();
}
} catch (Exception e) {
e.printStackTrace();
}
db.endTransaction();
db.close();
}
}
Upvotes: 3
Views: 9763
Reputation: 4258
projectAdapter.notifyDataSetChanged();
replace the above line with the given blow line then you got solution of your problem.
((MySimpleCursorAdapter)(ProjectsList.listView.getAdapter())).notifyDataSetChanged();
Mistake: You should get adapter of the list view after that you notify the adapter of the list.
Upvotes: 0
Reputation: 27455
As you're using a cursor to populate the list you have to get a new one or requery the old one after you changed (add, edit or remove) something on your model.
When you got a new cursor you can pass it to the adapter by calling changeCursor().
UPDATE
Following code will get a new cursor each time onResume() called. So your list should be up to date. Of course changes on the model which are made while the list is shown are not updated to the list. If you want a live update of the list you have to implement some kind of observer pattern. So your activity would get notified when the model changed.
public class ProjectsList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projects_list);
Intent serviceIntent = new Intent(this, LooserSync.class);
startService(serviceIntent);
ListView listView = (ListView) findViewById(R.id.lstText);
final String[] from = new String[] { Database.Project.C_PROJECTTITLE, Database.Project.C_SMALLIMAGE };
final int[] to = new int[] {R.id.txt_title, R.id.image};
MySimpleCursorAdapter projectAdapter = new MySimpleCursorAdapter(this, R.layout.listitems, null, from, to);
listView.setAdapter(projectAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(ProjectsList.this, DetailsActivity.class);
i.setData(Uri.withAppendedPath(Uri.withAppendedPath(
LooserProvider.CONTENT_URI, Database.Project.NAME), Long
.toString(id)));
i.putExtra("spendino.de.ProjectDetail.position",position);
startActivity(i);
}
});
}
public void onResume(){
Cursor cursor = managedQuery(Uri.withAppendedPath(LooserProvider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE, Database.Project.C_SMALLIMAGE }, null, null, null);
ListView listView = (ListView) findViewById(R.id.lstText);
((CursorAdapter)listView.getAdapter()).changeCursor(cursor);
}
Upvotes: 4