0

I have adapter and inside adapter there are asyntask. Show layout badge cant load fast and when i scroll value change randomly.

My adapter

private class MyAdapter extends ArrayAdapter<MyClass> {
        ConvertView holder = null;


        public MyAdapter(Context context, int resource, int textViewResourceId, List<MyClass> objects) {
            super(context, resource, textViewResourceId, objects);
            mInflater = LayoutInflater.from(context);

        }

        public int getCount() {

            return myclassList.size();
        }

        public MyClass getItem(int position) {
            return myclassList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final TextView txtTestKode, txtTestName, txtTestStatus;
            final RelativeLayout layoutBadge;

            final MyClass data = getItem(position);


            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.row_test_transaction, null, false);
                holder = new ConvertView(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ConvertView) convertView.getTag();
            }

            txtTestKode = holder.getTxtTestKode();
            txtTestName = holder.getTxtTestName();
            txtTestStatus = holder.getTxtTestStatus();


            new AsyncTask<String, Void, String>() {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }

                @Override
                protected String doInBackground(String... params) {
                    try {
                        String url = "my_url";
                        WebRequestPost req = new WebRequestPost(url);


                        req.execute();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(req.getResultStream()));
                        String result = reader.readLine();
                        reader.close();
                        req.closeStream();

                        return result;
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (SQLiteConstraintException e) {
                        e.printStackTrace();
                    } catch (SQLiteException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return null;
                }

                @Override
                protected void onPostExecute(String results) {
                    super.onPostExecute(results);
                    if (results != null) {
                        if (Integer.parseInt(results) > 0) {
                            holder.getLayoutBadge().setVisibility(View.VISIBLE);
                        }
                    }
                }
            }.execute();

            return convertView;
        }


    }

and:

public static class ConvertView {
        private View row;

        private RelativeLayout layoutBadge;

        public ConvertView(View row) {
            this.row = row;
        }


        public RelativeLayout getLayoutBadge() {
            if (layoutBadge == null) {
                layoutBadge = (RelativeLayout) row.findViewById(R.id.layoutBadge);
            }
            return layoutBadge;
        }


    }

Thanks

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Nur Sholeh
  • 31
  • 1

1 Answers1

0

you should not do these kinds of task in adapter. what is happening is the data is being fetched asynchronous and may be available in any time later (changing data randolmly). adapter continues to loading other rows data and dont wait for each rows task to be finished becuase you are doing it asynchronous.

the best practice is to provide data before creating the adapter and send the data as a list to adapter as a constructor argument.

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52