我想用毕加索在gridview中显示图像。但是图像始终无法加载。它总是显示错误图片。谁能帮我? 我正在使用
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
CustomGridViewAdapter.java
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import okhttp3.OkHttpClient;
/**
* Created by Nicky-PC on 4/9/2016.
*/
public class CustomGridViewAdapter extends ArrayAdapter<gridItem> {
Context context;
int layoutResourceId;
ArrayList<gridItem> data = new ArrayList<gridItem>();
private Picasso picasso;
private OkHttpClient okHttpClient;
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<gridItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public void setGridData(ArrayList<gridItem> mGridData)
{
this.data=mGridData;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.tvItem);
holder.imageItem = (ImageView) row.findViewById(R.id.imageView);
holder.txtPrice=(TextView) row.findViewById(R.id.tvPrice);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
gridItem item = data.get(position);
holder.txtTitle.setText(Html.fromHtml(item.getBarang()));
holder.txtPrice.setText(Html.fromHtml(item.getPrice()));
// Picasso.Builder builder = new Picasso.Builder(context);
// builder.listener(new Picasso.Listener() {
// @Override
// public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
// exception.printStackTrace();
// }
// });
// builder.build().load(item.getImageUrl()).into(holder.imageItem);
//
Picasso.with(context)
.load(item.getImageUrl())
.centerCrop()
.resize(150,150)
.onlyScaleDown()
.placeholder(R.drawable.rotate)
.error(R.drawable.shapes)
.into(holder.imageItem);
return row;
}
static class ViewHolder {
TextView txtTitle;
ImageView imageItem;
TextView txtPrice;
}
}
MainActivity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ProgressDialog;
import android.os.Asy[enter image description here][1]ncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends ActionBarActivity {
ProgressDialog pDialog;
JSONParser jParser= new JSONParser();
ArrayList<HashMap<String, String>> nameList;
JSONArray names=null;
GridView data;
private static final String URL_TEST_BARANG= "http://lomapod.azurewebsites.net/readBarang.php";
private static final String TAG_PESAN = "message";
private static final String TAG_HASIL = "result";
private static final String TAG_BARANG = "nama_barang";
private static final String TAG_IMAGE= "image_name";
private static final String TAG_PRICE= "harga";
ImageView imageview;
CustomGridViewAdapter mGridAdapter;
ArrayList<gridItem> mGridData;
public MainActivity()
{}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=(GridView) findViewById(R.id.lvSeller);
nameList= new ArrayList<HashMap<String, String>>();
imageview =(ImageView) findViewById(R.id.imageView);
mGridData=new ArrayList<>();
mGridAdapter=new CustomGridViewAdapter(this,R.layout.grid_item,mGridData);
new AmbilDataJson("1","4").execute();
}
public class AmbilDataJson extends AsyncTask<String,String,String> {
String id,kat;
int sukses=0;
public AmbilDataJson(String id,String kat) {
this.id=id;
this.kat=kat;
pDialog = new ProgressDialog(MainActivity.this);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Mengambil Data. Silahkan Tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id_penjual",id));
params.add(new BasicNameValuePair("id_kategori",kat));
try
{
JSONObject json = jParser.makeHttpRequest(URL_TEST_BARANG, "GET", params);
gridItem item=null;
if(json != null)
{
sukses = json.getInt(TAG_PESAN);
if(sukses == 0)
{
nameList = new ArrayList<HashMap<String,String>>();
Log.d("Semua Nama: ", json.toString());
names = json.getJSONArray(TAG_HASIL);
for(int i = 0; i < names.length();i++)
{
JSONObject c = names.getJSONObject(i);
String barang = c.getString(TAG_BARANG);
String image = "lomapod.esy.es/assets/"+c.getString(TAG_IMAGE);
String price = c.getString(TAG_PRICE);
item = new gridItem();
item.setBarang(barang);
item.setImageUrl(image);
item.setPrice(price);
HashMap<String,String> map = new HashMap<String,String>();
map.put(TAG_BARANG,barang);
map.put(TAG_IMAGE,image);
map.put(TAG_PRICE,price);
nameList.add(map);
mGridData.add(item);
}
}
}
}catch(JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
pDialog.dismiss();
if(sukses == 0)
{
CustomGridViewAdapter adapter = new CustomGridViewAdapter(
MainActivity.this,R.layout.grid_item,mGridData);
if(mGridData.size()>0)
{
mGridAdapter.setGridData(mGridData);
data.setAdapter(adapter);
}
else
Toast.makeText(getApplicationContext(),"Error - No Data Available", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_LONG);
}
}
}
}
这是它的样子