使用 AsyncTask 将项目单独添加到列表视图

发布于 2024-09-07 23:57:52 字数 3420 浏览 3 评论 0原文

我正在尝试一项一项地添加 ListView 项目。因此,如果我说 - 60 个项目 - 应用程序会将视图一次添加到列表视图中 - 从而向用户显示应用程序正在加载更多内容。

这是我的代码:

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()));
    }
}

所以,看起来我的代码正确打印了视图,并将它们正确地放入列表中,但是我的活动没有显示任何内容。我做错了什么?我认为之前设置适配器 - 当列表中没有视图时 - 然后更新列表已更改的适配器是正确的方法....显然不是..任何人都可以帮助我吗?

如果您需要澄清我的问题,请告诉我。

编辑:这是补充我的问题的适配器代码。

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;
    }

}

我的输出不输出任何 Log.v 语句,甚至在 GetView 方法内部也不显示它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-09-14 23:57:52
  • 肯定有什么问题
    使用您的适配器。你永远不应该
    保存应显示在
    ListView 靠你自己。仅保存
    创建所需的数据
    查看并覆盖getView方法
    您的适配器来创建所需的
    看法。

  • 第二件事是您要的列表
    在后台使用的任务是

    范围内可见
    仅限 doInBackground 方法。没有变化
    此列表可能会影响
    你的用户界面。

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

关于 Adapter 类的命名有一些不相关的事情。您将其称为 createSpecialAdapter。这听起来像一个方法名,但它是一个类名。尝试以大写字母开头所有类名称,并为它们提供它们所代表的事物的名称,例如 SpecialAdapter。

  • There is certainly something wrong
    with your Adapter. You should never
    save Views that should displayed in a
    ListView on your own. Only save the
    data that is needed to create the
    View and overwrite the getView method
    of your Adapter to create the needed
    view.

  • The second thing is the list that you
    are using in the background task is
    visible in the scope of the
    doInBackground method only. No change
    to this list can have an effect on
    your UI.

  • You are passing the JsonArray to your Adapter before starting the background thread. If your Adapter would be working correctly the ListView should be fully functional before the background thread even starts working. You should pass an empty JsonArray into the ListView and if you are extending an ArrayAdapter you can use addItem in your publish progress method.

Some a little bit unrelated things about the naming of your Adapter class. You called it createSpecialAdapter. This sounds like a method name but it is a classname. Try to start all class names with an uppercase letter and give them names of the things they represent like SpecialAdapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文