Setting answers dynamically to questions generated from RecyclerView using post method

90
August 13, 2019, at 06:20 AM

I am creating a survey app in a dual pane layout. Right pane displays questions generated from RecyclerView with fields like EditText, RadioButton etc. The Left pane is a list of sections corresponding to which questions get displayed on the right pane.

I am trying to provide a draft functionality such that if a user moves to next section, without filling all the answers to previous section, and gets back again, the answers would be dynamically filled to the each question, just in the state he left before. The only option seems to be

recyclerview.findViewHolderForAdapterPosition()

but it is likely to throw null if the view is not laid out yet. To tackle this, there are 3 options I found so far: 1. Use handler with post delayed 2. Use Viewtreeobserver 3. Use View.post() method

I prefer using post method, but there is one problem. The number of questions in all sections is ~30 and to dynamically set the answer I'll have to loop through this method like this:

  for (Question question: questions) {
        parent.post(new Runnable() {
            @Override
            public void run() {
                RecyclerView.ViewHolder holder = parent.findViewHolderForAdapterPosition(rightPaneList.indexOf(question));
                if (holder instanceof QuestionsAdapter.EditableItemViewHolder) {
                    ((QuestionsAdapter.EditableItemViewHolder) holder).editText.setText(question.getAnswer());
                }
                if (holder instanceof QuestionsAdapter.TwoOptionsViewHolder) {
                    ((QuestionsAdapter.TwoOptionsViewHolder) holder).options.check(question.getAnswerIndex());
                }
            }
        });
    }

So if there are 30 questions that would make it 30 runnables in a queue. And in worst case scenario if someone just tries to play around with left pane list of half filled sections very quickly switching from one to another, I have a feeling its going to crash because of too many threads all getting stacked up.

Even if the OS was faster than the mad user, theoretically it doesn't seem like the right choice, its not scalable. What if there were 100 questions per section? 1000? But then the same is true for handler way of going and I read somewhere ViewTreeObserver is somewhat costly. Is there a better way to do this? Or does android manage posts differently and I need not worry at all? Thanks

Rent Charter Buses Company
READ ALSO
My app fails with Parcel error when Implementing startForResult

My app fails with Parcel error when Implementing startForResult

I've got a Container with multiple fragments

115
Is there a way for webView to permanently save and store caches from a service worker?

Is there a way for webView to permanently save and store caches from a service worker?

I've got a webapp for android, which is using webview to display my webpageMy webpage has a service worker on index

138
How to rotate one circle anti clockwise canvas android

How to rotate one circle anti clockwise canvas android

I can't solve problem and I need helpI create planets around sun to rotate and I need one planet t rotate anti clockwise and my problem begins

120
How to detect key-guard state in android

How to detect key-guard state in android

I have an application that requires to detect the previous state of the key-guard before calling disableKeyguard() or reenableKeyguard(), set by the user! is there any way of accomplishing this task?

155