我有一个 Android 应用程序(从教程创建),它从 JSON 创建一个列表,并根据数据创建黄色或黑色背景。
我想要的是当一个人点击列表中的一个项目时,数据被传输到服务器(工作)但是我想刷新我的列表,因为数据已经改变(无法让它工作)有人可以帮助我吗?
这是我的代码:
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);
}
});
}
}