Not able to display item in SwipeFlingAdapterView

898 views Asked by At

I am using SwipeFlingAdapterView and I am fetching data from the MySQL database.

Variables:

private ArrayList<String> al;
    private ArrayAdapter<String> arrayAdapter;
    private int i;
    SwipeFlingAdapterView flingContainer;


JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;

    // url to get all products list
    private static String url_all_products = "http://example.com/items.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "items";

    private static final String TAG_NAME = "name";


    // products JSONArray
    JSONArray products = null;

When I am using below code then it will bind data :

al.add("php");
al.add("c");
al.add("python");
al.add("java");

// choose your favorite adapter
arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
        R.layout.item, R.id.tv_portal, al);

// set the listener and the adapter
flingContainer.setAdapter(arrayAdapter);
flingContainer
        .setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the
                // Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                al.remove(0);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                // Do something on the left!
                // You also have access to the original object.
                // If you want to use it just cast it (String)
                // dataObject
                Toast.makeText(MainActivity.this, "Left!",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                Toast.makeText(MainActivity.this, "Right!",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                /*
                 * al.add("XML ".concat(String.valueOf(i)));
                 * arrayAdapter.notifyDataSetChanged(); Log.d("LIST",
                 * "notified"); i++;
                 */
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
                // TODO Auto-generated method stub

            }
        });

// Optionally add an OnItemClickListener
flingContainer
        .setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition,
                    Object dataObject) {
                // Toast.make(MainActivity.this, "Clicked!");
            }
        });

It is loading all right.

But when I used below code, it is not bind the data even no error comes:

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("itemid", "1"));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_items, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

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

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {

                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable

                    String name = c.getString(TAG_NAME);


                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value

                    map.put(TAG_NAME, name);



                     al.add(name);

                }
            } else {

            }

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

        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products

        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

           arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
            R.layout.item, R.id.tv_portal, al);

    // set the listener and the adapter
    flingContainer.setAdapter(arrayAdapter);
                Toast.makeText(getApplicationContext(), "Done",
                        Toast.LENGTH_LONG).show();

            }
        });

    }
}

It is not loading the data but it shows the toast done.

I am using this - https://github.com/Diolor/Swipecards

How can I do this?

1

There are 1 answers

0
Asad35Waheed On

I also faced the same issue. Here's how I solved it. After this line

flingContainer.setAdapter(arrayAdapter);

Add this line

arrayAdapter.notifyDataSetChanged();