当改变的GridView的排它显示在其第一个图像

问题描述 投票:1回答:2

在我的应用程序,我可以上传短短五年图片,当我上传3或5它的正常工作,但是当我刚刚上传四象(再行更改),它显示我的位置3和位置0相同的图像。

 public void showList(final JSONArray responsearray) {
    try {
        for (int i = 0; i < responsearray.length(); i++) {
            JSONObject responseObject = responsearray.getJSONObject(i);
            Log.e("COUNT" + i, String.valueOf(responseObject));
            imageOne = responseObject.getString(TAG_PHOTO_ONE);
            get = new HashMap<>();
            get.put(TAG_PHOTO_ONE, imageOne);
            myList.add(get);
        }
        adapter = new SimpleAdapter(getActivity(), myList, R.layout.row_more_images,
                new String[]{TAG_PHOTO_ONE},
                new int[]{R.id.photo1}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final View v = super.getView(position, convertView, parent);
                if (convertView == null) {
                    TextView photo1 = v.findViewById(R.id.photo1);
                    photoOne = v.findViewById(R.id.photo_one);
                    String imgPath = getString(R.string.moreimgpath);
                    if (!photo1.getText().toString().equalsIgnoreCase("null")) {
                        final_image_url = imgPath + photo1.getText().toString();
                        Log.e("FINALPATH", final_image_url);
                        ImageLoader.getInstance().displayImage(final_image_url, photoOne, options, animateFirstListener);
                    }
                }
                return v;
            }
        };
        if (gridview.getAdapter() == null) {
            gridview.setAdapter(adapter);
        }
        gridview.setOnItemClickListener(this);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}
java android gridview
2个回答
0
投票

修改代码要

 public void showList(final JSONArray responsearray) {
        try {
            for (int i = 0; i < responsearray.length(); i++) {
                JSONObject responseObject = responsearray.getJSONObject(i);
                Log.e("COUNT" + i, String.valueOf(responseObject));
                String imageOne = responseObject.getString(TAG_PHOTO_ONE);
                HashMap<> get = new HashMap<>();
                get.put(TAG_PHOTO_ONE, imageOne);
                myList.add(get);
            }
            adapter = new SimpleAdapter(getActivity(), myList, R.layout.row_more_images,
                    new String[]{TAG_PHOTO_ONE},
                    new int[]{R.id.photo1}) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    final View v = super.getView(position, convertView, parent);
                    if (convertView == null) {
                        TextView photo1 = v.findViewById(R.id.photo1);
                        ImageView photoOne = v.findViewById(R.id.photo_one);
                        String imgPath = getString(R.string.moreimgpath);
                        if (!photo1.getText().toString().equalsIgnoreCase("null")) {
                            final_image_url = imgPath + photo1.getText().toString();
                            Log.e("FINALPATH", final_image_url);
                            ImageLoader.getInstance().displayImage(final_image_url, photoOne, options, animateFirstListener);
                        }
                    }
                    return v;
                }
            };
            if (gridview.getAdapter() == null) {
                gridview.setAdapter(adapter);
            }
    else{
    notfyDataSetChanged();
    }
            gridview.setOnItemClickListener(this);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

0
投票

我做了这个问题,只需添加:-adapter.notifyDataSetChanged(); gridview.invalidateViews();如果在条件(convertView == NULL)

 public void showList(final JSONArray responsearray) {
try {
    for (int i = 0; i < responsearray.length(); i++) {
        JSONObject responseObject = responsearray.getJSONObject(i);
        Log.e("COUNT" + i, String.valueOf(responseObject));
        imageOne = responseObject.getString(TAG_PHOTO_ONE);
        get = new HashMap<>();
        get.put(TAG_PHOTO_ONE, imageOne);
        myList.add(get);
    }
    adapter = new SimpleAdapter(getActivity(), myList, R.layout.row_more_images,
            new String[]{TAG_PHOTO_ONE},
            new int[]{R.id.photo1}) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View v = super.getView(position, convertView, parent);
            if (convertView == null) {
                TextView photo1 = v.findViewById(R.id.photo1);
                photoOne = v.findViewById(R.id.photo_one);
                String imgPath = getString(R.string.moreimgpath);
                if (!photo1.getText().toString().equalsIgnoreCase("null")) {
                    final_image_url = imgPath + photo1.getText().toString();
                    Log.e("FINALPATH", final_image_url);
                    ImageLoader.getInstance().displayImage(final_image_url, photoOne, options, animateFirstListener);
                }
                    adapter.notifyDataSetChanged();
                    gridview.invalidateViews();
            }
            return v;
        }
    };
    if (gridview.getAdapter() == null) {
        gridview.setAdapter(adapter);
    }
    gridview.setOnItemClickListener(this);
} catch (JSONException e) {
    e.printStackTrace();
} catch (NullPointerException e) {
    e.printStackTrace();
}}
© www.soinside.com 2019 - 2024. All rights reserved.