Android服务器响应值不适合listview

时间:2016-05-19 12:10:47

标签: android listview

我有服务器响应json字符串,如

[{  "fullname": "preeti",
    "username": "sshh",
    "country": "India"
}, {
    "fullname": "saloni",
    "username": "kkrp",
    "country": "India"
}]

我想将此值显示在自定义列表视图中。 所以,我有

User.java

public class User {

    private String fullname;
    private String username;

//getter and setters
}

我有viewadapter类

public class CustomListViewAdapter extends ArrayAdapter<User> {
     Context context;
     public CustomListViewAdapter(Context context, int resourceId,
            List<User> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {

        TextView txtName;
        TextView txtUsername;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        User user = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.name);
            holder.txtUsername = (TextView) convertView.findViewById(R.id.username);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtName.setText(user.getFullname());
        holder.txtUsername.setText(user.getUsername());


        return convertView;
    }

现在,在MainActivity中,我将该数据显示到listview

List<User> userDetails;
listView = (ListView)findViewById(R.id.list);   
// here serverResponse is the json string response value
User[] ser = new Gson().fromJson(serverResponse, User[].class);
userDetails = new ArrayList<User>();
for (int i = 0; i < ser.length; i++) {
  User user1 = new User(ser[i].getFullname(), ser[i].getUsername());
  userDetails.add(user1);
 }
 CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity(), R.layout.list_item, userDetails);
 listView.setAdapter(adapter);

但在这里,我只得到一个值而不是所有值。

Name:--preeti
Username:-sshh

不是所有的值。为什么????

6 个答案:

答案 0 :(得分:1)

更改此

  

for(int i = 0; i&lt; ser.length; i ++){     用户user1 =新用户(ser [i] .getFullname(),ser [i] .getUsername());     userDetails.add(用户1);    }

答案 1 :(得分:0)

 try this to get the list 


    ArrayList<User> userDetails;
    listView = (ListView)findViewById(R.id.list); 

    Type type = new TypeToken<ArrayList<User>>() {}.getType();

    // here serverResponse is the json string response value
    userDetails = new Gson().fromJson(serverResponse, type);

    for (int i = 0; i < userDetails.size(); i++) {
      User user1 = new User(userDetails.get(i).getFullname(), userDetails.get(i).getUsername());
      userDetails.add(user1);
     }

答案 2 :(得分:0)

你必须改变这个

for (int i = 0; i < ser.length; i++) 
{ User user1 = new User(ser[i].getFullname(), ser[i].getUsername()); userDetails.add(user1); }

答案 3 :(得分:0)

在适配器中创建List<User>并使用它...

public class CustomListViewAdapter extends ArrayAdapter<User> {
 Context context;
 List<User> items;
 public CustomListViewAdapter(Context context, int resourceId,List<User> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items= items;
...

让您的用户离开您的列表...

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    User user = items.get(position);
...

答案 4 :(得分:0)

尝试:

Type listType = new TypeToken<List<User>>(){}.getType();
List<User> users = new Gson().fromJson(serverResponse, listType);

答案 5 :(得分:0)

我使用了Recycler视图而不是ListView。

请检查我的实施。

我创建了两个Model Classes来保存服务器数据:

public class User {

    @SerializedName("fullname")
    private String FullName;

    @SerializedName("username")
    private String UserName;

    @SerializedName("country")
    private String Country;

    public String getFullName() {
        return FullName;
    }

    public void setFullName(String fullName) {
        FullName = fullName;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getCountry() {
        return Country;
    }

    public void setCountry(String country) {
        Country = country;
    }

}

This is the parent Model holds the user list.

class UserParent{
        @SerializedName("user_list_key")// Key From server - You are not added in the JSON
                List<User>UserList;
        public List<User> getUserList() {
            return UserList;
        }

        public void setUserList(List<User> userList) {
            UserList = userList;
        }
    }

然后我使用GSON Parser解析数据。

// Added in the Oncreate
RecyclerView recycler = (RecyclerView) findViewById(R.id.user_recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));

Gson gson = new GsonBuilder().create();
handleUserResponse(gson.fromJson(jsonResp, User.class));// jsonResp - Your Response string from server

handleUserResponse(UserParent parentObject){
    if(parentObject== null)return;

    // here serverResponse is the json string response value
    RecyclerAdapter adapter = new RecyclerAdapter(getActivity(), parentObject.getUserList());
    recycler.setAdapter(adapter);
}

适配器类

class RecyclerAdapater extends RecyclerView.Adapter<RecyclerHolder>{

        private LayoutInflater mInflater;
        private List<User> mUserList;

        public RecyclerAdapater(Context context, List<User> userList) {
            this.mUserList = userList;
            this.mInflater = LayoutInflater.from(context);
        }


        @Override
        public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = mInflater.inflate(R.layout.user_single_item_layout, parent, false);// Layout need to create with two text views
            RecyclerHolder userHolder = new RecyclerHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerHolder holder, int position) {
            holder.txtName.setText(mUserList.get(position).getFullName());
            holder.txtUsername.setText(mUserList.get(position).getUserName());
        }

        @Override
        public int getItemCount() {
            return mUserList.size();
        }

        class RecyclerHolder extends RecyclerView.ViewHolder{

            TextView txtName;
            TextView txtUsername;
            public RecyclerHolder(View itemView) {
                super(itemView);

                this.txtName = itemView.findViewById(R.id.tv_txt_name); // Form Layout
                this.txtUsername = itemView.findViewById(R.id.tv_user_name) // From Layout

            }
        }

    }

Recycler View Layout

 <android.support.v7.widget.RecyclerView
        android:id="@+id/user_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        >
    </android.support.v7.widget.RecyclerView>

build.gradle下的Gradle Dependency

编译'com.android.support:recyclerview-v7:23.3.0'//版本可能会根据您的工作空间而改变。