F
F
Fotonick2016-07-12 20:40:22
Android
Fotonick, 2016-07-12 20:40:22

How to fix button processing?

according to the local hint, I implemented the processing of three buttons inside the list element using Callback

в адаптер добавил
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;
    }


там же в адаптере обработчик кнопки вызывает один из методов
holder.appEdit = (Button) convertView.findViewById(R.id.appEdit);
            holder.appEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    callbacks.onAppEdit(position);

                }
            });


Во фрагменте перед назначением адаптера

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);


извлекаю данные из списка во фрагменте так
OneAppClass op = (OneAppClass) allAppsList.get(position);

But the solution turned out to be a trick. Firstly, the 8th element of the list may have the position parameter equal to, for example, 1. Secondly, at different scrolling speeds, the position value of the same element may 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. The problem was not noticeable until all list items fit on the screen without scrolling.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fotonick, 2016-07-12
@Photonick

Всё оказалось элементарно. Надо было просто в каждом обработчике кнопки перед вызовом callback извлечь нужный элемент из arraylist. И передавать в колбеке не position а сам oneAppClass
oneAppClass = (OneAppClass) getItem(position);
callbacks.onAppEdit(oneAppClass);
Соответственно
public interface Callbacks {
void onAppAccept(OneAppClass oneAppClass);
void onAppEdit(OneAppClass oneAppClass);
void onAppDelete(OneAppClass oneAppClass);
}
А иначе им оставался последний загруженный элемент списка, что логично не соответствовало нажатой кнопке.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question