如何在android中实现notifysetDatachanged方法?

如何在android中实现notifysetDatachanged方法?,android,Android,我正在开发一个应用程序,其中我有一个listview,其中包含来自服务器的一些数据,为此我使用json并实现了adapter.notifyDataSetchanged(),但我想知道我是否以写入方式进行操作,请检查 JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// GETTING DEAL LIST for (int i = 0; i <

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

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);
                                m_VcomAdpter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    m_VcomAdpter = new CVcomAdapter(HomeScreenActivity.this, s_VcomDataSet);
                    mVcomListview.setAdapter(m_VcomAdpter);
JSONArray posts=response.optJSONArray(ServerResponseStorage.s_szDEAL_数组);//获取交易清单
对于(int i=0;i
首先,您需要编写
m_VcomAdpter.notifyDataSetChanged()
循环
之外,因为它会一次又一次调用循环计数

第二件事,如果在api调用之后创建或初始化适配器,则不需要调用
notifyDataSetChanged
方法

因此,请在
之后直接调用该函数,以进行
循环

     m_VcomAdpter = new CVcomAdapter(HomeScreenActivity.this, s_VcomDataSet);
     mVcomListview.setAdapter(m_VcomAdpter);
第三件事如果你修改了你的数组,你可以像

m_VcomAdpter.notifyDataSetChanged() 
e、 g

JSONArray posts=response.optJSONArray(ServerResponseStorage.s_szDEAL_数组);//获取交易清单
对于(int i=0;i
用于更新ListView项的notifyDatasetChanged()

假设您在案例“SvComDataSet”中删除或添加了列表中的某些内容

您应该使用notifyDatasetChanged()来反映数据中的新更改并通知ti listview


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

您应该在调用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();
                                }
                            }
CVcomStorage m_VcomData=new CVcomStorage();
m_VcomAdpter=新的CVcomAdapter(HomeCreenactivity.this,s_VcomDataSet);
mVcomListview.setAdapter(m_VcomAdpter);
JSONArray posts=response.optJSONArray(ServerResponseStorage.s_szDEAL_数组);//获取交易清单
对于(int i=0;i   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();
                                }
                            }