为什么listview显示最后一项两次?

时间:2017-07-06 19:26:51

标签: android listview android-studio android-volley android-arrayadapter

我的listview两次显示最后一项。

我想用volley和MySQL向数据库中的成员显示,从android向API发送请求,该请求在 PHP 中编程, API 返回
JSON数据。

这是我的代码:

String email, member_id, member_type;
ListView community_member_list;
Context context = this;

MemberAdapter memberAdapter;
List<Model> modelList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_community);

    SharedPreferences SP = getSharedPreferences("MemberSession", MODE_PRIVATE);
    Boolean member_logged_in = SP.getBoolean("member_logged_in", false);
    email = SP.getString("email", null);
    member_id = SP.getString("member_id", null);
    member_type = SP.getString("member_type", null);

    if (!member_logged_in)
    {
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);
        finish();

    }else{

        community_member_list = (ListView) findViewById(R.id.community_members_list);

        memberAdapter = new MemberAdapter(getApplicationContext(), R.layout.community_list_item, modelList);
        community_member_list.setAdapter(memberAdapter);

        get_community_members();
    }

}

public void get_community_members()
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, new Login().site_url+"community",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {

                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("members");
                        Model model = null;

                        for(int i=0; i<jsonArray.length(); i++) {

                            JSONObject finalObject = jsonArray.getJSONObject(i);

                            model = new Model();

                            model.setMember_id(finalObject.getInt("physician_id"));
                            model.setProfile_photo(finalObject.getString("profile_photo"));
                            model.setFirst_name(finalObject.getString("first_name"));
                            model.setLast_name(finalObject.getString("last_name"));
                            model.setDesignation(finalObject.getString("designation"));
                            model.setOrganization(finalObject.getString("organization"));

                            modelList.add(model);
                        }

                        memberAdapter.add(model);
                        memberAdapter.setNotifyOnChange(true);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(context, "Error "+error, Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();

            params.put("member_id", member_id);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private class MemberAdapter extends ArrayAdapter {

    private List<Model> memberList;
    private int resource;
    private LayoutInflater inflater;

    MemberAdapter(Context context, int resource, List<Model> objects) {
        super(context, resource, objects);
        memberList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {

        if(convertView == null)
        {
            convertView = inflater.inflate(resource, null);
        }

        final ImageView imageView;
        TextView textView, textView2;

        imageView = (ImageView)convertView.findViewById(R.id.imageView);
        textView = (TextView)convertView.findViewById(R.id.textView);
        textView2 = (TextView) convertView.findViewById(R.id.textView2);

        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage("http://www.plexusd.com/uploads/specialist/" + memberList.get(position).getProfile_photo(), imageView, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                imageView.setImageResource(R.mipmap.avatar);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                imageView.setImageResource(R.mipmap.avatar);
            }
        });

        textView.setText("Dr. "+memberList.get(position).getFirst_name()+" "+memberList.get(position).getLast_name());
        textView2.setText(""+memberList.get(position).getDesignation()+",\n"+memberList.get(position).getOrganization());

        return convertView;
    }
}

以下是手机列表的图片。

here is the picture of list from mobile

2 个答案:

答案 0 :(得分:0)

在for循环后删除onResponse中的memberAdapter.add(model);。模型对象保存最后一个对象,然后再次在适配器中添加该对象,使最后一个对象显示2次。

try {

                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("members");
                        Model model = null;

                        for(int i=0; i<jsonArray.length(); i++) {

                            JSONObject finalObject = jsonArray.getJSONObject(i);

                            model = new Model();

                            model.setMember_id(finalObject.getInt("physician_id"));
                            model.setProfile_photo(finalObject.getString("profile_photo"));
                            model.setFirst_name(finalObject.getString("first_name"));
                            model.setLast_name(finalObject.getString("last_name"));
                            model.setDesignation(finalObject.getString("designation"));
                            model.setOrganization(finalObject.getString("organization"));

                            modelList.add(model);
                        }

                        memberAdapter.add(model); // remove this.
                        memberAdapter.setNotifyOnChange(true);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

答案 1 :(得分:0)

你添加了两次模型,其中一个在循环中,而不是循环后的