Android GridView重绘问题

时间:2015-05-13 15:17:56

标签: java android gridview

我创建了一个应用程序,允许用户拍照,上传到网络服务器。然后,此图像在活动

中的网格视图上可见

我虽然一切正常,但是我拍的越多,我注意到的问题越多

当用户开始滚动时,网格视图中的图像似乎正在重绘,有效地再次向用户显示相同的图像(并非总是以相同的顺序)

我已经阅读过其他帖子,人们在类似的东西上挣扎,但修复似乎对我不起作用

GridView Redraw Problems

提前感谢所有帮助

这是我的gridview活动

select u.idUser, u.username, 
STUFF((SELECT distinct ',' + convert(varchar,g.groupname)
        from groups g
        where g.IdUser = u.iduser 
        FOR XML PATH(''), TYPE   
        ).value('.', 'NVARCHAR(MAX)')   
        ,1,1,'') as rutas,
STUFF((SELECT distinct ',' + convert(varchar,r.rolename)
        from Roles r
        where r.IdUser = u.iduser 
        FOR XML PATH(''), TYPE   
        ).value('.', 'NVARCHAR(MAX)')   
        ,1,1,'') as rutas,
from users u 
where u.idUser = 4882

这是我的Image适配器(用毕加索定制)

package com.example.alex.jsonparsegridview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {

    GridView gridView;

    static final String[] MOBILE_OS = new String[]{
            "Android", "iOS", "Windows", "Blackberry"};

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new JSONParse().execute();

        TextView tv = (TextView)findViewById(R.id.footerText);


        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent(MainActivity.this, CameraActivity.class);
                myIntent.putExtra("dir", "BS"); //Optional parameters
                MainActivity.this.startActivity(myIntent);
            }
        });


    }


    private class JSONParse extends AsyncTask<String, String, JSONArray> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONArray doInBackground(String... args) {

            JSONParser jParser = new JSONParser();

            // Getting JSON from URL

            String url = "http://www.500kgiveaway.co.uk/getimagesmob.php";
            JSONArray json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute(JSONArray json) {
            pDialog.dismiss();
                // Getting JSON Array from URL

                JSONArray android = json;


                //copied from onCreate
                gridView = (GridView) findViewById(R.id.gridView1);

                gridView.setAdapter(new ImageAdapter(MainActivity.this, android));

                gridView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v,
                                            int position, long id) {
                        Toast.makeText(
                                getApplicationContext(),
                                ((TextView) v.findViewById(R.id.grid_item_label))
                                        .getText(), Toast.LENGTH_SHORT).show();
                    }
                });

                //End Copy





        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在您的getView()函数中。 GridView通过将视图传递到getView()中的convertView来重复使用视图。但是,这些视图不一定已经加载了正确的图像,它们是先前用于在不同位置显示网格中的不同单元格的视图。因此,如果convertView不为空,则可以避免不必为视图充气,但仍需将图像加载到视图中。

重新安排您的代码:

public View getView(int position, View convertView, ViewGroup parent) {

    View gridView = null;

    try {
        if (convertView == null) {
             LayoutInflater inflater = (LayoutInflater) context
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             gridView = new View(context);

             // get layout from mobile.xml
             gridView = inflater.inflate(R.layout.mobile, null);              
        }
        else
        {
             gridView = convertView;
        }

        JSONObject c = android.getJSONObject(position);

        // Storing  JSON item in a Variable
        String path = c.getString(TAG_PATH);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(path.toString());

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        Picasso.with(context).load("http://www.500kgiveaway.co.uk/" + path).resize(400,400).into(imageView);

        // Adding value HashMap key => value
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TAG_PATH, path);
        // TODO: add some checks so you don't add it if it's already in the list? 
        oslist.add(map);

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

    return gridView;
}

我不知道您的HashMap程序逻辑是什么,但您可能只想在第一次加载图像时将其添加到oslist。请注意,您无法使用(convertView == null)测试,因为convertView可能已用于完全不同的图像。