简体   繁体   English

如何在Listview中更新特定行

[英]How to update the particular row in Listview

I have implemented list view which scroll vertically, in each row there is a items like apple, lemon etc those items are scroll horizontally. 我实现了垂直滚动的列表视图,每行中都有一个项目,例如苹果,柠檬等,这些项目是水平滚动的。 Now my problem is i need to sort the items in selected row and i need to update the result to that row here i am not able to update the values to particular row.First time i am loading a data from activity to adapter and sorted values i am loading in adapter class. 现在我的问题是我需要对选定行中的项目进行排序,并且需要将结果更新到该行,在这里我无法将值更新到特定行。我在适配器类中加载。

Here code is.. 这里的代码是..

public static ArrayList<GroupsModel> CustomListViewValuesArr;
        public static ArrayList<ItemsModel> ItemsArr;

        public MyDisplay_Adapter(Context a, ArrayList d) {

            context = a;
            data = d;

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        public int getCount() {
            if (data.size() <= 0)
                return 1;
            return data.size();
        }

        public final Object getItem(int position) {
            return data.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }

        public static class ViewHolder {

            public TextView tvGroupTitle, tvGroupDescription, 
            public LinearLayout imgscrollchild;

        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            Log.d("getview:", "position=" + position);
            vi = convertView;
            final ViewHolder holder;

            if (convertView == null) {

                vi = inflater.inflate(R.layout.my_asset_list_item, null);
                holder = new ViewHolder();

                holder.tvGroupTitle = (TextView) vi.findViewById(R.id.tvGroupTitle);

                holder.tvGroupDescription = (TextView) vi
                        .findViewById(R.id.tvGroupDescription);
                holder.imgscrollchild = (LinearLayout) vi
                        .findViewById(R.id.imgscrollchild);

                vi.setTag(holder);

            } else {

                holder = (ViewHolder) vi.getTag();

            }
            if (data.size() <= 0) {

                vi.setVisibility(View.GONE);

            } else {

                tempValues = null;
                tempValues = (GroupsModel) data.get(position);

                holder.tvGroupTitle.setText(tempValues.getName().toString());
                holder.tvGroupDescription.setText(tempValues.getDescription()
                        .toString());


                if (tempValues.getItemmodel().size() == 0) {

                    return vi;

                } else {

                    for (int i = 0; i < tempValues.getItemmodel().size(); i++) {

                        a = i;
                        Log.i("Index", String.valueOf(i));
                        holder.imgscrollchild.addView(createimg(
                                tempValues.getItemmodel(), a));
                        Log.i("Index A", String.valueOf(a));

                    }
                }

            }
            return vi;
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public View createimg(final List<ItemsModel> bm, final int pos) {

            Here dynamically creating a imageview and textview


            return Hlayout;

        }



        private class Sort extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                String json = null;

                try {

                    String url_get = "my url"

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url_get);
                    HttpResponse response = httpclient.execute(httpGet);
                    StatusLine statusLine = response.getStatusLine();

                    int statusCode = statusLine.getStatusCode();
                    if (statusCode == 200) {

                        HttpEntity entity = response.getEntity();
                        json = EntityUtils.toString(entity);

                        try {

                            JSONArray array = new JSONArray(json);
                            ItemsArr = new ArrayList<ItemsModel>();
                            for (int i = 0; i < array.length(); i++) {

                                JSONObject Obj = array.getJSONObject(i);

                                String GName = Obj.getString("GName");
                                String GDesc = Obj.getString("GDesc");

                                ItemsArr.add(ItemsList);

                            }

                            GroupsModel list1 = new GroupsModel();
                            list1.setItemmodel(ItemsArr);
                            CustomListViewValuesArr = new ArrayList<GroupsModel>();
                            CustomListViewValuesArr.add(list1);

                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("Exception", e.toString());
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {

                adapter = new MyDisplay_Adapter(context,CustomListViewValuesArr);
                adapter.notifyDataSetChanged();

            }
        }



        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    }

You shouldn't create a new adapter instance on postExecute of your async task. 您不应在异步任务的postExecute上创建新的适配器实例。 You sholud create a method to change data on your adapter instead. 您应该创建一种方法来更改适配器上的数据。 And call it on PostExecute like: 并像这样在PostExecute上调用它:

    adapter.setData(myData);

    adaptar.notifyDatasetChanged();

If you don't want to do it like this and if you need to recreate an adapter you should reset it like: 如果您不想这样做,并且需要重新创建适配器,则应按以下步骤进行重置:

    listView.setAdapter(adapter);

Note: Code examples may be different from exact code. 注意:代码示例可能与确切的代码不同。

将更新的对象更新到arraylist,您可以通过清除arraylist中的所有数据并将更新的数据添加到同一列表中来执行此操作,也可以通过检查要替换的对象并使用adapter.notifyDataSetChanged()来做到这一点。 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM