Reputation: 21
I have an Android app (created from tutorials) that creates a list from JSON and depending on the data it creates a yellow or black background.
What i want is when a person clicks on a item from the list, data is transmitted to a server (works) But then i want to refresh my list because the data has changed (cannot get this to work) Can anyone help me?
This is my code:
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://www.somesite.com/kaku/kaku_list.php/";
// JSON Node names
private static final String TAG_ONTVANGERS = "ontvangers";
private static final String TAG_ID = "id";
private static final String TAG_BESCHRIJVING = "beschrijving";
private static final String TAG_KANAAL = "kanaal";
private static final String TAG_NUMMER = "nummer";
private static final String TAG_STATUS = "status";
// contacts JSONArray
JSONArray ontvangers = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> ontvanger = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
ontvangers = json.getJSONArray(TAG_ONTVANGERS);
// looping through All Contacts
for(int i = 0; i < ontvangers.length(); i++){
JSONObject c = ontvangers.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String beschrijving = c.getString(TAG_BESCHRIJVING);
String kanaal = c.getString(TAG_KANAAL);
String nummer = c.getString(TAG_NUMMER);
String status = c.getString(TAG_STATUS);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_BESCHRIJVING, beschrijving);
map.put(TAG_KANAAL, kanaal);
map.put(TAG_NUMMER, nummer);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
ontvanger.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new OntvangerAdapter(this, ontvanger,
R.layout.list_item,
new String[] { TAG_BESCHRIJVING, TAG_ID, TAG_STATUS }, new int[] {
R.id.beschrijving, R.id.Ontvanger_id, R.id.status});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String Ontvanger_id = ((TextView) view.findViewById(R.id.Ontvanger_id)).getText().toString();
JSONParser jParser = new JSONParser();
String url1 = "http://www.somesite.com/kaku/action.php?id="+Ontvanger_id+"/" ;
// getting JSON string from URL
jParser.getJSONFromUrl(url1);
}
});
}
}
Upvotes: 0
Views: 938
Reputation: 133560
From your comments
ListAdapter does not have notifyDataSetChanged()
http://developer.android.com/reference/android/widget/ListAdapter.html
You will to use ArrayAdapter
or BaseAdapter
. Its better to use a Custom Listview with a Custom Adapter extending either ArrayAdapter of BaseAdapter.(you can customize listview items easily).
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()
public void notifyDataSetChanged ()
Added in API level 1
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
Upvotes: 1
Reputation: 372
To update the data/list in list view, you have to make a call to the method notifyDataSetChanged();
How and when to call that?
After you modify your ontvanger
, call the function above.
Another side note, I think for newer API, you are not allowed to make HTTP request on UI thread anymore, you have to do it on AsyncTask or some background thread.
Hope that helps
Upvotes: 0