我需要在我的 Android 项目中使用 AQuery,以便对服务器进行异步调用。我只想调用服务器,当它给我数据时,我会用这些数据更新我的视图。因此,我在 AsyncTask 中编写了异步调用。下面你可以看到我做了什么:
public class Tab1Fragment extends Fragment implements OnItemClickListener {
private ListView categorieListView ;
private ArrayAdapter<String> listAdapterCategorie ;
private AQuery aq;
private String[] lista = null;
private ProgressDialog pDialog;
private String nome_categoria;
private int[] arrayID = null;
ArrayList<String> planetList;
JsonRequestActivity categoryAjax;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
pDialog = new ProgressDialog(getActivity());
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
aq = new AQuery(this.getActivity(), view);
categorieListView = (ListView) view.findViewById(R.id.listView1);
//new FetchMyDataTask(this.getActivity(), new FetchMyDataTaskCompleteListener(listAdapterCategorie, categorieListView, this.getActivity()), aq).execute("InputString");
if(lista == null){
new AggiornaLista().execute();
}else{
listAdapterCategorie = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.categoriegenerali, R.id.nomeAttivita, lista);
categorieListView.setAdapter( listAdapterCategorie );
}
categorieListView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent esercizioScelto = new Intent(this.getActivity(), CategoriaScelta.class);
nome_categoria = categorieListView.getItemAtPosition(position).toString();
esercizioScelto.putExtra("nome_categoria", nome_categoria);
esercizioScelto.putExtra("id_categoria", arrayID[position]);
startActivity(esercizioScelto);
}
private class AggiornaLista extends AsyncTask<String, Void, String[]>{
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog.setMessage("Caricamento dati server");
}
@Override
protected String[] doInBackground(String... params) {
String url = Const.URL_JSON_ARRAY;
Map<String, String> params2 = new HashMap<String, String>();
params2.put("type", "get_all");
aq.ajax(url, params2, JSONArray.class, new AjaxCallback<JSONArray>() {
@Override
public void callback(String url, JSONArray json, AjaxStatus status) {
if(json != null){
lista = new String[json.length()];
arrayID = new int[json.length()];
for(int i = 0; i < json.length(); i++){
try {
lista[i] = result.getJSONObject(i).getString("Name");
arrayID[i] = result.getJSONObject(i).getInt("ID");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Toast.makeText(aq.getContext(), jsonArray.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
//Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
// do something with the result
}
});
return lista;
}
@Override
protected void onPostExecute(String result[])
{
super.onPostExecute(result);
pDialog.dismiss();
listAdapterCategorie = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.categoriegenerali, R.id.nomeAttivita, result);
// Set the ArrayAdapter as the ListView's adapter.
categorieListView.setAdapter( listAdapterCategorie );
}
}
}
这种实施并没有像我预期的那样工作。实际上,在onPostExecute内部,doInBackground返回的参数并没有更新,它包含了初始化值(null)。然后我只在 AsyncTask 中改变了一些东西:
private class AggiornaLista extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog.setMessage("Caricamento dati server");
}
@Override
protected Void doInBackground(String... params) {
String url = Const.URL_JSON_ARRAY;
Map<String, String> params2 = new HashMap<String, String>();
params2.put("type", "get_all");
aq.ajax(url, params2, JSONArray.class, new AjaxCallback<JSONArray>() {
@Override
public void callback(String url, JSONArray json, AjaxStatus status) {
if(json != null){
lista = new String[json.length()];
arrayID = new int[json.length()];
for(int i = 0; i < json.length(); i++){
try {
lista[i] = result.getJSONObject(i).getString("Name");
arrayID[i] = result.getJSONObject(i).getInt("ID");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Toast.makeText(aq.getContext(), jsonArray.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
//Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
// do something with the result
listAdapterCategorie = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.categoriegenerali, R.id.nomeAttivita, lista);
// Set the ArrayAdapter as the ListView's adapter.
categorieListView.setAdapter( listAdapterCategorie );
}
});
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
pDialog.dismiss();
}
}
这样做,用户界面会正确更新,但我认为这不是正确的方法。那么,我能做些什么呢?谢谢。
安德烈亚