【发布时间】: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")存在。