滚动时会重复Android ListView内容

时间:2014-03-22 11:28:35

标签: android listview android-listview

这是我的代码。请帮我解决这个问题,我们将不胜感激。我也想知道如何用gridview解决同样的问题。请尽早告诉我详细信息。提前谢谢。

public class ItemListAdapter extends BaseAdapter {
    public Context context;
    public final ArrayList<HashMap<String, String>> product;

    public ItemListAdapter(Context context, ArrayList<HashMap<String, String>> cseList) {
        this.context = context;
        this.product = cseList;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View listView;

        if (convertView == null) {

            listView = new View(context);

            // get layout from mobile.xml
            listView = inflater.inflate(R.layout.item_list_single, null);

            // set value into textview
            TextView textView1 = (TextView) listView
                    .findViewById(R.id.product_title);
            textView1.setText(product.get(position).get("post_title"));

            TextView textView2 = (TextView) listView
                    .findViewById(R.id.regular_price);
            textView2.setText(product.get(position).get("_regular_price"));
            String a = textView2.getText().toString();

            TextView textView3 = (TextView) listView
                    .findViewById(R.id.sale_price);
            textView3.setText(product.get(position).get("_sale_price"));
            String b = textView3.getText().toString();

            TextView textView4 = (TextView) listView
                    .findViewById(R.id.discount);
            textView4.setText(a);


            //textView4.setText(i);
            TextView textView5 = (TextView) listView
                    .findViewById(R.id.regular_price_cross);

            if(a.equals(b))
            {
                textView4.setVisibility(View.INVISIBLE);
                textView5.setVisibility(View.INVISIBLE);
            }
            else if (b.equals(""))
            {
                textView2.setVisibility(View.INVISIBLE);
                textView5.setVisibility(View.INVISIBLE);
                textView4.setVisibility(View.INVISIBLE);
                textView3.setText(textView2.getText().toString());
            }


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

            String sample = String.valueOf(product.get(position).get("guid"));
            if (sample!="")
            {
            String address = sample.replaceAll("\\/", "/");
            new DownloadImageTask(imageView).execute(address);
            }
            else
            {
                imageView.setImageResource(R.drawable.logo);
            }

            //String mobile = categories[position];


        } else {
            listView = (View) convertView;
        }

        return listView;
    }

    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);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

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

    @Override
    public int getCount() {
    int a = product.size();
        return a;
    }

    @Override
    public Object getItem(int position) {
        return product.get(position);
    }

    @Override
    public long getItemId(int position) {
        return product.indexOf(getItem(position));
    }

}

0 个答案:

没有答案