3

我正在尝试显示网格视图的图像和文本。我从数据库中获取文本并将其存储到ArrayList以供以后使用。同样,我从 sd 卡中获取图像并显示它。

我编写的代码运行良好,但问题是getView()参数位置无法正常工作。位置以 0 开头,也以 0 结尾。所以网格的最后一项与第一项相同。下面是我的代码

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;
    }

这是我的原木猫

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

专辑.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);
            }
        });
    }

请需要认真的帮助我不知道这里发生了什么提前谢谢

4

2 回答 2

1

if(convertView==null)块中只获取对它们的引用viewssetTag在其他部分中获取前一个标签。在 else 之后应用所有其他的东西

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

进行以下更改并尝试

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;
于 2013-08-16T07:18:42.397 回答
0

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.

于 2013-08-16T08:54:17.907 回答