0

希望有人能指出我正确的方向,我正在创建一个自定义适配器以在列表视图图像、ID、文本中显示 3 个项目。

所以我从返回 JSON 对象的 web 服务中获取我的数据,它工作正常。我的问题是我不确定在 Asynctask 之后我哪里出错了,因为列表没有任何数据,我已经在将单个字段传递给微调器时实现了这一点,但只是无法弄清楚。我知道自定义适配器的工作原理与我手动分配新的 CallOut 对象并添加到 Arraylist 时一样,它们列表显示很好,感谢您的帮助。

public class ListCallOuts extends Activity {



ArrayList<CallOut> callOutResults=new ArrayList<CallOut>(); //=GetCallOuts();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    GetCallOuts();

    //ArrayList<CallOut> searchResults = GetSearchResults();





    final ListView lv1 = (ListView) findViewById(R.id.listView1);
    lv1.setAdapter(new CallOutAdapter(this,callOutResults));
    //lv1.setAdapter(new CallOutAdapter(this, searchResults));

    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Object o = lv1.getItemAtPosition(position);
            CallOut fullObject = (CallOut)o;
            Toast.makeText(ListCallOuts.this, "You have chosen: " + " " +   
           fullObject.getName(), Toast.LENGTH_LONG).show();
        }
    });
}

private void GetCallOuts() {

    new DownloadCallouts().execute(new String[]  
{"http://192.168.0.16:8080/return_callouts.json"});



}


private class DownloadCallouts extends AsyncTask<String,Void,JSONArray> {
    @Override
    protected JSONArray doInBackground(String... urls) {

        JSONArray array = null;
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                HttpEntity entity = execute.getEntity();
                String data = EntityUtils.toString(entity);
                array = new JSONArray(data);


            } catch (JSONException e) {
                e.printStackTrace(); 
            } catch (ClientProtocolException e) {
                e.printStackTrace();  
            } catch (IOException e) {
                e.printStackTrace();  
            }
        }
        return array;
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        for(int i=0;i<jsonArray.length();i++){

            try {
                JSONObject row = jsonArray.getJSONObject(i);
                CallOut callOut = new    
  CallOut(row.getString("id"),row.getString("customer_name")) ;
                callOutResults.add(callOut);
            } catch (JSONException e) {
                e.printStackTrace();  
            }


        }
    }
}
}
4

1 回答 1

0

改变这个:

final ListView lv1 = (ListView) findViewById(R.id.listView1);
lv1.setAdapter(new CallOutAdapter(this,callOutResults));

有了这个:

final ListView lv1 = (ListView) findViewById(R.id.listView1);
callOutAdapter = new CallOutAdapter(this,callOutResults);
lv1.setAdapter(callOutAdapter);

注意:您应该将其定义为您的类的属性,以便在 onPostExecute 方法中访问它。

CallOutAdapter callOutAdapter;

并在onPostExecute添加此行更新您的arraylist 后:

callOutAdapter.notifyDataSetChanged();
于 2013-11-03T14:03:38.993 回答