无法从阵列适配器设置SwipeFlingAdapterView

时间:2015-07-25 11:24:00

标签: android android-fragments android-arrayadapter swipe android-json

我使用了https://github.com/Diolor/Swipecards,如果数据是静态的,但它无法与AsyncTask

一起使用,则效果很好

现在我想用json数据绑定适配器。但它没有用。

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

        }
    }

适配器方法:

    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="";
}

当我运行上面的代码时。它什么都不会显示。

即使此代码成功运行,但未将数据绑定到适配器。

logcat中没有显示错误。

我还有debug代码。

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

        }

它不执行getView方法

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

我怎样解决这个问题?

3 个答案:

答案 0 :(得分:6)

您只需将更新的数据集通知适配器

使用

notifyDataSetChanged();

notifyDataSetInvalidated()

答案 1 :(得分:5)

我已经解决了这个问题。

我刚刚将 - arrayAdapter.notifyDataSetChanged(); onPostExecute。

关于onPostExecute的新更新代码:

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

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

完成了。

答案 2 :(得分:1)

您检查了依存关系吗?在build.gradle中?

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

将这些行导入到build.gradle(app)中,很可能你会没事的