Question

Making a custom spinner and I want to asynchronously retrieve images and json from the web. I found a library to help with that, loopj, and it works perfectly, however when I try to put it in my Adapter class, it doesn't like "findViewById" since I am not extending Activity, but I am already extending ArrayAdapter. Is there any way to extend both? I don't fully understand extend, so I apologize if that's a stupid question, but how can I solve my problem? Instead of hardcoding images in I would much rather be able to import them from web.

My problem is at the "SmartViewImage image = ..." code. I am not sure how to solve this issue and still be able to asynchronously do what I want. I will also want to retrieve text so I am kind of stuck at this point. Any help is appreciated.

public class CustomAdapter extends ArrayAdapter<String>{

    private Activity activity;
    private ArrayList data;
    public Resources res;
    SpinnerModel tempValues=null;
    LayoutInflater inflater;



    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(
                          MainActivity activitySpinner, 
                          int textViewResourceId,   
                          ArrayList objects,
                          Resources resLocal
                         ) 
     {
        super(activitySpinner, textViewResourceId, objects);
        /********** Take passed values **********/
        activity = activitySpinner;
        data     = objects;
        res      = resLocal;


        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


      }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

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

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

        /********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
        View row = inflater.inflate(R.layout.spinner_rows, parent, false);

        /***** Get each Model object from Arraylist ********/
        tempValues = null;
        tempValues = (SpinnerModel) data.get(position);

        TextView label        = (TextView)row.findViewById(R.id.course);
        TextView sub          = (TextView)row.findViewById(R.id.sub);
        ImageView companyLogo = (ImageView)row.findViewById(R.id.image);

        SmartImageView image = (SmartImageView) 
                findViewById(R.id.my_image);

        if(position==0){

            // Default selected Spinner item 
            label.setText("Please select course");
            sub.setText("");
        }
        else
        {
            // Set values for spinner each row 
            label.setText(tempValues.getCourseName());
            sub.setText(tempValues.getCourseShortDesc());
            companyLogo.setImageResource(res.getIdentifier("com.androidexample.customspinner:drawable/"+tempValues.getImage(),null,null)); //line that gets image//////


            image.setImageUrl("http://iam.colum.edu/students/jordan.max/poker.png");

        }   

        return row;
      }
}
Was it helpful?

Solution 2

Sorry, but java doesn't allow multiple inheritance. So you cannot make your CustomAdapter that extends ArrayAdapter and Activity. There is an interesting discussion about that here So you need to find another way of doing that

OTHER TIPS

You already have activity field in your adapter, use this:

SmartImageView image = (SmartImageView) 
                activity.findViewById(R.id.my_image);

but, as I see you dont set activity to anything, you can (I suppose) use activity = activitySpinner; in your constructor.

Also, if your CustomAdapter is internal to MainActivity, then you can simply use MainActivity.this.

Java does not support multiple inheritance, so you cannot extend from two classes.

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