0

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

2 Answers2

1

Try to use ArrayList instead of User[] array.

ArrayList<User> list = new ArrayList<User>();

To add a user to this list.

Just like:

list.add(new User(xxx, yyy));
xxx
  • 3,315
  • 5
  • 21
  • 40
0

IMHO there are a couple of problem in your code.

1 - Json file source

JSONArray jArray = object_exc.getJSONArray("exercise");

The constructor request a string that represent a json string. Obviously "exercise" is not a valid json. So you will never find "name" field..so the problem is here!!!


Improvements

2 - Using pure array structure

Maybe is better use an ArrayList is a better option for next manipulation data. (for example sorting!)

3 - object.getString(String abc)

I suggest you to use

object.optString("name", "no_name")

in this way you can put a default return value and avoid other problems. read this SO thread JSON: the difference between getString() and optString()

Community
  • 1
  • 1
appersiano
  • 2,670
  • 22
  • 42