Zouhair
Zouhair

Reputation: 145

Refresh Fargment from adapter

I know this question has been around, but trying solutions proposed in SO threads weren't helpful! I Have an activity (Favorites) with 3 fragment (represent categories, Video/figures/other) ..in each fragment there is a list of favorites following the category ..
my problem is ..when i delete an item from list of favorite ..it get deleted from database but the listview don't get refresh instantely! ..i have to move between fragments for that to happen .. i tried:

1. notifyDataSetChanged(); //do nothing
2. `refreshEvents(listfavorite);` // after the delete button onclick with refresh events=:
         public void refreshEvents(final List<Data> events)
            {
                this.listfavorite.clear();
        this.listfavorite.addAll(events);
        notifyDataSetChanged(); 
The result of 2nd method: the list is cleared instantely but don't get repopulated again!

    3.  
    //after on delete on click
     Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //listfavorite.addAll(events);
                    notifyDataSetChanged();            }
            }, 1000);

This is my Fragment Adapter

public class FragmentAdapter  extends BaseAdapter {
    private List<Data> listfavorite;
    Context context;
    String name, packagename, id, section;
    Long _id;
    private DBManager dbManager;
    boolean isFavourite;
    private LayoutInflater mInflater;

    public FragmentAdapter(Context FragmentOther,List<Data> resultsFavorite){
        this.listfavorite = resultsFavorite;
        this.context=FragmentOther;
        mInflater = LayoutInflater.from(FragmentOther);

    }

    public void removeItemAtPosition(int position) {
        if (listfavorite != null) {
            listfavorite.remove(position);
            notifyDataSetChanged();
        }
    }

    public void clearAll() {
        if (listfavorite != null) {
            listfavorite.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listfavorite.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return listfavorite.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }


    public View getView(int position, View rowView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if(rowView == null){
            rowView = mInflater.inflate(R.layout.activity_view_record, null);
            holder = new ViewHolder();

            //define those textsview that corresponds to the row views so later we'll retrieve the data from them
            holder.idTextView = (TextView) rowView.findViewById(R.id.id);
            holder.nameTextView = (TextView) rowView.findViewById(R.id.name);
            holder.sectionTextView = (TextView) rowView.findViewById(R.id.section);
            holder.packageTextView = (TextView) rowView.findViewById(R.id.packagename);

            //retrieve to this strings
            id = listfavorite.get(position).getIdFav();
            name = listfavorite.get(position).getNameAct();
            section = listfavorite.get(position).getSectionName();
            packagename = listfavorite.get(position).getPackageAct();

            //This will be used for deleting data, because delete data we'll need to pass a long type variable
            _id = Long.parseLong(id);

            // define the button/textview because the popmenu needs a header!
            holder.viewoption=(TextView) rowView.findViewById(R.id.textViewOptions);
            holder.viewoption.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // so we can use a custom pop menu we'll use this code, first define the contextwrapper
                    // and define the style of popmenu as second argument! , the style should have as parent
                    // Widget.AppCompat.PopupMenu ! then
                    //pass it in popmenu as firstargument
                    // the gravity.right serve to create margin from right side of the screen
                    Context wrapper = new ContextThemeWrapper(context, R.style.YOURSTYLE);
                    //creating a popup menu
                    PopupMenu popup = new PopupMenu(wrapper, holder.viewoption, Gravity.RIGHT);
                    //inflating menu from xml resource
                    popup.inflate(R.menu.options_menu);
                    // this is really important! to show icon you should use this function, because the icons in normal
                    // cases don't show up! (we pass the name of the popupmenu inside
                    setForceShowIcon(popup);
                    //adding click listener
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.delete:
                                    //delete from database
                                    //Open the database
                                    dbManager = new DBManager(context);
                                    dbManager.open();
                                    dbManager.delete(_id);

                                    //change value of isfav
                                    isFavourite = false;

                                    //save it to sharedprefrenece, the state will be read in other activities and
                                    //show the correspondante state False or true
                                    saveState(isFavourite);
                                    refreshEvents(listfavorite);
                                    notifyDataSetChanged();
                                    //update activity so it'll delete it
                                    // onResume();
                                    break;
                                case R.id.gotoact:
                                    Intent access_activity = new Intent();

                                    //we use this type of intent because it allow us to use strins for intent
                                    //after retrieving data from row, we'll pass it as second argument
                                    access_activity.setClassName(context,packagename);
                                    context.startActivity(access_activity);
                                    break;

                            }
                            return false;
                        }
                    });

                    //displaying the popup
                    popup.show();

                }
            });
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        holder.nameTextView.setText(Html.fromHtml(listfavorite.get(position).getNameAct()));
        holder.sectionTextView.setText(listfavorite.get(position).getSectionName());

        return rowView;
    }

    public void refreshEvents(final List<Data> events)
    {

            this.listfavorite.clear();
    this.listfavorite.addAll(events);
    notifyDataSetChanged(); 

    }

my fragment.java

public class FragmentVideosFav extends Fragment {

    private static final String ARG_TITLE = "title";
    private String mTitle;
    //------Define Database
    private DBManager dbManager;
    //
    List<Data> listContact;

    //ListView
    private ListView listView;

    // Adapter
    private FragmentAdapter adapter;

    public FragmentVideosFav (){}

    public static FragmentVideosFav getInstance(String title) {
        FragmentVideosFav fra = new FragmentVideosFav();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TITLE, title);
        fra.setArguments(bundle);
        return fra;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mTitle = bundle.getString(ARG_TITLE);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_other, container, false);
        listContact = Getlistfavorite();
        //define the list
        listView = (ListView) v.findViewById(R.id.list_view);
        //if the list is empty! set the correspondante layout!
        listView.setEmptyView(v.findViewById(R.id.empty));
        // set list to the adapter
        adapter= new FragmentAdapter(getActivity(),listContact);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return v;
    }


    //refresh fragment when switch to ..
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(
                isVisibleToUser);

        if (getFragmentManager() != null) {

            getFragmentManager()
                    .beginTransaction()
                    .detach(this)
                    .attach(this)
                    .commit();
        }
    }



    // init Data of fragment
    private List<Data> Getlistfavorite(){
        List<Data> favoritelist = new ArrayList<Data>();
        dbManager = new DBManager(getActivity());
        dbManager.open();
        Cursor c = dbManager.fetchvideo();
        //startManagingCursor(c);
        int ititle = c.getColumnIndex(_ID);
        int idesc = c.getColumnIndex(ACTIVITY_NAME);
        int isection = c.getColumnIndex(SECTION_NAME);
        int ipath = c.getColumnIndex(PACKAGE_NAME);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            String title = c.getString(ititle);
            String desc = c.getString(idesc);
            String section = c.getString(isection);
            String path = c.getString(ipath);
            favoritelist.add(new Data(title, desc , section , path));}

        return favoritelist;
    }


}

Upvotes: 0

Views: 46

Answers (1)

MidasLefko
MidasLefko

Reputation: 4559

Try this:

View getView(int position, View rowView, ViewGroup parent) {
    Data favorite = listfavorite.get(position);
    // Etc
    case R.id.delete:
        dbManager = new DBManager(context);
        dbManager.open();
        dbManager.delete(_id);

         //change value of isfav
         isFavourite = false;

         //save it to sharedprefrenece, the state will be read in other activities and
        //show the correspondante state False or true
        saveState(isFavourite);

        listfavorite.remove(favorite);
        notifyDataSetChanged();

Upvotes: 2

Related Questions