mardi 29 décembre 2015

Contact Image in listview is repeating when use a Baseadapter

I am building an application where I am storing all contact data in my sqlite database and populating that data in listview.

In the process the contact images are repeating and is displayed without any ordering.

The adapter:

private class ContactAdapter extends SimpleCursorAdapter

    {
        //Context context;
        ArrayList<HashMap<String, String>> data;
        private Cursor c;
        private Context context;
        LayoutInflater inflater;
        public ContactAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.c = c;
            this.context = context;
            inflater = LayoutInflater.from(this.context);
        }


        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolderContact viewHolder;

            //viewHolder = new ViewHolderContact();


            if(convertView==null)
            {
                viewHolder = new ViewHolderContact();

                convertView = inflater.inflate(R.layout.layout_contactlist, null);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.ivimage);
                viewHolder.imgNext = (ImageView) convertView.findViewById(R.id.imgnext);
                viewHolder.textView_Name = (TextView)convertView .findViewById(R.id.txtname);

            //  view.imgad.setScaleType(ImageView.ScaleType.FIT_XY);
                /*view.imgad.setScaleType(ImageView.ScaleType.FIT_XY);
                //view.imgad.setPadding(0,10,0,0);
                //view.imgad.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,100));
                //LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,90);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,90);
                lp.setMargins(0,20,0,0);
                view.imgad.setLayoutParams(lp);
                 */
                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder = (ViewHolderContact)convertView.getTag();
            }

            this.c.moveToPosition(position);     
            String contactid = this.c.getString(this.c.getColumnIndex("_id"));
            String contactname = this.c.getString(this.c.getColumnIndex("contactname"));
            String contactnumber = this.c.getString(this.c.getColumnIndex("contactnumber"));
            String contactimage= this.c.getString(this.c.getColumnIndex("contactimage"));
            String isInstalled= this.c.getString(this.c.getColumnIndex("isInstalled"));


            //System.out.println("Isinstalled--->"+isInstalled);

            if(isInstalled.equals("Y"))
            {
                viewHolder.imgNext.setVisibility(View.GONE);
            }


            if (contactimage == null || contactimage.equals("")) {
                if (Build.VERSION.SDK_INT >= 16) {
                    viewHolder.imageView.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
                }
                else {
                    viewHolder.imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                }
                // If there is no image in the database "NA" is stored instead of a blob 
                // test if there more than 3 chars "NA" + a terminating char if more than
                // there is an image otherwise load the default
            }   //iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            else{   
                try {
                    Bitmap bmp=getContactBitmapFromURI(ContactList.this,Uri.parse(contactimage));
                    Bitmap round=getRoundedShape(bmp);
                    viewHolder.imageView.setImageBitmap(round);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            viewHolder.textView_Name.setText(contactname);

            return convertView;                 

        }

        class ViewHolderContact {
            TextView textView_Name;
            ImageView imageView,imgNext;

        }

    }

c is the cursor instance

The method getContactBitmapFromURI(Context context, Uri uri)

public static Bitmap getContactBitmapFromURI(Context context, Uri uri) throws FileNotFoundException {
    InputStream input = context.getContentResolver().openInputStream(uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

Output:

Instance 1

When I scroll then the output becomes:

Instance 2

As you can see that the image is not properly displayed.They are repeating or/and disappearing.Please help.

Aucun commentaire:

Enregistrer un commentaire