Listview需要永远加载到模拟器中

时间:2012-06-10 18:20:11

标签: android

我一直在运行此代码(在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");

}

2 个答案:

答案 0 :(得分:1)

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

ArrayAdapter构造函数的第二个参数应该是单独的ListView项布局,而不是ListView本身的id。您正在传递R.id.list id ListView的{​​{1}}。

尝试传递android.R.layout.simple_list_item_1,而不是为每个项目提供单行文字布局。

答案 1 :(得分:0)

事实证明,我的问题实际上与我发布的代码无关。问题是我曾尝试在doInBackground 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);
        }
    }


}