重复Json android图像和文本

时间:2013-09-05 14:58:40

标签: android json

Json android,我有一个加载图片和文字的json,作品很完美,但图片与文字不匹配,图片重复骨骼和文字,我错了,我是新手

public class CustomAdapter extends BaseAdapter {

    LayoutInflater inflator;
    ArrayList<JSONFields> result;

    public CustomAdapter(Context context, ArrayList<JSONFields> result) {
        // TODO Auto-generated constructor stub

        inflator = LayoutInflater.from(context);
        this.result = result;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        Holder holder;
        DownloadImageTask task = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = inflator.inflate(R.layout.helper, null);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            holder.textview1 = (TextView) convertView
                    .findViewById(R.id.textView1);
            holder.descrip = (TextView) convertView
                    .findViewById(R.id.descripcion);


            task = new DownloadImageTask(holder.image);
            convertView.setTag(holder);

        } else {
            holder = (Holder) convertView.getTag();
        }

        JSONFields fields = new JSONFields();

        fields = result.get(position);
        if (task != null)
            task.execute(fields.getUrl());


        holder.textview1.setText(fields.getName());
        holder.descrip.setText(fields.getDescripcion());



        return convertView;

    }

    public class Holder {
        ImageView image;
        TextView textview1;
        TextView descrip;
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;


        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
                in.close();
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}



import com.ditec.json.data.JSONFields;

public class MainActivity extends Activity {

    public static final String URL = "http://169.254.65.190:5000/fbsharing/7BkWVF6r";
    ListView listview;

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

        listview = (ListView) findViewById(R.id.listView1);

        GetJSON json = new GetJSON();
        json.execute(URL);

    }

    public class GetJSON extends AsyncTask<String, Void, ArrayList<JSONFields>> {

        @Override
        protected ArrayList<JSONFields> doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                String url = params[0];
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(url);
                HttpResponse response;
                response = client.execute(get);
                InputStream content = response.getEntity().getContent();
                BufferedReader reader;
                reader = new BufferedReader(new InputStreamReader(content, "iso-8859-1"), 8);
                String line;
                String json = "";
                while ((line = reader.readLine()) != null) {
                    json += line;
                }

                JSONArray array = new JSONArray(json);
                ArrayList<JSONFields> alData = new ArrayList<JSONFields>();
                for (int i = 0; i < array.length(); i++) {

                    JSONFields data = new JSONFields();
                    JSONObject obj = array.getJSONObject(i);

                    data.setUrl(obj.getString("url"));
                    data.setName(obj.getString("name"));
                    data.setDescription(obj.getString("descripcion"));
                    alData.add(data);
                }
                System.out.println("Data returned sucessfully");
                return alData;

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<JSONFields> result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            CustomAdapter adapter = new CustomAdapter(MainActivity.this, result);

            listview.setAdapter(adapter);

        }
    }

}



public class JSONFields {

    private String name;
    private String url;
    private String descripcion;

    public String getName() {
        return name;
    }
    public String getUrl() {
        return url;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public void setDescription(String descripcion) {
        this.descripcion = descripcion;
    }

}

2 个答案:

答案 0 :(得分:1)

使用Volley库获取JSON,并轻松实现获取图像。它非常有效。

示例:http://www.kpbird.com/2013/05/volley-easy-fast-networking-for-android.html

ImageDownload是NetworkImageView。 :http://www.jayway.com/2013/08/20/volley-networking-for-android/

它只有几行代码。我每次都用这个。

答案 1 :(得分:0)

看看这篇帖子,它解决了你的图像与你的文本不匹配的确切问题(由于视图在图像加载时被重复使用/回收)。 Listview with images, but when scroll the List, the images apear in wrong rows 一个简单的解决方法是在imageview上使用setTag和getTag,设置一个字符串(例如url),用于标识应该在imageview中的图像,然后检查标签是否仍然相同加载图像,捕捉图像视图(convertview)已被回收的情况。