0

I am trying to add ListView items one by one. So if I have say -- 60 items -- the application would add the views to the list view one at a time -- thus showing the user that the application is loading more things.

This is my code:

try {
        JSONArray j = getTaggsJSON();
        Log.v(TAG, String.valueOf(j.length()));
        a = new createSpecialAdapter(this, R.layout.individual_tagg_view,
                R.layout.list_item, view, j);
        ListView v = this.getListView();
        v.setStackFromBottom(true);
        setListAdapter(a);
        new addViewsToList().execute(j);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServiceException e) {
        e.printStackTrace();
    }

}

private class addViewsToList extends AsyncTask<JSONArray, View, List<View>> {

    protected List<View> doInBackground(JSONArray... jsonArrays) {
        List<View> v = new ArrayList<View>();
        Log.v(TAG, String.valueOf(jsonArrays[0].length()));
        for (int x = 0; x < jsonArrays[0].length(); x++) {
            try {
                Log.v(TAG, jsonArrays[0].getJSONObject(x).toString());
                v.add(ViewAdapter.createTaggView(jsonArrays[0]
                        .getJSONObject(x), c));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            publishProgress(v.get(x));
        }
        return v;
    }

    protected void onProgressUpdate(View... v) {
        Log.v(TAG, "I'm updating my progress!");
        a.notifyDataSetChanged();
    }

    protected void onPostExecute(List<View> views) {
        Log.v(TAG, "i'm done!");
        Log.v(TAG, String.valueOf(views.size()));
    }
}

So, it looks like my code is printing out the views correctly, and putting them in the list correctly, however my activity displays nothing. What am I doing wrong? I thought setting the adapter before -- when there are no views in the list -- and then updating the adapter that the list was changed was the right way to go.... Obviously not.. Can anyone help me?

If you need clarification on my questions please let me know.

EDIT: Here is the adapter code to supplement my question.

private class SpecialAdapter extends ArrayAdapter<JSONArray> {

    JSONArray json;

    public SpecialAdapter(Context context, int textViewResourceId,
            int listItem, JSONArray j) {
        super(context, textViewResourceId);
        this.json = j;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Log.v(TAG,"I'm inside the getView method.");
        try {
            JSONObject info = json.getJSONObject(position);
            Log.v(TAG,info.toString());



        if (convertView == null) {
            LayoutInflater i = (LayoutInflater) RecentTaggs.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = i.inflate(R.layout.individual_tagg_view, parent,
                    false);
            holder = new ViewHolder();
            holder.username = (TextView) convertView
                    .findViewById(R.id.userName);
            holder.review = (TextView)convertView.findViewById(R.id.review_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.review.setText(info.getString("review"));

        return convertView;

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

My output doesn't output any of my Log.v statements, and doesn't show that its even inside the GetView method.

4

1 回答 1

1
  • 您的适配器肯定有问题。你永远不应该自己保存应该在 ListView 中显示的视图。只保存创建视图所需的数据并覆盖适配器的 getView 方法以创建所需的视图。

  • 第二件事是您在后台任务中使用的列表 仅在 doInBackground 方法
    的范围内可见。
    对此列表的任何更改都不会影响
    您的 UI。

  • 在启动后台线程之前,您将 JsonArray 传递给您的适配器。如果您的适配器可以正常工作,则 ListView 应该在后台线程开始工作之前完全正常工作。您应该将一个空的 JsonArray 传递给 ListView,如果您正在扩展ArrayAdapter,您可以在发布进度方法中使用 addItem。

关于适配器类的命名有些无关紧要的事情。您将其称为 createSpecialAdapter。这听起来像一个方法名,但它是一个类名。尝试以大写字母开头所有类名,并为它们指定它们所代表的事物的名称,例如 SpecialAdapter。

于 2010-07-06T06:29:31.470 回答