虽然调用了getCount(),但Gridview适配器不会调用getView()

时间:2016-01-12 04:16:18

标签: android gridview android-adapter

我已经尝试逐字逐句地复制其他人的代码,但我似乎无法让GridView显示除白页之外的任何内容。 getCount()被调用并返回正确的数字,但是,getView()未被调用。任何想法将不胜感激。

public class drinkSaleList extends Activity {

private ArrayList<String> drinkPhotoPaths = new ArrayList<String>();

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

    GridView gridview = (GridView) findViewById(R.id.gridview);

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int drinkCount = sharedPref.getInt("Drink", 0);

    if(drinkCount > 0)
    {
        for(int i = 1; i <= drinkCount; i++){
            String path = getExternalFilesDir(null) + "/drinkObject" + i + ".ser";

            drinkPhotoPaths.add(path);
        }
    }

    gridview.setAdapter(new drinkSaleLayoutAdapter(this, drinkPhotoPaths));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            //Create an Intent to start the ImageViewActivity
            Intent intent = new Intent(drinkSaleList.this,
                    SaleActivity.class);
            Toast.makeText(drinkSaleList.this, "You clicked " + position + "and Id is " + id, Toast.LENGTH_SHORT).show();
            // Add the ID of the thumbnail to display as an Intent Extra
            // intent.putExtra(EXTRA_RES_ID, (int) id);

            // Start the ImageViewActivity
            // startActivity(intent);
        }
    });

}
public class drinkSaleLayoutAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mPhotoPaths;


    public drinkSaleLayoutAdapter(Context c, List<String> photoPaths){

        mContext = c;
        mPhotoPaths = photoPaths;
    }

    public int getCount(){
        int x = mPhotoPaths.size();
        return mPhotoPaths.size();

    }

    public Object getItem(int position){

        return mPhotoPaths.get(position);
    }

    public long getItemId(int position){

        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent){

        ImageView imageView;

        if(convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);

        }
        else{
            imageView = (ImageView) convertView;
        }

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(mPhotoPaths.get(position));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Bitmap bm = BitmapFactory.decodeStream(fis);
        imageView.setImageBitmap(bm);

        return imageView;
    }
}

}

2 个答案:

答案 0 :(得分:0)

代码似乎是正确的,除了一条非常重要的缺失线。在getView()函数中,在else条件下,您将convertView分配给imageView实例,但实际上convertView未被充气或未初始化。

您需要更改以下代码:

if(convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
            convertView = imageView;
        }
        else{
            imageView = (ImageView) convertView;
        }

我希望通过更改代码来实现它。

答案 1 :(得分:0)

你没有设置convertView所以它变为空。首先,您必须设置convertView。 你应该试试这个......

    public View getView(int position, View convertView, ViewGroup parent){
      ViewHolder viewHolder;


            if(convertView == null){
LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.your_view, null);    // you need one xml file which has parent layout to hold imageview
viewHolder = new ViewHolder;
viewHolder.linearLayout = converView.findViewById(R.id.linearLayout);   //your parent layout id which is defined in xml file
                viewHolder.imageView = new ImageView(mContext);
                viewHolder.imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                viewHolder.imageView.setPadding(8,8,8,8);
    viewHolder.linearLayout.addView(viewHolder.imageView);   // add imageview on layout
convertView.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            FileInputStream fis = null;
            try {
                fis = new FileInputStream(mPhotoPaths.get(position));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            Bitmap bm = BitmapFactory.decodeStream(fis);
            viewHolder.imageView.setImageBitmap(bm);

            return convertView;
        }

private static class ViewHolder
{
     LinearLayout linearLayout;
    ImageView imageView;
}

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"'
    android:id="@+id/linearLayout""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

希望它可以帮到你