出于某种原因,当使用Picasso库从服务器检索图像URL时,无法将图像加载到列表视图但是,imageURL确实传递到PicassoClient
类。共有10幅图像,每幅图像的大小约为20kB。
仅供参考,我已经提到了许多例子,但问题无法解决。请提供一些建议,说明如何解决这些问题。
searchActivity.java
public class searchActivity extends AppCompatActivity{
final static String urlAddress = "http://10.0.2.2/Herb/herblist.php";
ListView lv = (ListView) findViewById(R.id.HerbListView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Downloader dl = new Downloader(searchActivity.this, urlAddress, lv);
dl.execute();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(searchActivity.this, searchDetail.class));
}
});
}
}
DataParser.java
public class DataParser extends AsyncTask<Void,Void,Integer> {
Context c;
String jsonData;
ListView lv;
ProgressDialog pd;
ArrayList<Herb> herb=new ArrayList<>();
public DataParser(Context c, String jsonData, ListView lv) {
this.c = c;
this.jsonData = jsonData;
this.lv = lv;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
pd.dismiss();
...
CustomAdapter adapter=new CustomAdapter(c,herb);
lv.setAdapter(adapter);
}
private int parseData()
{
try
{
JSONArray ja=new JSONArray(jsonData);
JSONObject jo=null;
herb.clear();
Herb Herb;
for(int i=0;i<ja.length();i++)
{
jo=ja.getJSONObject(i);
int id=jo.getInt("h_id");
String name=jo.getString("h_name");
String imageUrl=jo.getString("h_image");
Herb=new Herb();
Herb.setId(id);
Herb.setName(name);
Herb.setImageUrl(imageUrl);
herb.add(Herb);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Herb> herbs;
LayoutInflater inflater;
public CustomAdapter(Context c, ArrayList<Herb> herbs) {
this.c = c;
this.herbs = herbs;
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=inflater.inflate(R.layout.model,parent,false);
}
TextView nametxt= (TextView) convertView.findViewById(R.id.herbtitle);
ImageView img= (ImageView) convertView.findViewById(R.id.thumbnail);
Herb herb = herbs.get(position);
nametxt.setText(herb.getName());
PicassoClient.downloadImage(c,herb.getImageUrl(),img);
return convertView;
}
}
PicassoClient.java
public class PicassoClient {
public static void downloadImage(Context c, String imageUrl, ImageView img){
if (imageUrl.length() > 0 && imageUrl != null) {
Picasso.with(c).load(imageUrl).into(img);
//Picasso.with(c).setLoggingEnabled(true);
} else {
Picasso.with(c).load(R.drawable.ic_menu_gallery).into(img);
}
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.User.herb_recognition_test.searchActivity"
tools:showIn="@layout/activity_search">
<ListView
android:id="@+id/HerbListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
model.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
android:layout_height="150dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/thumbnail"
android:padding="10dp"
android:scaleType="fitXY"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/herbtitle"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:layout_alignParentLeft="true"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
创建列表视图时生成的错误logcat
D/EGL_emulation: eglMakeCurrent: 0x9cad3880: ver 3 1 (tinfo 0x9ca5e890)
D/EGL_emulation: eglMakeCurrent: 0x9cad3880: ver 3 1 (tinfo 0x9ca5e890)
D/EGL_emulation: eglMakeCurrent: 0x9cad3880: ver 3 1 (tinfo 0x9ca5e890)
.searchActivity: +741ms
W/SurfaceFlinger: couldn't log to binary event log: overflow.
W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
D/skia: ---- fAsset->read(8192) returned 0
D/skia: --- SkAndroidCodec::NewFromStream returned null
答案 0 :(得分:0)
首先尝试使用它来输入一行而不调用另一个过程。然后检查它是否有效。
乍一看它看起来应该有效,但你有很多条件,也许其中一个是错的,所以它不允许使用毕加索。
试试这一行。 Picasso.with(YOUR_CONTEXT).load(YOUR_URL).fit()代入(YOUR_IMAGEVIEW);
BTW,你错过了fit()函数。
答案 1 :(得分:0)
似乎由于服务器的IP地址盯着图像而出现此问题。我应该将IP地址放在数字(例如192.168.x.x)而不是localhost。