F
F
Fotonick2016-06-23 23:04:35
JavaScript
Fotonick, 2016-06-23 23:04:35

How to update fragment after AsyncTask called by button in adapter?

UPDATE! The solution advised by Mickey Mouse turned out to be a trick. Firstly, the 8th element of the list may have the position parameter equal to, for example, 1. As I understand it, position is a serial number not in the entire list, but only among the visible elements on the screen. Secondly, at different scrolling speeds, the position value of the same element can be different. For example, if you scroll slowly, then the last element of the list gets position=1, and if you scroll quickly, then position=2. As I understand it, it depends on which already existing list element will be reused for the new one. Ultimately, when processing a button click, the data of the wrong element is retrieved from the arraylist. Tell me how to fix this solution, or how to completely rewrite the processing of buttons so that the correct data is retrieved.
There is one activity. In it, through the NavigationDrawer, fragments with content change. There is one fragment with the list of applications in Listview. Each Listview item contains three buttons.
d09162e407a24e4eab72fcee0ee72776.png
I process button clicks like this

фрагмент адаптера
--------------------------------
            holder.appAccept = (Button) convertView.findViewById(R.id.appAccept);
            holder.appAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            holder.appEdit = (Button) convertView.findViewById(R.id.appEdit);
            holder.appEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            holder.appDelete = (Button) convertView.findViewById(R.id.appDelete);
            holder.appDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    whatAction = "delete";

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(parent.getContext());
                    alertDialog.setTitle("Удалить приложение");
                    alertDialog.setMessage("Вы действительно хотите удалить приложение?");

                    alertDialog.setPositiveButton("Удалить", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            new AdminDecision(position, whatAction, parent.getContext()).execute();
                            MySingleton ms = MySingleton.getInstance();
                            Boolean adminResult = ms.getAdminResult();
                            String feedInput = ms.getAppsUpdate();

                        }
                    });

                    alertDialog.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.appIcon.setVisibility(View.INVISIBLE);
        }
-------------------------

For example, when you click on the delete button, Asynctask is called, which deletes the application from the database. So the problem is that I can’t update this fragment with the list of applications after the end of the asynctask (in fact, the application is no longer in the database, but nothing has changed in the fragment).
It turns out such a sequence of calls - a fragment (filling in data when creating) - an adapter - asynctask of deletion - asynctask of obtaining a new list of data. Fragment, adapter and two asyntasks are separated into different files.
At the end of the last asynctask, I decided to write the status of the successful completion and the received data to a singleton in order to be able to pull them out of any activity.
@Override
        protected void onPostExecute(Void aVoid) {

            MySingleton ms = MySingleton.getInstance();
            ms.setAppsUpdate(jsonFeed.toString());
            ms.setAdminResult(adminResult);
        }

I tried to update the fragment by calling OnResume (recreating the fragment again with a repeated request for data), but it turned out that AlertDialog (to confirm the intention to delete one of the items in the list) does not call OnPause for the fragment, and I did not come up with another automatic fragment update.
Tell me how to be. Looks like I screwed up....

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Taratin, 2015-11-23
@Viccc

$(document).ready(function(){
    for(var i=1;i<=8;i++) {
        var text = $("#primtext"+i),
        	img = $("#prim"+i+" img"),
        	info = $("#priminfo"+i);
        
        text.hide();
        
        $("#prim"+i).hover(
            
        function(text,img,info){
            text.show();
            img.hide();
            info.css({color: "#50cb10"});
        }.bind(null,text,img,info),
            
        function(text,img,info){
            text.hide();
            img.show();
            info.css({color: "#898989"});
        }.bind(null,text,img,info)
        
        );
    }
});

I
Ivanq, 2015-11-23
@Ivanq

$(document).ready(function(){
    for(var i=1;i<=8;i++) {
        $("#primtext"+i).hide();
        $("#prim"+i).mouseenter(function(){
            $("#primtext"+i).show();
            $("#prim"+i+" img").hide();
            $("#priminfo"+i).css("color", "#50cb10");
        });
        $("#prim"+i).mouseleave(function(){
            $("#primtext"+i).hide();
            $("#prim"+i+" img").show();
            $("#priminfo"+i).css("color", "#898989");
        });
    }
});

Sory, didn't check.

M
MikkiMouse, 2016-06-24
@MikkiMouse

You should have data in the adapter, the list of these same applications with which the ListView is filled?
After the AsyncTask completes, you need to remove the "app" from the adapter data and call notifyDataSetChanged.

F
Fotonick, 2016-06-24
@Fotonick

Solution from MikkiMouse
added to the adapter

private Callbacks callbacks;

    public interface Callbacks {
        void onAppAccept(int position);

        void onAppEdit(int position);

        void onAppDelete(int position);
    }

    public void setCallbacks(Callbacks callbacks) {
        this.callbacks = callbacks;
    }

in the same place in the adapter, the button handler calls one of the methods
holder.appEdit = (Button) convertView.findViewById(R.id.appEdit);
            holder.appEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    callbacks.onAppEdit(position);

                }
            });

In the snippet before assigning the adapter
oneAppAdminAdapter = new OneAppAdminAdapter(AllAppsAdminFragment.this, allAppsList);

                        oneAppAdminAdapter.setCallbacks(new OneAppAdminAdapter.Callbacks() {
                            @Override
                            public void onAppAccept(int position) {

                               //вызов асинтаска принятия
                            }

                            @Override
                            public void onAppEdit(int position) {                                

                               //вызов фрагмента редактирования
                            }

                            @Override
                            public void onAppDelete(int position) {
                               //вызов асинтаска удаления
                            }
                        });

                        setListAdapter(oneAppAdminAdapter);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question