简体   繁体   中英

Android ListView does not display new items

I have a ListView which is populated based on the value of the selected RadioButton, with the RadioButtons' OnClick events triggering the ListView to refresh with new values.

I have this almost working, but the issue I have is that if the first RadioButton has 6 items to display in the ListView, then the second clicked RadioButton has 10 items to display, the ListView displays the first 6 items of the 10, then the first 4 items again, as if it's wrapping the items. I am populating the ListView using ArrayList<HashMap<String, String>> listValues and a SimpleAdapter and I have checked that listValues does contain 10 distinct values.

The code to display the items is as follows:

    ListView listview = (ListView)getView().findViewById(R.id.my_listview);

    SimpleAdapter simpleAdapt = 
            new SimpleAdapter(getView().getContext(), listValues, 
                               R.layout.my_listitem, 
                               null, null) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HashMap<String, String> listItem = listValues.get(position);

            View v = convertView;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);                
                v = inflater.inflate(R.layout.my_layout, null);

                TextView nameTextview =
                        (TextView)v.findViewById(R.id.my_textview);
                nameTextview.setText(listItem.get("name"));

                TextView detailTextview =
                        (TextView)v.findViewById(R.id.my_textview_2);
                detailTextview.setText(listItem.get("detail"));

                if (listItem.get("main_image") != null) {
                    int imageId = Integer.valueOf(listItem.get("main_image"));
                            (ImageView)v.findViewById(R.id.my_imageview);
                    image.setImageResource(imageId);
                }
            } 
            return v;
        };
    };

    listview.setAdapter(simpleAdapt);

Is there an obvious reason for the repeating list items?

The way you are handling the getView method is not that efficient. You should give a try to ViewHolder pattern. The Link below is a good practice that you can follow.

http://www.vogella.com/articles/AndroidListView/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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