1

I have tried to inplement the "Picasso Framework" in my MainActivity, but it keeps giving me an error. It is in this line "Picasso.with(MainActivity.this)", the "Picasso" has an red line under it. Can someone please help me. I have imported the jar.file.

Please look at my code:

package com.chrfugl.xxxx;

import java.util.List;
import java.util.Vector;

import com.chrfugl.studenterhuset.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

public static final String[] title = new String[] {
        "1", 
        "2",
        "3",
        "4",
        "5, 
        "6",
        };

public static final String[] detail = new String[] {
    "Text",
    "Text",
    "Text",
    "Text",
    "Text",
    "Text",
    };

public static Integer[] imgid = { 
    R.drawable.1, R.drawable.2, 
    R.drawable.3, R.drawable.4, 
    R.drawable.5, R.drawable.6, 

};

public static final String[] footer = new String[] {
    "Text", 
    "Text",
    "\Text",
    "\Text",
    "\Text",
    "\Text",
    }; 

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mInflater = (LayoutInflater)     getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();
    for (int i = 0; i < title.length; i++) {

        try {
            rd = new RowData(i, title[i], detail[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.add(rd);
    }
    CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item, R.id.title, data);
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
}

public void onListItemClick(ListView parent, View v, int position, long id) {
    Intent intent = new Intent(this, NewActivity.class);
    intent.putExtra("position", position);
    startActivity(intent);
}

private class RowData {
    protected int mId;
    protected String mTitle;
    protected String mDetail;

    RowData(int id, String title, String detail) {
        mId = id;
        mTitle = title;
        mDetail = detail;
    }

    @Override
    public String toString() {
        return mId + " " + mTitle + " " + mDetail;
    }
}

private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {

        super(context, resource, textViewResourceId, objects);
    }

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

        ViewHolder holder = null;
        TextView title = null;
        TextView detail = null;
        ImageView i11 = null;
        RowData rowData = getItem(position);
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        title = holder.gettitle();
        title.setText(rowData.mTitle);
        detail = holder.getdetail();
        detail.setText(rowData.mDetail);

        i11 = holder.getImage();
        Picasso.with(MainActivity.this)
        .load(imgid[rowData.mId])
        .placeholder(android.R.color.black)
        .error(android.R.color.black)
        .resize(100, 100)
        .centerCrop()
        .into(i11);
        return convertView;
    }

    private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private TextView detail = null;
        private ImageView i11 = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView gettitle() {
            if (null == title) {
                title = (TextView) mRow.findViewById(R.id.title);
            }
            return title;
        }

        public TextView getdetail() {
            if (null == detail) {
                detail = (TextView) mRow.findViewById(R.id.detail);
            }
            return detail;
        }

        public ImageView getImage() {
            if (null == i11) {
                i11 = (ImageView) mRow.findViewById(R.id.img);
            }
            return i11;
        }
    }
}
}
1

1 Answer 1

1

Try this-

Edit your CustomAdapter class by adding a private Context variable. Then, in the constructor of the CustomAdapter, initialize that variable.

Then you will have a Context to reference every time you need to make a Picasso call.

So, Custom Adapter should look like this:

private class CustomAdapter extends ArrayAdapter<RowData> {

    private Context mContext;

    public CustomAdapter(Context context, int resource,
        int textViewResourceId, List<RowData> objects) {

        super(context, resource, textViewResourceId, objects);
        mContext = context;

    }...

Then make Picasso calls in getView() like this"

Picasso.with(mContext)... and add whatever else you need.

Hope this helps!

Sign up to request clarification or add additional context in comments.

9 Comments

Can you explain me the bit with the getView()? I am not sure what you want me to do? And thanks for your answer!
Your Picasso call looks fine as it is right now in getView(). I think your problem may actually be that you aren't importing the Picasso library. Try adding import com.squareup.picasso.Picasso; to your imports.
Hi again. I have now imported picasso, and now the error is gone, so thank you! But when i try to run the app on my device (nexus 4) it crashes when booting. Do you have any idea why this is?
And should i not edit my CustomAdapter then, as you said in your first answer?
I would need to see the Log as to why your app is crashing. Also- you can edit your CustomAdapter, but I'm not sure its necessary. I would do it just to be safe.
|

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.