【问题标题】:Custom array push data with loop带循环的自定义数组推送数据
【发布时间】:2016-08-03 07:57:40
【问题描述】:

我有一个数据集 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;
    }
}

还有一个自定义适配器 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;
    }
}

我正在尝试使用代码从 web 服务推送数据

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

}

但它返回 null 异常,因为

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")
        };

这工作正常。请有人帮忙

【问题讨论】:

  • 具体哪里出错了?
  • 尝试使用arraylist,并确保object.getString("name")存在。

标签: java android


【解决方案1】:

尝试使用 ArrayList 而不是 User[] 数组。

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

将用户添加到此列表。

就像:

list.add(new User(xxx, yyy));

【讨论】:

    【解决方案2】:

    恕我直言,您的代码中有几个问题。

    1 - Json 文件来源

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

    构造函数请求一个表示 json 字符串的字符串。显然“锻炼”不是有效的 json。所以你永远找不到“名称”字段..所以问题就在这里!!!


    改进

    2 - 使用纯数组结构

    也许更好地使用 ArrayList 是下一个操作数据的更好选择。 (例如排序!)

    3 - object.getString(String abc)

    我建议你使用

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

    这样你可以放一个默认的返回值,避免其他问题。阅读这个 SO 线程 JSON: the difference between getString() and optString()

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 2023-03-10
      • 1970-01-01
      • 2017-06-22
      • 2016-05-18
      • 1970-01-01
      • 2018-10-15
      • 2017-08-23
      • 2017-06-08
      相关资源
      最近更新 更多