Incorrect data retrieved from item in listview share button android

痞子三分冷 提交于 2019-12-12 04:31:17

问题


I have a listview which displays items retrieved from the database. Each item in the listview has a share button which allows the user to share the information. However when I click on the share button, the information displayed is different from the item I clicked. And the no matter which item I click, it will be getting information of the first few item.

For example, clicking on item N 2 would display info of item N 3. In other words, no matter which item I click, it will be displaying wrong information. Am I doing anything wrong here?

**The listview displays items correctly flawlessly.

Adapter.java

@Override
public View getView(final int  position, View convertView,  ViewGroup parent) {
    // convert view = design
    View v = convertView;

    if(convertView==null){

    v = vi.inflate(Resource, null);
    holder = new ViewHolder();

    holder.titre = (TextView) v.findViewById(R.id.titre);
    holder.artist = (TextView) v.findViewById(R.id.artist); 
    holder.imageview = (ImageView) v.findViewById(R.id.urlImage);
    v.setTag(holder);      


        }

    else

        holder=(ViewHolder)v.getTag();
        holder.titre.setText(mediaList.get(position).getTitre());
        holder.artist.setText(mediaList.get(position).getArtist());
        imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );


        Button  button = (Button) v.findViewById(R.id.btnOne);
        //holder=(ViewHolder)v.getTag();
       button.setOnClickListener(new View.OnClickListener() { 
             @Override 
             public void onClick( View v) {
                //holder=(ViewHolder)v.getTag(); 
                //v.setTag(holder);
                 //onShareItem( v);
                 v.getTag();


                 Drawable mDrawable =  holder.imageview.getDrawable();
                 Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

                 Intent sharingIntent = new Intent(Intent.ACTION_SEND);

                 sharingIntent.setType("image/png");

                 ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                 mBitmap.compress(Bitmap.CompressFormat.PNG, 1, bytes);

                 String path = Images.Media.insertImage(context.getContentResolver(), 
                            mBitmap, "Image Description", null);

                 Uri imageUri = Uri.parse(path);
                 sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                 sharingIntent.setType("image/*");

                 sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity( sharingIntent); 


             }



        });
       v.setTag(holder);

       return v;





}

回答1:


solution

@Override
public int getCount() {
    return mediaList.size();
} //
@Override
public Media getItem(int position) {
    return mediaList.get(position);
}
@Override
public long getItemId(int position) {
    return position;
} 

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    int loader = R.drawable.ic_launcher;
    if (view == null) {
        holder = new ViewHolder();
        view = vi.inflate(R.layout.row, null);

        holder.imageview = (ImageView) view.findViewById(R.id.urlImage);

        holder.titre = (TextView) view.findViewById(R.id.titre);

        holder.artist = (TextView) view.findViewById(R.id.artist);

        holder.button1 = (Button) view.findViewById(R.id.btnOne);
        holder.button2 = (Button) view.findViewById(R.id.btnOne);
        view.setTag(holder);

    }else{
        holder = (ViewHolder) view.getTag();    
    }
    imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );
    holder.titre.setText(mediaList.get(position).getTitre());
    holder.artist.setText(mediaList.get(position).getArtist());
    holder.button1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {                               

                //holder=(ViewHolder)v.getTag(); 
                v.setTag(holder);
                 //onShareItem( v);
                int i=  (int) getItemId( position);
                v.getTag(i);
                // v.getTag(holder);

                 //partager *****************************************

                 Drawable mDrawable =  holder.imageview.getDrawable();
                 Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

                 Intent sharingIntent = new Intent(Intent.ACTION_SEND);

                 sharingIntent.setType("image/png");

                 ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                 mBitmap.compress(Bitmap.CompressFormat.PNG, 1, bytes);

                 String path = Images.Media.insertImage(context.getContentResolver(), 
                            mBitmap, "Image Description", null);

                 Uri imageUri = Uri.parse(path);
                 sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                 sharingIntent.setType("image/*");

                 sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity( sharingIntent); 
             /////:::::::::::::::::::::::::::::::::::::::::::

         }                              
    });         



    return view;
}


来源:https://stackoverflow.com/questions/37059906/incorrect-data-retrieved-from-item-in-listview-share-button-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!