无法在SwipeFlingAdapterView中显示项目

时间:2015-06-10 05:30:04

标签: android json android-activity android-arrayadapter android-custom-view

我正在使用SwipeFlingAdapterView,我从MySQL数据库中获取数据。

变量:

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;

当我使用下面的代码时,它将绑定数据:

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!");
            }
        });

装载好了。

但是当我使用下面的代码时,即使没有错误,它也不会绑定数据:

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

            }
        });

    }
}

它没有加载数据,但它显示了吐司done

我正在使用此作品 - https://github.com/Diolor/Swipecards

我该怎么做?

1 个答案:

答案 0 :(得分:3)

我也面临同样的问题。这是我如何解决它。 在此行之后

flingContainer.setAdapter(arrayAdapter);

添加此行

arrayAdapter.notifyDataSetChanged();