如何在android中实现notifysetDatachanged方法?

时间:2017-02-28 10:09:10

标签: android

我正在开发一个应用程序,其中我有一个列表视图,其中包含来自服务器的一些数据,为此我使用json并且还实现了adapter.notifyDataSetchanged()但我想知道我是否以写入方式进行请检查。

w+

3 个答案:

答案 0 :(得分:0)

首先,您需要在m_VcomAdpter.notifyDataSetChanged(); for之外编写loop,因为它会再次调用您的循环计数。

如果您在api通话后创建或初始化适配器,那么第二件事就是您不需要调用notifyDataSetChanged方法。

因此请在for loop

之后直接调用此方法
     m_VcomAdpter = new CVcomAdapter(HomeScreenActivity.this, s_VcomDataSet);
     mVcomListview.setAdapter(m_VcomAdpter);

如果您修改了阵列,那么第三件事就是像

那样调用
m_VcomAdpter.notifyDataSetChanged() 

e.g。

JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// GETTING DEAL LIST
                    for (int i = 0; i < posts.length(); i++) {
                        try {
                            JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                            m_VcomData = new CVcomStorage();// object create of DealAppdatastorage
                            m_VcomData.setmDealTitle(post.getString(ServerResponseStorage.s_szDEAL_NAME));//getting deal name
                            m_VcomData.setmDealCode(post.getString(ServerResponseStorage.s_szDEAL_CODE));// getting deal code
                            m_VcomData.setmDealValue(post.getString(ServerResponseStorage.s_szDEAL_VAlUE));
                            m_VcomData.setmDescription(post.getString(ServerResponseStorage.s_szDEAL_DETAILS));
                            m_VcomData.setmDealActionUrl(post.getString(ServerResponseStorage.s_szDEAL_ACTION_URL));
                            String logo = post.getString(ServerResponseStorage.s_szDEAL_LOGO);
                            m_VcomData.setmIcon(imgPath + logo);
                            if (BuildConfig.klogInfo)
                                Log.d(m_kTAG, "Logo Path::" + item.getS_szicon());
                            if (BuildConfig.kMonkeyInfo)
                                Log.i("Monkey", "Logo Path::" + item.getS_szicon());
                            if (!s_VcomDataSet.contains(item)) {
                                s_VcomDataSet.add(m_VcomData);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    m_VcomAdpter = new CVcomAdapter(HomeScreenActivity.this, s_VcomDataSet);
                    mVcomListview.setAdapter(m_VcomAdpter);

答案 1 :(得分:0)

notifyDatasetChanged()用于更新ListView项目。

假设您已在案例“s_VcomDataSet”中删除或添加了列表中的内容。

你应该使用notifyDatasetChanged()反映数据的新变化并通知ti listview。

为了更好的可用性,您应该使用RecyclerView代替listview

答案 2 :(得分:0)

您应该在调用notifyDatasetChanged()之前设置适配器。您的代码应该遵循以下几行 -

   CVcomStorage m_VcomData = new CVcomStorage(); 
    m_VcomAdpter = new CVcomAdapter(HomeScreenActivity.this, s_VcomDataSet); 
mVcomListview.setAdapter(m_VcomAdpter);

        JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// GETTING DEAL LIST
                            for (int i = 0; i < posts.length(); i++) {
                                try {
                                    JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                                    // object create of DealAppdatastorage
                                    m_VcomData.setmDealTitle(post.getString(ServerResponseStorage.s_szDEAL_NAME));//getting deal name
                                    m_VcomData.setmDealCode(post.getString(ServerResponseStorage.s_szDEAL_CODE));// getting deal code
                                    m_VcomData.setmDealValue(post.getString(ServerResponseStorage.s_szDEAL_VAlUE));
                                    m_VcomData.setmDescription(post.getString(ServerResponseStorage.s_szDEAL_DETAILS));
                                    m_VcomData.setmDealActionUrl(post.getString(ServerResponseStorage.s_szDEAL_ACTION_URL));
                                    String logo = post.getString(ServerResponseStorage.s_szDEAL_LOGO);
                                    m_VcomData.setmIcon(imgPath + logo);
                                    if (BuildConfig.klogInfo)
                                        Log.d(m_kTAG, "Logo Path::" + item.getS_szicon());
                                    if (BuildConfig.kMonkeyInfo)
                                        Log.i("Monkey", "Logo Path::" + item.getS_szicon());
                                    if (!s_VcomDataSet.contains(item)) {
                                        s_VcomDataSet.add(m_VcomData);
                                        m_VcomAdpter.notifyDataSetChanged();
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }