2 o19830015 o19830015 于 2013.07.30 11:27 提问

AsyncTask结果处理

使用AsyncTask结果生成listView,代码:

    public class MyActivity extends Activity {
        MainActivity ob;
        String url_connectX = "http://hosr.com/test/getjson.php";
        Httppostaux post;
        JSONArray lststat;
        public JSONArray liststatus() {
            JSONArray jdata=post.getserverdataX(url_connectX);
            return jdata;
        }       
        @Override
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.my_new_screen);
            post = new Httppostaux();
            new asyncpost().execute();
        }   //************************************************************************************
        class asyncpost extends AsyncTask< String, String, String > {
            List<String> listContents;          
            protected void onPostExecute(String result) {
               pDialog.dismiss();
               if (result.equals("ok")){
                   ListView myListView = (ListView) findViewById(R.id.ListView1);
                   Log.e("JSON log", "This is my problem = "+listContents);         
                   myListView.setAdapter(new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_list_item_1, listContents));
                }else{
                   Log.e("JSON log", "ListContents has problems."+listContents);            
                   err_getlista();
                }
            }
            protected String doInBackground(String... params) {
                lststat = liststatus();
                if (lststat != null){                       
                    if (lststat != null && lststat.length() > 0){
                        JSONObject json_data; 
                        try {
                            int length = lststat.length();
                            List<String> listContents = new ArrayList<String>(length);
                            for (int i = 0; i < length; i++)
                            {
                                json_data = lststat.getJSONObject(i);
                                listContents.add(json_data.getString("client_name"));
                            }
                        } catch (Exception e) {
                                Log.e(null, e.toString());
                        }                   
                    }               
                    return "ok"; 
                }else{          
                    return "err";                     
                }
            }
            public void err_getlista(){
                Toast toast1 = Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT);
                toast1.show();      
            }   
        }
//************************************************************************************
    }

但是listContents 出现空值,为什么?请各位帮忙看一下谢谢

2个回答

Kakalapa1986
Kakalapa1986   2013.07.30 13:59
已采纳

在doInBackground中重新声明listContents。这是一个局部变量,优先级高于类成员。

// should be ...
// listContents = new ArrayList<String>(length);
List<String> listContents = new ArrayList<String>(length);
for (int i = 0; i < length; i++)
{
    json_data = lststat.getJSONObject(i);
    listContents.add(json_data.getString("client_name"));
}
cj6585256
cj6585256   2013.07.30 11:49

打断点调试下,看JSONArray jdata=post.getserverdataX(url_connectX);这方法获取到值了吗?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!