我正在尝试使用volley.jar库在listview中将图像和对应于每个图像的字符串值从服务器加载到我的Android应用程序。我无法使其正常工作,无法找到我的内容做错了。我很感激,如果有人能告诉我我做错了什么。
我有一个Movie类,我已经声明了获取和设置变量标题和thumbnailUrl
的方法这是我的CustomListAdapater类
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
return convertView;
}
}
这在我的MainActivity类
中public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "---------------------my api url-------------";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
try {
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
}catch (Exception e) {
}
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.e(TAG+" response", response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("description"));
String logo = obj.getString("logo");
String src ="------------Image directory path-------------"+logo;
movie.setThumbnailUrl(src);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
}
这是我在logcat中获得的
D/OpenGLRenderer﹕ Flushing caches (mode 0)
D/AbsListView﹕ Get MotionRecognitionManager
D/TextLayoutCache﹕ Using debug level: 0 - Debug Enabled: 0
D/libEGL﹕ loaded /system/lib/egl/libGLES_android.so
D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888.
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x51edf000 size:307200 offset:0 fd:64
D/OpenGLRenderer﹕ Enabling debug mode 0
I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888.
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5203a000 size:8990720 offset:7454720 fd:67
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5293d000 size:614400 offset:307200 fd:70
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x529d3000 size:20766720 offset:19230720 fd:73
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x53ea1000 size:921600 offset:614400 fd:76
D/Volley﹕ [1] 2.onErrorResponse: MainActivity
D/Volley﹕ [1] Request.finish: 3487 ms: [ ] http:----------------------------myApi.php 0xcb62917f NORMAL 1
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x51edf000 size:307200 offset:0
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x5293d000 size:614400 offset:307200
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x53ea1000 size:921600 offset:614400
D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... !
答案 0 :(得分:2)
尝试使用Picasso强大的Android图像下载和缓存库,您只需要包含jar文件并编写一行代码以获取更多详细信息,请点击链接: