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 to XMLs.
I had challenges with ArrayAdapter to populate the ListView.
This is how I overcome and need your suggestions on it.
Extend the Adaptor class.
Then pass the JSONArray to the constructor.
Here the constructor calls super with dummy String array setting the length of the JSONArray.
Store the constructor arguments in class for further use.
public myAdaptor(Context context, int resource, JSONArray array)
{
super(context, resource, new String[array.length()]);
// Store in the local varialbles to the adapter class.
this.context = context;
this.resource = resource;
this.profiles = objects;
}
The getView() will do the job of fetching JSONObjects from JSONArray to build view.
public View getView(int position, View convertView, ViewGroup parent)
{
View view;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, parent, false);
}
else
{
view = convertView;
}
// Here
JSONObject item = (JSONObject) profiles.getJSONObject(position);
// READY WITH JSONObject from the JSONArray
// YOUR CODE TO BUILD VIEW OR ACCESS THE
}
Now Any improvements/suggestions/thoughful-question??
getViewuse data structure like ArrayList,HashMap,.. to pass parsed values inmyAdaptorclass.