Skip to content Skip to sidebar Skip to footer

Dynamicaly Loading Data To Gridview

While i am working on gridview i faced following problems, any help will be appreciated, When I load data to my gridview it loads only first 3 items of the array but there are 18

Solution 1:

Once check the loop onTaskCompleted method. you are creating instance of the adapter and setting to gridview inside loop i think its wrong after observing and checking your code i found the first issue.

Second the way you are handling Adapter view inflating method for populating row is also wrong and i can say not a proper way...

So once check the below code for the adapter and onTaskCompleted method.

@OverridepublicvoidonTaskCompleted(JSONArray responseJson) {

        try {
            String[] Description = newString[responseJson.length()];
            String[] ImageURL = newString[responseJson.length()];
            JSONArray jArrayCategoryWise = newJSONArray();
            for (int i = 0; i < responseJson.length(); i++) {
                JSONObjectobject = responseJson.getJSONObject(i);
                if ("1".equalsIgnoreCase(object.getString("MainCategoryID"))) {
                    // add to another jsonarray if MainCategoryID == 1
                    jArrayCategoryWise.put(object);
                }
            }

            /******
             * You can also create class with getter setter method for the
             * Description and ImageUrl.Make the arraylist of the class after
             * parsing jsonArray.
             */// which will required on more iteration of your json array responseCustomGrid adapter = newCustomGrid(getActivity(),
                    jArrayCategoryWise);
            grid.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Once check the data is coming properly or not.or are you adding anything in between

ADAPTER

publicclassCustomGridextendsBaseAdapter {

    privateContext context;
    privateContext mContext;
    // private final String[] Description;// private final String[] ImageURL;privateJSONArray json;
    private int size = 0;

    publicCustomGrid(Context c, JSONArray json) {
        this.context = c;
        this.json = json;
        if (this.json != null)
            size = this.json.length();
        // You can also create class with getter setter method (getter setter// for Descriptions and ImageUrl). and pass arraylist of that class// this.Description = Description;// this.ImageURL = ImageURL;
    }

    @Overridepublic int getCount() {
        return size;
    }

    @OverridepublicObjectgetItem(int position) {
        returnthis.json.get(position);
    }

    @Overridepublic long getItemId(int position) {
        return position;
    }

    @OverridepublicViewgetView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = newViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.custom_trips_frag_row, parent, false);
            holder.ivImage = (ImageView) convertView
                    .findViewById(R.id.grid_image);
            holder.tvHeader = (TextView) convertView
                    .findViewById(R.id.grid_text);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JSONObject jObjectRowData = this.json.getJSONObject(position);
        holder.tvHeader.setText(jObjectRowData.getString("Description"));
        Picasso.with(this.context).load(jObjectRowData.getString("ImageURL"))
                .into(holder.ivImage);

        return convertView;
    }

    privateclassViewHolder {
        privateTextView tvHeader;
        privateImageView ivImage;
    }
}

Please check the answer.. hope i gt ur quesion correctly and you gt your answer ... * Suggestion * ONCE CHECK THE ViewHolder class pattern for the BaseAdapter.

Solution 2:

First, change your adapter to expends the

extendsArrayAdapter<String>

Change the constructor: public CustomGrid(Context c) { super(c, R.layout.fragment_pizza); } Now Change getView() method:

// Get the itemStringitem= getItem(position);

Set this item text to your view and if you require more data sets, create the model class and change extends ArrayAdapter<String> with extends ArrayAdapter<ModelClass> and

// Get the itemModelClassitem= getItem(position);

Now in your Activity class, initilize the ModelClass, set it's variables and add to the adapter using add or addAll methods.

Post a Comment for "Dynamicaly Loading Data To Gridview"