当更改 GridView 的行时,它会显示第一个图像

在我的应用程序中,我只能上传五张图片,当我上传 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();

    }

}


繁星coding
浏览 88回答 2
2回答

慕虎7371278

我做了这个问题,只需添加 :-adapter.notifyDataSetChanged(); gridview.invalidateViews(); 在 if 条件下(convertView==null)&nbsp;public void showList(final JSONArray responsearray) {try {&nbsp; &nbsp; for (int i = 0; i < responsearray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject responseObject = responsearray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; Log.e("COUNT" + i, String.valueOf(responseObject));&nbsp; &nbsp; &nbsp; &nbsp; imageOne = responseObject.getString(TAG_PHOTO_ONE);&nbsp; &nbsp; &nbsp; &nbsp; get = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; get.put(TAG_PHOTO_ONE, imageOne);&nbsp; &nbsp; &nbsp; &nbsp; myList.add(get);&nbsp; &nbsp; }&nbsp; &nbsp; adapter = new SimpleAdapter(getActivity(), myList, R.layout.row_more_images,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{TAG_PHOTO_ONE},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new int[]{R.id.photo1}) {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final View v = super.getView(position, convertView, parent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView photo1 = v.findViewById(R.id.photo1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoOne = v.findViewById(R.id.photo_one);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String imgPath = getString(R.string.moreimgpath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!photo1.getText().toString().equalsIgnoreCase("null")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_image_url = imgPath + photo1.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("FINALPATH", final_image_url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageLoader.getInstance().displayImage(final_image_url, photoOne, options, animateFirstListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridview.invalidateViews();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return v;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; if (gridview.getAdapter() == null) {&nbsp; &nbsp; &nbsp; &nbsp; gridview.setAdapter(adapter);&nbsp; &nbsp; }&nbsp; &nbsp; gridview.setOnItemClickListener(this);} catch (JSONException e) {&nbsp; &nbsp; e.printStackTrace();} catch (NullPointerException e) {&nbsp; &nbsp; e.printStackTrace();}}

浮云间

将代码更改为&nbsp;public void showList(final JSONArray responsearray) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < responsearray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject responseObject = responsearray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("COUNT" + i, String.valueOf(responseObject));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String imageOne = responseObject.getString(TAG_PHOTO_ONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<> get = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get.put(TAG_PHOTO_ONE, imageOne);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.add(get);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter = new SimpleAdapter(getActivity(), myList, R.layout.row_more_images,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{TAG_PHOTO_ONE},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new int[]{R.id.photo1}) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final View v = super.getView(position, convertView, parent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (convertView == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView photo1 = v.findViewById(R.id.photo1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageView photoOne = v.findViewById(R.id.photo_one);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String imgPath = getString(R.string.moreimgpath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!photo1.getText().toString().equalsIgnoreCase("null")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_image_url = imgPath + photo1.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("FINALPATH", final_image_url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageLoader.getInstance().displayImage(final_image_url, photoOne, options, animateFirstListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return v;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gridview.getAdapter() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridview.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; notfyDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridview.setOnItemClickListener(this);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (NullPointerException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java