如何在android中使用改造来检索数据?

时间:2017-02-04 07:39:01

标签: android gridview retrofit2

我是新来的改造。我想使用改造设置我的GridView,但我不知道如何使用改造来设置GridView适配器。 这是我的JSON输出:

{
  "status_code": 200,
  "status": "OK",
  "status_message": "Success",
  "pigeon_list": [
    {
      "id": "1",
      "pigeon_name": "sofiee",
      "auth_token": "58809c7129a5a",
      "country_code": "AE",
      "strain_id": "75",
      "distance": "3",
      "pigeon_price": "50.00",
      "pigeon_details": "One of the best ",
      "image": "http:.98a8ac5.jpeg",
      "pedigree_image": "http://...1.jpeg",...
      "status": "",
      "created": "2017-01-19 16:52:14",
      "updated": "0000-00-00 00:00:00",
      "strain_name": "Janssen/gaston wowers ",
      "usr_mobile": "+971/12345678",
      "usr_image": "http://....19a.jpeg",
      "usr_email": "...edo@gmail.com"
    },

以下是我需要API数据的方法:

private void filter() {
        //Showing a progress dialog while our app fetches the data from url
        final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
        String DATA_URL = "http......../searchPigeonList";
        final String lstrain = (String)lsStrain.getSelectedItem();
        final String ldistance = (String)lsDistance.getSelectedItem();
        final String lcountry = (String)lsCountry.getSelectedItem();

        RestAdapter radapter=new RestAdapter.Builder().setEndpoint(DATA_URL).build();
        MInterface restInt=radapter.create(MInterface.class);
        restInt.getUser(new Callback<POJO>() {
             @Override
        public void success(POJO pojo, retrofit.client.Response response) {

            pid2.add(pojo.getpigeon_detail);

            }

当我尝试这个时,它在getpigeon_detail上给了我红线。它说不能解决这个方法。这是设置阵列适配器的正确方法吗? 我使用了这个例子http://mobilesiri.com/retrofit-tutorial-android-studio/

我的POJO课程:

public class POJO {





        @SerializedName("id")
        @Expose
        private String id;
        @SerializedName("pigeon_name")
        @Expose
        private String pigeonName;
        @SerializedName("auth_token")
        @Expose
        private String authToken;
        @SerializedName("country_code")
        @Expose
        private String countryCode;
        @SerializedName("strain_id")
        @Expose
        private String strainId;
        @SerializedName("distance")
        @Expose
        private String distance;
        @SerializedName("pigeon_price")
        @Expose
        private String pigeonPrice;
        @SerializedName("pigeon_details")
        @Expose
        private String pigeonDetails;
        @SerializedName("image")
        @Expose
        private String image;
        @SerializedName("pedigree_image")
        @Expose
        private String pedigreeImage;
        @SerializedName("status")
        @Expose
        private String status;
        @SerializedName("created")
        @Expose
        private String created;
        @SerializedName("updated")
        @Expose
        private String updated;
        @SerializedName("strain_name")
        @Expose
        private String strainName;
        @SerializedName("usr_mobile")
        @Expose
        private String usrMobile;
        @SerializedName("usr_image")
        @Expose
        private String usrImage;
        @SerializedName("usr_email")
        @Expose
        private String usrEmail;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPigeonName() {
            return pigeonName;
        }

        public void setPigeonName(String pigeonName) {
            this.pigeonName = pigeonName;
        }

        public String getAuthToken() {
            return authToken;
        }

        public void setAuthToken(String authToken) {
            this.authToken = authToken;
        }

        public String getCountryCode() {
            return countryCode;
        }

        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }

        public String getStrainId() {
            return strainId;
        }

        public void setStrainId(String strainId) {
            this.strainId = strainId;
        }

        public String getDistance() {
            return distance;
        }

        public void setDistance(String distance) {
            this.distance = distance;
        }

        public String getPigeonPrice() {
            return pigeonPrice;
        }

        public void setPigeonPrice(String pigeonPrice) {
            this.pigeonPrice = pigeonPrice;
        }

        public String getPigeonDetails() {
            return pigeonDetails;
        }

        public void setPigeonDetails(String pigeonDetails) {
            this.pigeonDetails = pigeonDetails;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getPedigreeImage() {
            return pedigreeImage;
        }

        public void setPedigreeImage(String pedigreeImage) {
            this.pedigreeImage = pedigreeImage;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getCreated() {
            return created;
        }

        public void setCreated(String created) {
            this.created = created;
        }

        public String getUpdated() {
            return updated;
        }

        public void setUpdated(String updated) {
            this.updated = updated;
        }

        public String getStrainName() {
            return strainName;
        }

        public void setStrainName(String strainName) {
            this.strainName = strainName;
        }

        public String getUsrMobile() {
            return usrMobile;
        }

        public void setUsrMobile(String usrMobile) {
            this.usrMobile = usrMobile;
        }

        public String getUsrImage() {
            return usrImage;
        }

        public void setUsrImage(String usrImage) {
            this.usrImage = usrImage;
        }

        public String getUsrEmail() {
            return usrEmail;
        }

        public void setUsrEmail(String usrEmail) {
            this.usrEmail = usrEmail;
        }

    }

我的过滤器methode()

 private void filter() {
        //Showing a progress dialog while our app fetches the data from url
        final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
        String DATA_URL = "http......../searchPigeonList";
        //ArrayList<POJO> pigeonArray = new ArrayList<POJO>();
        ArrayList<POJO> pigeonArray = new ArrayList<POJO>();
        final String lstrain = (String)lsStrain.getSelectedItem();
        final String ldistance = (String)lsDistance.getSelectedItem();
        final String lcountry = (String)lsCountry.getSelectedItem();
        PMPigeonListAdapter pmpigeonlistadapter = new        PMPigeonListAdapter(getApplicationContext(), pigeonArray);
        gridView.setAdapter(pmpigeonlistadapter);


        //Adding adapter to gridview
       // gridView.setAdapter(pmpigeonlistadapter);
       // pmpigeonlistadapter.notifyDataSetChanged();


        // final String lstrain = lsStrain.getSelectedItem().toString();
        //final String ldistance = lsDistance.getSelectedItem().toString();
        // final String lcountry = lsCountry.getSelectedItem().toString();
        Toast.makeText(this, ""+lstrain, Toast.LENGTH_SHORT).show();


        RestAdapter radapter=new RestAdapter.Builder().setEndpoint(DATA_URL).build();
        MInterface restInt=radapter.create(MInterface.class);
        restInt.getUser(new Callback<POJO>() {
            @Override
            public void success(POJO pojo, retrofit.client.Response response) {
                pigeonArray.add(pojo);
                pmpigeonlistadapter.notifyDataSetChanged();
            }

            @Override
            public void failure(RetrofitError error) {

                String err = error.getMessage();
            }
        });
     //Log.d("Test",response);
        //Creating GridViewAdapter Object



        Log.d("TAG11",gridView.getAdapter().getClass().getName());
    }



    @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params2 = new HashMap<String, String>();
                    params2.put("country", lcountry);
                    params2.put("strain", lstrain);
                    params2.put("distance", ldistance);
                    return params2;
                }
            };

            RequestQueue requestQueue2 = Volley.newRequestQueue(this);
            gridView.setAdapter(null);
            requestQueue2.add(stringRequest2);


        }

PmPigeonlist适配器:

public class PMPigeonListAdapter extends BaseAdapter {

    //Imageloader to load images
    private ImageLoader imageLoader;

    //Context
    private Context context;
    private ArrayList<String> pigeonArray;
    // you just need to set the context and the pigenArray.
    public PMPigeonListAdapter(Context context, ArrayList<String>pigeonArray) {
        //Getting all the values
        this.context = context;
        this.pigeonArray=pigeonArray;
    }

    static class ViewHolder {
        ImageView imageView;
        TextView textView;
        LinearLayout grid_id;
    }

    @Override
    public int getCount() {
        return pigeonsArray.size();
    }

    @Override
    public Object getItem(int position) {
        return pigeonArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //Creating a linear layout
        View view = convertView;
        final ViewHolder gridViewImageHolder;
//             check to see if we have a view
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = inflater.inflate(R.layout.pigeon_grid_item, parent, false);
            gridViewImageHolder = new ViewHolder();
            gridViewImageHolder.imageView = (ImageView) view.findViewById(R.id.imageView1);
            gridViewImageHolder.textView = (TextView) view.findViewById(R.id.text1);
            gridViewImageHolder.grid_id = (LinearLayout) view.findViewById(R.id.grid_id);

            view.setTag(gridViewImageHolder);
        } else {
            gridViewImageHolder = (ViewHolder) view.getTag();
        }


        NetworkImageView networkImageView = new NetworkImageView(context);

        imageLoader = PMCustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        networkImageView = (NetworkImageView) gridViewImageHolder.imageView;
        networkImageView.setAdjustViewBounds(true);
        networkImageView.setImageUrl(images.get(position), imageLoader);
        //scaleImage(networkImageView);

        //TextView textView = new TextView(context);
        //Here you will display pigeonDetails based on the POJO object
        gridViewImageHolder.textView.setText("€" + pigeonArray.get(position).getPigeonDetails());
        gridViewImageHolder.grid_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PMSearchResultDetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("pname",pigeonArray.get(position).getName());
                intent.putExtra("pprice",pigeonArray.get(position).getPrice());
         //etc for the other value u want to pass. (use the POJO getter)
                //intent.putExtra("pdetails",pdetails.get(position));
                //intent.putExtra("pmobile",pmobile.get(position));
                //intent.putExtra("pemail",pemail.get(position));
                //intent.putExtra("pimage",images.get(position));
                context.startActivity(intent);
            }
        });

        return view;
    }

    private void scaleImage(ImageView view)
    {
        Drawable drawing = view.getDrawable();
        if (drawing == null) {
            return;
        }
        Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int bounding_x = ((View)view.getParent()).getWidth();//EXPECTED WIDTH
        int bounding_y = ((View)view.getParent()).getHeight();//EXPECTED HEIGHT

        float xScale = ((float) bounding_x) / width;
        float yScale = ((float) bounding_y) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(xScale, yScale);

        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        width = scaledBitmap.getWidth();
        height = scaledBitmap.getHeight();
        BitmapDrawable result = new BitmapDrawable(context.getResources(), scaledBitmap);

        view.setImageDrawable(result);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
}

1 个答案:

答案 0 :(得分:0)

我的第一个猜测是getpigeon_detail是一种方法。因此,您可以致电:
pojo.getpigeon_detail ();

还要确保模型类中存在该方法,并且该方法是公共的。

你应该打电话:
pojo.getPigeonDetails();

////编辑////

首先,你的适配器(绑定到gridview的那个)构造函数应该具有ContextArray hodling POJO对象作为参数。

在您的类中检索您的数据,具有将存储POJO对象的全局变量类型Array。我们称之为pigeonArray。

初始化你的适配器和gridview并绑定它们:

PMPigeonListAdapter pmpigeonlistadapter = newPMPigeonListAdapter(getApplicationContext(), pigeonArray);
gridView.setAdapter(pmpigeonlistadapter);  

然后在:

public void success(POJO pojo, retrofit.client.Response response) {
    pigeonArray.add(pojo);
    pmpigeonlistadapter.notifyDataSetChanged();
}

然后只需从你的pojo对象中读取你感兴趣的值,并显示它们!