列表视图图像放错位置,当我滚动时也会错位,只有图像错位

时间:2014-11-17 11:24:05

标签: android

这是我的代码:

公共类NewProductListofitems扩展了Activity {

ArrayList<Products> actorsList;

ListAdapter adapter;
ListView listview;
int count;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_productlistofitems);
    textview=(TextView)findViewById(R.id.producttitle);
    actorsList = new ArrayList<Products>();
    new JSONAsyncTask().execute("http://emart.icore.net.i/get_productlistofitems.php"+"?"+"c"+"="+categry_id);

    listview = (ListView)findViewById(R.id.list);
    adapter = new ListAdapter(actorsList,getApplicationContext());

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Products item=(Products) adapter.getItem(position);
            Intent in = new Intent(getApplicationContext(),
                    SingleProduct.class);
            // sending pid to next activity
            in.putExtra("pd", item.getProductid());
            in.putExtra("d", item.getCategoryid());
            in.putExtra("im", item.getThumbnailUrl());
            in.putExtra("dp", item.getTitle());
            in.putExtra("dsp", item.getRate());
            in.putExtra("ds", item.getStock());

            System.out.println(in);
            //System.out.println(in);
            // starting new activity and expecting some response back
            startActivity(in);              
        }
    });
}


class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(NewProductListofitems.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting to server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) 
            {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("products");

                for (int i = 0; i < jarray.length(); i++) 
                {
                    JSONObject object = jarray.getJSONObject(i);

                    count=jarray.length();
                    Products actor = new Products();
                    actor.setProductid(object.getString("product_id"));
                    actor.setCategoryid(object.getString("category_id"));
                    actor.setTitle(object.getString("product_name"));
                    actor.setThumbnailUrl(object.getString("small_image"));
                    actor.setRate(object.getString("sale_price"));
                    actor.setStock(object.getString("stock"));

                    actorsList.add(actor);
                    System.out.println(actorsList);
                    System.out.println("count value is --->"+count);

                }

                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false)
        {
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        }
        textview.setText(+count+" results for "+"'"+cat_title+"'");

    }
}

这是BaseAdapter类:

公共类ListAdapter扩展了BaseAdapter {

private List<Products> imageBitmapList;
private Context context;

public ListAdapter(List<Products> imageBitmapList, Context context) {
    this.imageBitmapList = imageBitmapList;
    this.context = context;
}

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

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return imageBitmapList.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.productlistofitems, null);
    }

    ImageView imageView = (ImageView) v.findViewById(R.id.list_image);
    imageView.setImageResource(R.drawable.no_image);
    new DownloadImageTask(imageView).execute(imageBitmapList.get(position).getThumbnailUrl());
    TextView title=(TextView) v.findViewById(R.id.title);
    title.setText(imageBitmapList.get(position).getTitle());
    TextView sp=(TextView) v.findViewById(R.id.saleprice);
    sp.setText(imageBitmapList.get(position).getRate());
    return v;
}
 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 mIcon = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon;
        }

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

 public View getDropDownView (int position, View convertView, ViewGroup parent)
 {
     View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.productlistofitems, null);
        }

        ImageView imageView = (ImageView) v.findViewById(R.id.list_image);
        imageView.setImageResource(R.drawable.no_image);
        new DownloadImageTask(imageView).execute(imageBitmapList.get(position).getThumbnailUrl());
        TextView title=(TextView) v.findViewById(R.id.title);
        title.setText(imageBitmapList.get(position).getTitle());
        TextView sp=(TextView) v.findViewById(R.id.saleprice);
        sp.setText(imageBitmapList.get(position).getRate());
        return v;

 }

}

我在这方面很努力。但是没有解决方案。任何人都可以帮助我。

0 个答案:

没有答案