notifyDataSetChanged() without refreshing the UI?

会有一股神秘感。 提交于 2019-12-04 18:36:24

Better to have one boolean field in your Guest Class :isPresent. whenever user taps on list item you can get the selected item using adapter.getItemAtPosition(). update the value isPresent to true. and make show the tick mark.

In your adapter class. check for isPresent value. If it is marked to true then show the tick mark else hide it.

This is how you can achieve the both. Show Tick Mark on ListItem click and if you scroll the listview and come back to the same item you tickmark show/hide will be taken care by Adapter.

you could retain the position like this

// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// ...

// restore
mList.setSelectionFromTop(index, top);

Actually possible if you don't want to make a "selected item" here is the code

public void updateItem(ListView listView, Activity activity) {
        if (mData == null) return;

        DebugLog.i("A", "firstCell: " + listView.getFirstVisiblePosition() + " lastCell: " + listView.getLastVisiblePosition());
        for (int firstCell = listView.getFirstVisiblePosition(); firstCell <= listView.getLastVisiblePosition(); firstCell++) {
            final DataItem item = (DataItem) getItem(firstCell); // in this case I put the this method in the Adapter and call it from Activity where the adapter is global varialbe
            View convertView = listView.getChildAt(firstCell);
            if (convertView != null) {

                final TextView titleTextView = (TextView) convertView.findViewById(R.id.item_title);
                    // here is the most important to do; you have to use Main UI thread to update the view that is why you need activity parameter in the method                
                    mActivity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            titleTextView.setText( item + " updated");
                        }
                    });

                }
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!