How can I refresh the list after changes?

2019-08-21 14:16发布

I use my ArrayAdapter expanded getView method. I change CheckBox but the ListView does not refresh

my source

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

        // Planet to display
        UrlItem urlItem = (UrlItem) this.getItem(position);
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.database_table_item, null);
            viewHolder = new ViewHolder();
            viewHolder.checkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);



            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                            int getPosition = (Integer) buttonView.getTag();
                            DatabaseTable.setPosition(getPosition);


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

                                UrlItems.get(i).setUse(false);

                            }

                            UrlItems.get(getPosition).setUse(isChecked);


                        }
                    });

How can I refresh the list after changes ??

for your information in the manifest set

<activity
            android:name=".DatabaseTable"
            android:label="database_table" android:windowSoftInputMode="adjustPan" >

See the link for the upgrade using the button "selectAll" I have exact same idea but without the button selectAll

http://pastebin.com/PgNeDnXq

2条回答
乱世女痞
2楼-- · 2019-08-21 14:51
mAdapter.notifyDataSetChanged();

notify that the data set have changed ; )

查看更多
Luminary・发光体
3楼-- · 2019-08-21 14:54
adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated

Note: If you get any errors with the above line, you could try the following code (where mListView is the name of my ListView object)

((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged(); 

Another way would be to invalidate the List so that it is redrawn form the updated data set once again.

mListView=(ListView) findViewById(R.id.MyListView);
mListView.invalidate();
查看更多
登录 后发表回答