Android Picasso Framework,异步加载图片

时间:2013-06-20 10:00:45

标签: android image listview frameworks

我试图在我的MainActivity中添加“Picasso Framework”,但它一直给我一个错误。正是在这条线“Picasso.with(MainActivity.this)”中,“毕加索”下面有一条红线。有人可以帮帮我吗。我导入了jar.file。

请查看我的代码:

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 个答案:

答案 0 :(得分:1)

试试这个 -

通过添加CustomAdapter变量来修改您的private Context课程。然后,在CustomAdapter的构造函数中,初始化该变量。

然后,每当您需要进行Context来电时,您都会有一个Picasso参考。

因此,自定义适配器应如下所示:

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;

    }...

然后让Picasso在getView()中调用“

Picasso.with(mContext)...并添加您需要的任何内容。

希望这有帮助!