1

I am trying to show the images on the gridView but getting following errors:-

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
 at com.hubdatasolutions.receiptcloud.ReceiptTabActivity$ReceiptAdapter.getView(ReceiptTabActivity.java:128)

row_grid.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp" >
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/item_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:contentDescription="clickedImage"
            android:src="#ffffff" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
             android:layout_alignParentRight="true"
             android:src="@drawable/close"
           />
    </RelativeLayout>
        <TextView
            android:id="@+id/item_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textSize="15sp" >
        </TextView>

    </LinearLayout>

activity_receipt_tab.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/mainLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@drawable/menu_bg"
         tools:context=".ReceiptTabActivity" >
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical" >
             <ImageView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:background="@drawable/logo" />
             <GridView
                 android:id="@+id/gridView"
                 android:layout_width="match_parent"
                android:layout_height="0dp"
                 android:layout_weight="1"
                 android:columnWidth="90dp"
                 android:gravity="center"
                 android:horizontalSpacing="10dp"
                 android:numColumns="auto_fit"
                 android:stretchMode="columnWidth"
                android:verticalSpacing="10dp" >
             </GridView>
             <include layout="@layout/tab_footer" />
         </LinearLayout>
     </RelativeLayout>

ReceptTabActivity.java

This is the java file here I am getting error. This is the file I tried to develop two classes. i am inflating the gridView in the same file.

    public class ReceiptTabActivity extends BaseTabActivity {

    GridView mGrid;
    ReceiptAdapter mAdapter;
    ArrayList<ReceiptData> mReceiptList = new ArrayList<ReceiptData>();

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

        mReceiptList = APICaller.getInstance().getReceiptList();


        mGrid = (GridView) findViewById(R.id.gridView);
    //  mAdapter = new ReceiptAdapter(this, R.layout.receipt_grid_item);
        //mGrid.setAdapter(mAdapter);
        mAdapter = new ReceiptAdapter(this,R.layout.row_grid);
        mGrid.setAdapter(mAdapter);
        APICaller.getInstance().downloadPicData();
    }

    @Override
    public void onRefreshData(Refreshable refreshable, int requestCode) {
        if (requestCode == APICaller.DOWNLOAD_PIC_REQUEST_CODE) {
            for (ReceiptData receipt : mReceiptList) {
                String filePath = receipt.getFilePath();
                if (filePath != null && !filePath.isEmpty()) {
                    File file = new File(filePath);
                    Bitmap bitmap = decodeFile(file);
                    if (bitmap != null)
                        receipt.setImage(bitmap);
                    else
                        Log.i("Bitmap", "Still null");
                } else {
                    Log.i("String", "Still null");
                }
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    // decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    private Bitmap decodeOriginalFile(File f) {
        try {

            return BitmapFactory.decodeStream(new FileInputStream(f));
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    class ReceiptAdapter extends ArrayAdapter<View> {
        Context mContext;
        int mResource;
        ArrayList<ReceiptData> data = new ArrayList<ReceiptData>();
        public ReceiptAdapter(Context context, int resource) {
            super(context, resource);
            mContext = context;
            mResource = resource;
        }

        @Override
        public int getCount() {
            return mReceiptList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //ImageButton mainView;
            ImageView mainView;
            //View row = convertView;
            //RecordHolder holder = null;

            mainView = (ImageView) findViewById(R.id.item_image);
            //String info = ImageView.getText().toString();
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            mainView = (ImageView) inflater.inflate(mResource, parent, false);


            Bitmap bitmap = mReceiptList.get(position).getImage();
            if (bitmap != null)
                mainView.setImageBitmap(bitmap);

            return mainView;
        }

    }
}  

3 Answers 3

1

Initialize inflater in constructor of adapter class

LayoutInaflater inflater;
public ReceiptAdapter(Context context, int resource) {
            super(context, resource);
            mContext = context;
            mResource = resource;
            infalter= LayoutInflater.from(context);
        }

Change getView to

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

        ViewHolder viewHolder;
        if(convertView==null)
        { 
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row_grid, null);
        // inflate row_grid.xml  

        viewHolder.mainView= (ImageView)convertView.findViewById(R.id.item_image);
        //initialize imageview
        convertView.setTag(viewHolder);
        }else{
         viewHolder = (ViewHolderItem) convertView.getTag();

        }
        Bitmap bitmap = mReceiptList.get(position).getImage();
        if (bitmap != null)
           holder.mainView.setImageBitmap(bitmap); 
           // set bitmap to imageview

        return convertView;
    }

Use a ViewHolder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

static ViewHolder
{
     ImageView mainView;
}   
Sign up to request clarification or add additional context in comments.

Comments

0

For these two lines:

    mainView = (ImageView) findViewById(R.id.item_image);
...
    mainView = (ImageView) inflater.inflate(mResource, parent, false);

It seems you made a copy paste error. Yes you want the first one to be cast to ImageView, but I think the second mainView that is being inflated should be the LinearLayout in the first xml snippet you wrote there?

Comments

0

in your adapter's getView, inflate the row_grid first and then use it to look for your ImageView

  View rowview = (View) inflater.inflate(mResource, parent, false);
  mainView = (ImageView) rowview.findViewById(R.id.item_image);

do any customizations you need and then return the row_grid (rowview in the above code)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.