-1

在 Java 类中,我有三个 List 作为类中的字段。所以我相信我让全班都可以使用它们?和所有内部类?

但是,这不起作用:(例如,我将在这里只显示一个列表,而不是全部三个)

class Items extends ListActivity {

List<String> items = null;  // field

Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // stuff

    items = new ArrayList<String>();

    new Task().execute();

    Log.d("Size", String.valueOf(items.size()));



 }

 class Task extends AsyncTask<String, String, Void> {

    // in this subclass, I parse items from JSON and add to List "items".

     items = add(item);

 }

退出任务后,我似乎丢失了列表中的项目。它记录“大小 0”;如果我在任务中执行此操作,它会显示适当数量的项目。我在这里错过了关于 List 变量范围的一个非常基本的观点吗?

编辑:下面是完整的任务类(稍微清理一下以供发布)

class Task extends AsyncTask<String, String, Void> {
    private InputStream is = null;
    private String result = "";


    @Override
    protected Void doInBackground(String... params) {
        String url_select = "items.php";

        param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("category", Category));

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // read content
            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;

    }

    protected void onPostExecute(Void v) {

        try {

            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                item = json_data.getString("item");

                items.add(item);

                Log.d("Items", item);
            }
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), "No items!",
                    Toast.LENGTH_SHORT).show();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }


    }
}
4

2 回答 2

3

我不是 Android 开发人员,但不是AsyncTask像 new Thread 这样的东西吗?如果是,那么您只会看到“大小 0”,因为在更新您的列表Log.d("Size", String.valueOf(items.size()));之前已执行。new task().execute();

于 2012-07-22T23:41:13.287 回答
1

您需要通读本教程

基本上,默认访问级别是package private(我的描述可能是错误的),但基本上这意味着只要您在同一个包中,您就可以看到该成员,但它对子类不可见。

尝试改用protected访问级别

于 2012-07-22T23:35:37.920 回答