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
  • where specifically is the error ? Commented Aug 3, 2016 at 8:03
  • 1
    Try to use arraylist, and make sure object.getString("name") exist. Commented Aug 3, 2016 at 8:05

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.