Not able to set SwipeFlingAdapterView from array adapter
Asked Answered
H

3

8

i have use the https://github.com/Diolor/Swipecards and it works perfect if data is static but it is not working with AsyncTask

Now i want to bind the adapter with json data. but it is not worked.

public class HomeFragment extends Fragment {

    private Context context;

    private ArrayList<Productp> al;
    private ArrayAdapter<Productp> arrayAdapter;

    SwipeFlingAdapterView flingContainer;

    ImageView img_product;

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();
    private static String url_all_products = "http://www.example.com/prod.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_IMAGE = "url";

    JSONArray products = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);
        context = getActivity();

        initUI(rootView);
        return rootView;
    }

    private void initUI(View view) {

        flingContainer = (SwipeFlingAdapterView) view.findViewById(R.id.frame);


        new Loaditems().execute();


    }

    class Loaditems extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(context);
            pDialog.setMessage("Loading products. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }

        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("range", "1"));

            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
                    params);

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    products = json.getJSONArray(TAG_PRODUCTS);

                    al = new ArrayList<Productp>();

                    for (int i = 0; i < products.length(); i++) {

                        JSONObject c = products.getJSONObject(i);

                        Productp pp = new Productp();

                        pp.portal = c.getString(TAG_IMAGE);

                        al.add(pp);
                    }
                } else {

                }

            } catch (JSONException e) {
                e.printStackTrace();

            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            arrayAdapter = new ProductAdpater(context, al);
            flingContainer.setAdapter(arrayAdapter);

        }
    }

adapter method:

    private class ProductAdpater extends ArrayAdapter<Productp> {

        Context mContext;

        public ProductAdpater(Context context, List<Productp> items) {
            super(context, R.layout.row_homeview, items);

            try {
                this.mContext = context;

            } catch (Exception e) {

                e.printStackTrace();
            }

        }

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

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

        public View getView(final int position, View convertView,
                ViewGroup parent) {

            View view = convertView;
            ViewHolder holder = null;

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (view == null) {
                view = mInflater.inflate(R.layout.row_homeview, parent, false);
                holder = new ViewHolder();
                holder.imgIcon = (ImageView) view
                        .findViewById(R.id.addviewimage);

                view.setTag(holder);
            } else
                holder = (ViewHolder) view.getTag();
            UrlImageViewHelper.setUrlDrawable(holder.imgIcon,
                    al.get(position).portal);
            return view;
        }

        class ViewHolder {

            ImageView imgIcon;

        }

    }

    }

productp class:

public class Productp  {


    public String portal="";
}

When i run the above code. it will display nothing.

Even this code run successfully but not bind the data to the adapter.

There are no error show in logcat.

I have also debug the code.

the execution exit from 

public ProductAdpater(Context mcontext, ArrayList<Productp> items) {
            super(mcontext, R.layout.row_homeview, items);

            try {
                context = mcontext;
                // this.list = items;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }

It it not execute the getView method

public View getView(final int position, View convertView,
            ViewGroup parent) {}

How can i solved this ?

Hudis answered 25/7, 2015 at 11:24 Comment(5)
Try extending BaseAdapter once instead of ArrayAdapterHeraclea
Tried that already. But still not able to bind data.Hudis
Okay. And are you sure items array is not empty and atleast has size > 1?Heraclea
Yes i have debug the whole code. it is not empty. When i wrote the whole coding into onPostExecute instead of doInBackgroud then it is working. But when i write the code into doInback then it not working.Hudis
@AbhishekV check my update answer. :-) i have solved itHudis
H
4

i have solved this.

I have just put the - arrayAdapter.notifyDataSetChanged(); onPostExecute.

My new updated code on onPostExecute :

 protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            arrayAdapter = new ProductAdpater(context, al);
            flingContainer.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
        }

It is done.

Hudis answered 28/7, 2015 at 8:3 Comment(0)
L
6

You just need to notify the updated data set to the adapter

by using

notifyDataSetChanged();

notifyDataSetInvalidated()

Labellum answered 28/7, 2015 at 13:6 Comment(1)
Yes its fine i just give you the explanation for calling notifyDataSetChanged(); notifyDataSetInvalidated()Labellum
H
4

i have solved this.

I have just put the - arrayAdapter.notifyDataSetChanged(); onPostExecute.

My new updated code on onPostExecute :

 protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            arrayAdapter = new ProductAdpater(context, al);
            flingContainer.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
        }

It is done.

Hudis answered 28/7, 2015 at 8:3 Comment(0)
G
0

Have you checked dependancies? in build.gradle?

dependencies { compile 'com.lorentzos.swipecards:library:1.0.9' }

import these lines in your build.gradle(app) and most probably you will be fine

Giggle answered 21/10, 2019 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.