从服务器获取数据时,将arraylist大小设为ZERO?

时间:2014-06-01 14:25:48

标签: android

我正在尝试在daynamic列表视图中显示数据。我从服务器数据库中获取数据。但是我的代码中的问题是task_kist是一个字符串类型的arraylist。当我执行

new TaskList().execute(); 

之前

Log.i("Total size", String.valueOf(task_list.size()));

此行在" new TaskList()。execute();"之前执行。 。在类TaskList中,size或task_list是11但是当我检查上面提到的总大小时,它是0.任何人都可以建议我解决为什么task_list的大小为零而我在其中添加了一些值。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tasklist);

     Bundle bundle = getIntent().getExtras();
        username = bundle.getString("username");

        Log.i("username from login", username);

        registerButtons();

        new TaskList().execute();
        Log.i("Totalllllll size", String.valueOf(task_list.size()));



        String str[] = new String[5];

        for(int i =0; i<5;i++) {
            str[i]=String.valueOf(task_list.size());
        }

        ArrayAdapter<String> ap = new ArrayAdapter<String>(this,android.R.layout.test_list_item,str);
        lv.setAdapter(ap);


        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {


                Toast.makeText(AllTask.this, "You have Selected : "+((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
            }
        });


  }
 /**
 * Background Async Task to Create new product
 * */
class TaskList extends AsyncTask<String, String, String> {

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", username));


        // getting JSON Object
        // Note that create product url accepts POST method
        json = jsonParser.makeHttpRequest(url_get_tasklist,
                "GET", params);

        // check log cat fro response
        Log.i("Create Response", json.toString());

        // check for success tag
        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);


            if (success == 1) {


                tasks = json.getJSONArray(TAG_NAME);

                // looping through All members
                for (int i = 0; i < tasks.length(); i++) {


                    JSONObject c = tasks.getJSONObject(i);


                    // Storing each json item in variable
                    tasklist = c.getString(TAG_TASKLIST);



                    task_list.add(tasklist);
                    Log.i("size", String.valueOf(task_list.size()));
                    Log.i("array", task_list.get(i));


                }


            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return null;
    }


}

1 个答案:

答案 0 :(得分:0)

这是因为您在后台线程中运行的异步任务中获得大小。在完成异步任务之前,您的日志打印值为0 因此,跟踪doInBackground for循环登录为

// looping through All members
                for (int i = 0; i < tasks.length(); i++) {
//your code 
}
Log.i("Total size", String.valueOf(task_list.size()));  // track log here

修改
您也可以在方法中跟踪日志onPostExecute

@Override
        protected void onPostExecute(String result) {
            Log.i("Total size", String.valueOf(task_list.size()));  // track log here
}