完全显示的列表视图图像无法正常显示

时间:2015-11-18 08:30:50

标签: android listview android-listview

我的以下代码。

public class Wishlist extends Activity {
    Button checkout;
    ListView ListCart;
    String name, cusid, ffname, llname, phone, fax, password, email;
    String[] qu, s;
    int[] g;
    int k = 0;
    String cost;
    ProgressDialog pDialog = null;
    List<CartProducts> product_list;
    Context ctx;
    Integer pos = 0, total = 0, q = 0, gtot = 0, total1 = 0, sum = 0;
    SQLiteDatabase FavData;

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

        Intent page1 = getIntent();
        cusid = page1.getStringExtra("cus_id");
        ffname = page1.getStringExtra("fname");
        llname = page1.getStringExtra("lname");
        phone = page1.getStringExtra("ph");
        fax = page1.getStringExtra("fax");
        password = page1.getStringExtra("password");
        email = page1.getStringExtra("email");
        ListCart = (ListView) findViewById(R.id.list_item);


        pDialog = new ProgressDialog(this);
        ctx = this;
        FavData = Wishlist.this.openOrCreateDatabase("SHOPPING_CARTFAV", MODE_PRIVATE, null);
        FavData.execSQL("CREATE TABLE IF NOT EXISTS fav_items(product_id varchar, name varchar, price varchar, quantity integer, model varchar, image varchar, manufacturer varchar )");
        ArrayList<CartProducts> myList = new ArrayList<CartProducts>();
        Cursor crsr = FavData.rawQuery("SELECT * FROM fav_items", null);
        final String[] productID = new String[crsr.getCount()];
        final String[] ProductName = new String[crsr.getCount()];
        final String[] ProductPrice = new String[crsr.getCount()];
        final String[] ProductQuantity = new String[crsr.getCount()];
        final String[] ProductModel = new String[crsr.getCount()];
        final String[] ProductImage = new String[crsr.getCount()];
        final String[] ProductManufacturer = new String[crsr.getCount()];
        int j = 0;

        while (crsr.moveToNext()) {
            String id = crsr.getString(crsr.getColumnIndex("product_id"));
            productID[j] = id;//product_id,name,price,quantity,model,image,manufacturer
            name = crsr.getString(crsr.getColumnIndex("name"));
            ProductName[j] = name;
            String price = crsr.getString(crsr.getColumnIndex("price"));
            ProductPrice[j] = price;
            String s = ProductPrice[j].toString();
            s = s.replace(",", "");
            String[] parts = s.split("\\."); // escape .
            String part1 = parts[0];
            String part2 = parts[1];
            part1 = part1.replace("₹", "");
            total = Integer.parseInt(part1); // Toast.makeText(Table.this, part1, Toast.LENGTH_SHORT).show();
            String qnty = crsr.getString(crsr.getColumnIndex("quantity"));
            ProductQuantity[j] = qnty;
            String s2 = ProductQuantity[j].toString();
            total1 = Integer.parseInt(s2);
            sum = total * total1;
            String model = crsr.getString(crsr.getColumnIndex("model"));
            ProductModel[j] = model;
            String image = crsr.getString(crsr.getColumnIndex("image"));
            ProductImage[j] = image;
            String manufacturer = crsr.getString(crsr.getColumnIndex("manufacturer"));
            ProductManufacturer[j] = manufacturer;
            //Toast.makeText(getApplicationContext(), productID[j] + "" + ProductName[j] + "" + ProductPrice[j] + "" + ProductQuantity[j] + "" + ProductModel[j] + "" + ProductImage[j] + "" + ProductManufacturer[j], Toast.LENGTH_SHORT).show();
            myList.add(new CartProducts(productID[j], ProductName[j], ProductPrice[j], ProductQuantity[j], ProductModel[j], ProductImage[j], ProductManufacturer[j]));
            gtot = gtot + sum;
            j++;
        }
        ListCart.setAdapter(new Wishlist_Listadapter(ctx, R.layout.activity_wishlist_cartrow, myList));
      getListViewSize(ListCart);
        String s1 = ProductPrice.toString();
    }

    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            if (listItem != null) {
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
        //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        if (params != null) {
            params.height = totalHeight
                    + (myListView.getDividerHeight() * (myListAdapter
                    .getCount() - 1));

            myListView.setLayoutParams(params);
            // print height of adapter on log
        }
        myListView.requestLayout();
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
    }
  

适配器类

Public class Wishlist_Listadapter  extends ArrayAdapter<CartProducts> {
    Bitmap bitmap;
    ImageView img;
    String urll, name,totalps;
    SQLiteDatabase FavData;
    Integer total = 0, quanty = 1, grandtot = 0, i = 0;
    String it;
    Button addbtn, minbtn;
    EditText editqu;
    int total1 = 0, quantity=0, fulltotal = 0, sum;
    SQLiteOpenHelper dbhelper;
    Wishlist_Listadapter cart = Wishlist_Listadapter.this;
    private int resource;
    private LayoutInflater inflater;
    private Context context;
    int count=1 ;

    public Wishlist_Listadapter(Context ctx, int resourceId, List<CartProducts> objects) {
        super(ctx, resourceId, objects);
        resource = resourceId;
        inflater = LayoutInflater.from(ctx);
        context = ctx;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        /* create a new view of my layout and inflate it in the row */
        convertView = (RelativeLayout) inflater.inflate(resource, null);

        final ViewHolder viewholder;
        viewholder = new ViewHolder();
        final CartProducts banqt = getItem(position);
        totalps=(banqt.getPrice());
        String s = totalps.toString();
        s = s.replace(",", "");
        String[] parts = s.split("\\."); // escape .
        String part1 = parts[0];
        String part2 = parts[1];
        part1 = part1.replace("₹", "");// Toast.makeText(getContext(), part1, Toast.LENGTH_LONG).show();
        total = Integer.parseInt(part1);


        quanty = Integer.parseInt(banqt.getQuantity());
        grandtot = total *quanty;
        viewholder.total = (TextView) convertView.findViewById(R.id.txt_total);
        viewholder.total.setText(String.valueOf(grandtot));
        Button delet = (Button) convertView.findViewById(R.id.btn_remove);
        delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*delete function*/
                it = banqt.getProduct_id();
                FavData = context.openOrCreateDatabase("SHOPPING_CARTFAV", context.MODE_PRIVATE, null);
                FavData.execSQL("DELETE FROM fav_items WHERE product_id=" + it + ";");
                Intent intent = ((Wishlist) context).getIntent();
                ((Wishlist) context).finish();
                context.startActivity(intent);
            }
        });
        viewholder.txtName = (TextView) convertView.findViewById(R.id.product_name);
        viewholder.txtName.setText(banqt.getName());
        img = (ImageView) convertView.findViewById(R.id.img_product);
        urll = banqt.getImage().toString();
        urll = urll.replaceAll(" ", "%20");// Toast.makeText(getContext(),urll,Toast.LENGTH_LONG).show();
        new LoadImage().execute(urll);
        return convertView;
    }
    static class ViewHolder {
        TextView txtName;
        TextView total;
        EditText editqu;
        TextView txtprice;
    }
    private class LoadImage extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected Bitmap doInBackground(String... args) {
            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap image) {
            if (image != null) {
                img.setImageBitmap(image);
                //    pDialog.dismiss();
            } else {
                //  pDialog.dismiss();
                Toast.makeText(getContext(), "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

listview工作正常 我只是在listview中夸大cardview。 使用此代码时图像无法显示。列表视图中只显示最后一张图片

params.height = totalHeight
                    + (myListView.getDividerHeight() * (myListAdapter
                    .getCount() - 1)); 
  

我的问题是: listview 仅显示最后一张图片

检查此图片:

image

2 个答案:

答案 0 :(得分:0)

尝试将ImageView添加到Holder课程并使用viewholder.img.setImageBitmap(new LoadImage().execute(urll)),并将返回类型更改为Bitmap

答案 1 :(得分:0)

使用BaseAdapter代替ArrayAdapter。使用UILPicasso或其他图像加载程序库加载并显示图像。

public class ImageAdapter extends BaseAdapter {
    private List<ImageBean> list;
    private ArrayList<ImageBean> arraylist;
    private LayoutInflater inflater;

    public ImageAdapter(Context context, List<ImageBean> list) {
        this.list = list;
        inflater = LayoutInflater.from(context);
        this.arraylist = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ImageBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.recycler_view_item, parent, false);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Loading image with UIL example
        ImageLoader.getInstance().displayImage(getItem(position).getUrl(), holder.ivImage, ImageUtils.UIL_USER_AVATAR_DISPLAY_OPTIONS);

        return convertView;
    }

    private class ViewHolder {
        public ImageView ivImage;
    }

}