Android ArrayAdapter and JSONArray

后端 未结 4 2014
忘了有多久
忘了有多久 2021-01-07 10:48

I am new to Android Development.

I purely like to work with JSON Objects and Arrays for my simple application considering the lightness of the JSON Carrier compared

4条回答
  •  佛祖请我去吃肉
    2021-01-07 11:00

    This is the listview adapter class.

    public class Adapter extends BaseAdapter {
        Context context = null;
        ArrayList offers = null;
    
    
        public Adapter(Context context, ArrayList offer) {
            this.context = context;
            this.offers = offer;
    
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return offers.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return offers.get(position).getTitle();
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
    
            View v;
            final TextView tvpoints;
            final TextView tv,tv_quantity;
            if (convertView == null) {
                LayoutInflater li = ((Activity) context).getLayoutInflater();
                v = li.inflate(R.layout.design_userlist, null);
    
            } else {
                v = convertView;
            }
            tvpoints = (TextView) v.findViewById(R.id.tvpointlist);
            tv_quantity= (TextView) v.findViewById(R.id.tv_quantity);
            tv = (TextView) v.findViewById(R.id.tvdatalist);
            ((Activity) context).runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    tv.setText(offers.get(position).getTitle().toUpperCase());
                    tv_quantity.setText(offers.get(position).getQuatity().toUpperCase());
                    tvpoints.setText(offers.get(position).getPoint() + "");
                }
            });
    
            return v;
        }
    
    }
    

    Object class

    public class OffersAvailable {
        String title, point, quatity, description,nid;
    
        public String getNid() {
            return nid;
        }
    
        public void setNid(String nid) {
            this.nid = nid;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getPoint() {
            return point;
        }
    
        public void setPoint(String point) {
            this.point = point;
        }
    
        public String getQuatity() {
            return quatity;
        }
    
        public void setQuatity(String quatity) {
            this.quatity = quatity;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
    }
    

    Use the json in the main class and store it inthe Arraylist of type OffersAvailable.

    and pass it to the listviews adapter. if you are getting the response from the internet use asynchttpclient method.And parse the json.

提交回复
热议问题