我正在Android中开发一个JSON解析应用程序,我在我的服务器上有一个php脚本,用于从mysql数据库中获取所有数据并编码为json,应用程序与服务器联系并使用来自的数据填充ListView使用AsyncTaskLoader的JSON。它工作正常但如果我删除所有数据库记录,然后再次启动我的应用程序,ListView仍会显示旧数据。即使我按下调用AsyncTaskLoader但仍旧数据的刷新按钮。我不知道为什么请帮助我。
谢谢 P / s:抱歉我的英语不好^^
抱歉,我忘记了密码。我们走了
TVSchedFragment.java
public class TVSchedFragment extends ListFragment {
private TVSchedAdapter adpt;
ListView lView;
// JSON Node names
private static final String TAG_SCHEDULE = "Sched";
private static String url = "http://192.168.20.205/DOM/app/json.php";
String jsonStr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tvsched, container,
false);
lView = (ListView) rootView.findViewById(android.R.id.list);
adpt = new TVSchedAdapter(new ArrayList<TVSchedModel>(), getActivity());
lView.setAdapter(adpt);
(new AsyncListViewLoader()).execute();
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_tvsched, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_tvsched_refresh:
(new AsyncListViewLoader()).execute();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private class AsyncListViewLoader extends
AsyncTask<String, Void, List<TVSchedModel>> {
private final ProgressDialog dialog = new ProgressDialog(getActivity());
@Override
protected void onPostExecute(List<TVSchedModel> result) {
super.onPostExecute(result);
dialog.dismiss();
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Downloading schedule...");
dialog.show();
}
@Override
protected List<TVSchedModel> doInBackground(String... params) {
List<TVSchedModel> result = new ArrayList<TVSchedModel>();
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray(TAG_SCHEDULE);
for (int i = 0; i < contacts.length(); i++) {
result.add(convertSched(contacts.getJSONObject(i)));
}
return result;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
private TVSchedModel convertSched(JSONObject obj)
throws JSONException {
String name = obj.getString("Program");
String desc = obj.getString("Description");
String time = obj.getString("dateTimeString");
String chan = obj.getString("Channel");
return new TVSchedModel(time, name, desc, chan);
}
}
@Override
public void onDestroy() {
super.onDestroy();
adpt.clear();
}
}
ServiceHandler.java
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
答案 0 :(得分:0)
如果您使用ArrayAdapter显示列表视图,请在finish()或刷新列表时清除数组列表。清除上一个列表存储更新的数据并调用adapter.notifyDataChanged();
。