0

我一直在运行这段代码(除了onCreate因为我必须先从网上检索数据),它就停在线上mainView.setAdapter,永远不会进入下一个调试语句(或为此加载数据)。我究竟做错了什么?

private void loadInteractions(JSONArray interactions){
    mainView = (ListView) findViewById(R.id.list);
    myInteractionsTask.cancel(false);
    String[] interactionsArray=new String[interactions.length()];
    for(int interactionCounter=0; interactionCounter<interactions.length();interactionCounter++){
        TextView interactionView = new TextView(this);
        Log.d("Debug", "Loading Interaction #"+interactionCounter);
        try {
            Log.d("Debug", "Loading text: "+interactions.getString(interactionCounter));
            interactionsArray[interactionCounter]=interactions.getString(interactionCounter);
            Log.d("Debug", "Done loading text");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    mainView.setAdapter(new ArrayAdapter<String>(this, R.id.list, interactionsArray));
    Log.d("Debug", "Done loading textView");

}
4

2 回答 2

1
mainView.setAdapter(new ArrayAdapter<String>(this, R.id.list, interactionsArray));

构造函数的第二个参数ArrayAdapter应该是单独的ListView项目布局,而不是ListView本身的 id。你正在通过R.id.list哪个是你的ListView身份证。

尝试传递android.R.layout.simple_list_item_1,这将为您提供每个项目的单行文本布局。

于 2012-06-10T18:24:55.843 回答
0

事实证明,我的问题实际上与我发布的代码无关。问题是我曾尝试doInBackground在 a中添加 UI 元素,AsyncTask而您永远不应该这样做,而是将其放入onPostExecute我将编辑帖子以显示这一点。这是最终代码。

class LoadInteractionsTask extends AsyncTask<String,Integer,Integer>{
    JSONArray interactions;
    boolean loadStatus=false;

    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        // Create a new HttpClient and Post Header
        myLogin.cancel(false);
        HttpGet httpget = new HttpGet("http://www.example.com/device/get_module_list.php");
        Log.d("Debug", "starting loading");

        try {
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity myentity =response.getEntity();
            Log.d("Debug", "response: "+myentity);

            String loginJSON = EntityUtils.toString( myentity );
            JSONTokener myTokener =new JSONTokener(loginJSON);
            //Log.d("Debug", "status: "+myTokener.nextValue());


            //JSONObject
            JSONObject myJSON = (JSONObject) (myTokener).nextValue();
            Log.d("Debug", "status: "+myJSON.getInt("status"));
            if(myJSON.getInt("status")==1){
                interactions=myJSON.getJSONArray("modules");
                loadStatus=true;
            }else{
                Log.d("Debug", "error: "+myJSON.getString("reason"));

            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            Log.d("Debug", "caught ClientProtocolException");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("Debug", "caught IOException: "+e.getMessage());
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if(loadStatus){
            loadInteractions(interactions);
        }
    }


}
于 2012-06-10T19:55:38.210 回答