2

我有经典的 ListFragment 和 ListView (没有布局膨胀)。我正在通过 JSON 将“用户数据”从服务器下载到 Users.class 中的简单列表。我想用下载的数据填充自定义适配器,但它不起作用......下载很好,我的 Users.class 充满了用户,但是当我从 AsyncTaks 的 onPostExecute() 方法传递它来创建适配器并查看它时,getView() 方法是从来没有打电话。我已经实现了 getCount() 和 getId() 方法。如果我手动调用 getView(),convertView 对象在膨胀的布局和数据方面似乎很好......我在做什么错?谢谢你的帮助。

代码如下:Users.class

public class Users {

private List<User> listOfUsers;

public Users() {
    listOfUsers = new ArrayList<User>();
}

public void putUser(User user) {
    this.listOfUsers.add(user);
}

public List<User> getUsers() {
    return this.listOfUsers;
}

public void setUsers(List<User> users) {
    this.listOfUsers = users;
}

public int getUsersCount() {
    return this.listOfUsers.size();
}

public long getUserId(int pos) {
    return this.listOfUsers.get(pos).getId();
}

}

AsyncTask 的代码如下:

public class GetUsers extends AsyncTask<Void, Long, Users> {

interface GetUsersIFace {
    void setAdapter(Users users);
}

private static String TAG = "GetUsers.class";

private Context context;
private GetUsersIFace fragment;

public GetUsers(Context context, GetUsersIFace fragment) {
    super();
    this.context = context;
    try {
        this.fragment = fragment;
    } catch (ClassCastException e) {
        Log.d(TAG, e.getMessage());
    }
}

@Override
protected Users doInBackground(Void... unused) {
    try {
        if (isOnline()) {
            InputStream input;
            if ((input = getInputStream()) != null) {
                return getJSON(input);
            }
        }
    } catch (IOException e) {
        Log.d(TAG, "Error while reading from server.");
        Log.d(TAG, e.getMessage());
    } catch (NullPointerException e) {
        Log.d(TAG, "Server not responding.");
        Log.d(TAG, e.getMessage());
    } catch (Exception e) {
        Log.d(TAG, "Uknown issue.");
        Log.d(TAG, e.getMessage());

    }
    return null;
}

@Override
protected void onPostExecute(Users users) {
    fragment.setAdapter(users);

}

public boolean isOnline() throws NullPointerException {
    ConnectivityManager mConnManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = mConnManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        return false;
    }
}

private InputStream getInputStream() throws IOException {
    URL url = new URL("http://192.168.0.20/test.json");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream input = new BufferedInputStream(con.getInputStream());
    return input;
}

private Users getJSON(InputStream input) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        InputStreamReader reader = new InputStreamReader(input);
        Users users = mapper.readValue(reader, Users.class);
        return users;
    } catch (JsonParseException e) {
        Log.d(TAG, e.getMessage());
    } catch (JsonMappingException e) {
        Log.d(TAG, e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}

}

ListFragment 的代码如下:

public class UsersListFragment extends ListFragment implements GetUsersIFace {

ListView mListView;
ArrayAdapter<User> mAdapter;
Activity attachedActivity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.attachedActivity = activity;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView = getListView();
    GetUsers task = new GetUsers(attachedActivity, this);
    task.execute();
}

static class UserAdapter extends ArrayAdapter<User> {

    private static class ViewHolder {
        TextView name, occupation, age;
        ImageView userimage;
    }

    LayoutInflater inflater;
    Users users;

    public UserAdapter(Context context, Users users) {
        super(context, R.layout.userslist_item);
        this.users = users;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            ViewHolder holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.userslist_item, parent,
                    false);
            holder.name = (TextView) convertView
                    .findViewById(R.id.username);
            holder.occupation = (TextView) convertView
                    .findViewById(R.id.useroccupation);
            holder.age = (TextView) convertView.findViewById(R.id.userage);
            holder.userimage = (ImageView) convertView
                    .findViewById(R.id.userfoto);
            convertView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        User user = users.getUsers().get(position);

        holder.name.setText(user.getName().toString());
        holder.occupation.setText(user.getOccupation());
        holder.age.setText(String.valueOf(user.getAge()));
        return convertView;
    }

    @Override
    public int getCount() {
        return users.getUsersCount();
    }

    @Override
    public long getItemId(int pos) {
        return users.getUserId(pos);
    }

}

public void setAdapter(Users users) {
    mAdapter = new UserAdapter(attachedActivity, users);
    setListAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
}

}

User.class 的代码只是 getter 和 setter。

4

0 回答 0