Question

I am trying to display the images and text for the grid view. I am getting text from database and store it into ArrayList for later use. same I am getting the images form sd card and display it.

The code I write working nice but the problem is getView() parameter position not work normally. position start with 0 and also end with 0. so the last item of the grid is same as the first. bellow is my code

public class ImageAdapter extends BaseAdapter
    {
        private LayoutInflater inflater;
        public ImageAdapter()
        {
            // TODO Auto-generated constructor stub
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() 
        {
            // TODO Auto-generated method stub
            return listMainProduct.size();
//          return arrGrpName.size();
        }

        @Override
        public Object getItem(int position) 
        {
            // TODO Auto-generated method stub
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            // TODO Auto-generated method stub

            ViewHolder holder = null;
            if(convertView == null)
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.album_item, null);
                holder.imgView = (ImageView)convertView.findViewById(R.id.thumbImage);
                holder.txtView = (TextView)convertView.findViewById(R.id.tv);
                holder.txtView.setText(arrGrpName.get(position).toString());

                //Log.v("Text ", "Text :- "+arrGrpName.get(position).toString());
            Log.v("Position ", "Position :- "+position);

                BitmapFactory.Options options=new BitmapFactory.Options();
                options.inSampleSize = 2;

                String path = "/mnt/sdcard/JS_Images";
                File imgFile = new File(path+"/"+arrImageName.get(position).toString()+".jpg");
//              Log.v("Path", ""+(path+"/"+arrImageName.get(position).toString())+".jpg");

                Bitmap myBitmap = null;
                if(imgFile.exists())
                {
                    myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options);
//                  myBitmap = BitmapFactory.decodeFile(arrImageName.get(position),options);
                    holder.imgView.setImageBitmap(myBitmap);
                }
                else  
                {
                }
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

        return convertView;
        }

    }
    class ViewHolder
    {
        ImageView imgView;
        TextView txtView;
    }

here is my log cat

08-16 12:40:45.614: V/Position(27928): Position :- 0
08-16 12:40:45.667: V/Position(27928): Position :- 1
08-16 12:40:45.687: V/Position(27928): Position :- 2
08-16 12:40:45.691: V/Position(27928): Position :- 3
08-16 12:40:45.710: V/Position(27928): Position :- 4
08-16 12:40:45.717: V/Position(27928): Position :- 5
08-16 12:40:45.723: V/Position(27928): Position :- 6
08-16 12:40:45.743: V/Position(27928): Position :- 7
08-16 12:40:45.746: V/Position(27928): Position :- 8
08-16 12:40:45.751: V/Position(27928): Position :- 0

Album.class

public class Album extends Activity 
{
    public static DataSource dataSource;
    private ImageAdapter adapter;

    List<getMainProduct> listMainProduct;
    List<getStyleMst> listStyleMst;

    /**
     * Array of Strings (for temp storage of data form Database)
     **/

    ArrayList<String> arrGrpName = new ArrayList<String>();
    ArrayList<String> arrImageName = new ArrayList<String>();
    ArrayList<String> arrGrpNo = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.album);

        dataSource = new DataSource(Album.this);
        listMainProduct = dataSource.getMainProductData();

        /**
         * Getting ImageFolder & ImageName from Database 
         **/

        for(int i = 1 ; i <= listMainProduct.size() ; i++)
        {
            String repImg = null;
            String StrIMGS = dataSource.getImageNameForAlbum(i);
            if(StrIMGS!=null)
            {
                repImg = StrIMGS.replaceAll("%20", " ");
            }
//          Log.v("ImageName ", ""+ StrIMGS);
            String StrFolder = dataSource.getImageFolderFromDB(i);

//          Log.v("FolderName ", ""+ StrFolder);
            Log.v("Path ", ""+ StrFolder+"/"+repImg);
            String Path = StrFolder+"/"+repImg;
            arrImageName.add(Path);
//          Log.v("IMG Size ", "Size :- "+arrImageName);
        }

        /**
         * Getting GroupName & GrpNo form Database
         **/

        for(int i = 0 ; i < listMainProduct.size() ; i++)
        {
            String strGrpName = listMainProduct.get(i).getGrpName();
            int intGrpNo = listMainProduct.get(i).getGrpNo();
            arrGrpName.add(strGrpName);
            Log.v("ArrName ", "ArrayText :- "+arrGrpName.toString());
            arrGrpNo.add(String.valueOf(intGrpNo));
            Log.v("ArrNo ", "ArrayTextNo :- "+arrGrpNo.toString());
        }


        GridView gridView = (GridView)findViewById(R.id.AlbumGrid);
        adapter = new ImageAdapter();
        gridView.setAdapter(adapter);

        /** 
         * When Grid Item Selected or Clicked 
         **/

        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
            {
                // TODO Auto-generated method stub
                Toast.makeText(Album.this, ""+arrGrpNo.get(position).toString(), Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Album.this,Gallery.class);
                i.putExtra("GrpNo", arrGrpNo.get(position).toString());
                startActivity(i);
            }
        });
    }

please need serious help i don't know what happen here thanks in advance

Was it helpful?

Solution

In if(convertView==null) block just get reference of views and setTag to them, in else part get previous tag. Apply all other things after else

{
    holder = (ViewHolder) convertView.getTag();
}

make following changes and try

ViewHolder holder = null;
if(convertView == null)
{
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.album_item, parent,false);
    holder.imgView = (ImageView)convertView.findViewById(R.id.thumbImage);
    holder.txtView = (TextView)convertView.findViewById(R.id.tv);
    convertView.setTag(holder);
 }
 else
 {
     holder = (ViewHolder) convertView.getTag();
 }

 holder.txtView.setText(arrGrpName.get(position).toString());

 //Log.v("Text ", "Text :- "+arrGrpName.get(position).toString());
 Log.v("Position ", "Position :- "+position);

 BitmapFactory.Options options=new BitmapFactory.Options();
 options.inSampleSize = 2;

 String path = "/mnt/sdcard/JS_Images";
 File imgFile = new File(path+"/"+arrImageName.get(position).toString()+".jpg");
 //Log.v("Path", ""+(path+"/"+arrImageName.get(position).toString())+".jpg");

 Bitmap myBitmap = null;
 if(imgFile.exists())
 {
     myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options);
     //myBitmap = BitmapFactory.decodeFile(arrImageName.get(position),options);
     holder.imgView.setImageBitmap(myBitmap);
 }
 else  
 {
 }

 return convertView;

OTHER TIPS

the position issue is normal, and (i think) google talks about it on "the world of listView" lecture . it occurs when extra re-calculation is required for the layout phase. it is not the last item.

what you can do is to hold the position of the item in the viewHolder (always, no matter what is the situation of convertView) , and check if it's the same as the parameter. if it is, just return the view . if not, fill it with the updated data according to the new position.

another issue that i've found in your code is that you fill the data only if convertView==null.

this is wrong. you need to fill the data no matter what is the situation with convertView. you replace convertView only if it's null, and fill its viewHolder with the views in order to avoid un-needed calls for findViewById.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top