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 arr
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.
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
String[] Description = new String[responseJson.length()];
String[] ImageURL = new String[responseJson.length()];
JSONArray jArrayCategoryWise = new JSONArray();
for (int i = 0; i < responseJson.length(); i++) {
JSONObject object = 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 response
CustomGrid adapter = new CustomGrid(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
public class CustomGrid extends BaseAdapter {
private Context context;
private Context mContext;
// private final String[] Description;
// private final String[] ImageURL;
private JSONArray json;
private int size = 0;
public CustomGrid(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;
}
@Override
public int getCount() {
return size;
}
@Override
public Object getItem(int position) {
return this.json.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
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;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView 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.
First, change your adapter to expends the
extends ArrayAdapter<String>
Change the constructor:
public CustomGrid(Context c) {
super(c, R.layout.fragment_pizza);
}
Now Change getView()
method:
// Get the item
String item = 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 item
ModelClass item = getItem(position);
Now in your Activity class, initilize the ModelClass, set it's variables and add to the adapter using add or addAll methods.