I have a custom class to data set User.java
public class User {
public int icon;
public String title;
public User(){
super();
}
public User(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
Also have a custom adapter UserAdapter.java
public class UserAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
User data[] = null;
public UserAdapter(Context context, int layoutResourceId, User[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.list_image);
holder.txtTitle = (TextView)row.findViewById(R.id.title);
row.setTag(holder);
}
else
{
holder = (UserHolder)row.getTag();
}
User User = data[position];
holder.txtTitle.setText(User.title);
holder.imgIcon.setImageResource(User.icon);
return row;
}
static class UserHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
I am trying to push data from webservice with the code
public User user_data[] = new User[500];
try {
JSONObject object_exc = response;
JSONArray jArray = object_exc.getJSONArray("exercise");
for (int i = 0; i < jArray.length(); i++) {
JSONObject object = jArray.getJSONObject(i);
user_data[i] = new User(R.drawable.nopic, object.getString("name"));
}
}catch (Exception e){
}
But it is returning null exception where as
User user_data[] = new User[]
{
new User(R.drawable.weather_cloudy, "Cloudy"),
new User(R.drawable.weather_showers, "Showers"),
new User(R.drawable.weather_snow, "Snow"),
new User(R.drawable.weather_storm, "Storm"),
new User(R.drawable.weather_sunny, "Sunny")
};
this is working fine. Please some one help