我有问题是我无法在listview中显示图像,我使用Android JSON Parsing Using Volley来显示带有图像的listview。项目的信息成功显示,但图像不成功。我卡在那里仍然找不到解决方案。
这是itemRequest类
public class itemRequest extends StringRequest{
private static final String ITEM_REQUEST_URL = "http://XXXXXXXXXXXXX.php";
private Map<String, String> params;
public itemRequest(String userID, String sessionID, Response.Listener<String> listener) {
super(Request.Method.POST, ITEM_REQUEST_URL, listener,null);
String id="6";
String isOpen="y";
params = new HashMap<>();
params.put("userID", userID);
params.put("sessionID", sessionID);
params.put("isOpen", isOpen);
params.put("id", id);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
这是项目类
public class items extends BaseActivity {
private ProgressDialog pDialog;
private ArrayList<RowItem> rowItems = new ArrayList<RowItem>();
private ListView mylistview;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean error = jsonResponse.getBoolean("error");
System.out.println(jsonResponse.toString());
if (error){
String errorMessage = jsonResponse.getString("message");
AlertDialog.Builder builder = new AlertDialog.Builder(items.this);
builder.setMessage(errorMessage)
.setNegativeButton("Retry", null)
.create()
.show();
}else {
JSONArray itemSet = jsonResponse.getJSONArray("itemSet");
for (int i = 0; i < itemSet.length(); i++){
JSONObject item = itemSet.getJSONObject(i);
String title = item.getString("title");
String seller = item.getString("nickName");
String price = item.getString("price");
String itemID = item.getString("itemID");
String Path;
JSONArray pic = item.getJSONArray("picPath");
if (pic.length()==0)
{
Path="uploads/default.jpg";
}else {
Path = pic.getString(0);
}
RowItem row=new RowItem(title,seller,price,Path,itemID);
rowItems.add(row);
}
mylistview = (ListView) findViewById(R.id.list);
CustomListAdapter adapter = new CustomListAdapter(items.this, rowItems);
System.out.println(rowItems.isEmpty());
mylistview.setAdapter(adapter);
}
}catch (JSONException e){
e.printStackTrace();
}
}
};
Intent intent = getIntent();
ArrayList<String> list =intent.getStringArrayListExtra("userInfo");
System.out.println(list.toString());
itemRequest itemRequest = new itemRequest(list.get(0),list.get(1),responseListener);
RequestQueue queue = Volley.newRequestQueue(items.this);
queue.add(itemRequest);
pDialog.dismiss();
}
}
这是CustomListAdpater
public class CustomListAdapter extends BaseAdapter {
private Context context;
private List<RowItem> rowItems;
private LayoutInflater inflater = null;
public static final String IMAGE_BASEURL = "http://XXXXXXXXXXX/";
private LruCache<Integer, Bitmap> imageCache;
private RequestQueue queue;
public CustomListAdapter(Context context, List<RowItem> rowItems){
this.context=context;
this.rowItems=rowItems;
inflater = LayoutInflater.from(context);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
imageCache = new LruCache<>(cacheSize);
queue = Volley.newRequestQueue(context);
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
private class ViewHolder{
ImageView imageView;
TextView tv_title;
TextView tv_seller;
TextView tv_price;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final RowItem row_pos = rowItems.get(position);
final ViewHolder holder;
System.out.print("=================================================================");
if (convertView==null){
convertView = mInflater.inflate(R.layout.custom_layout, null);
holder = new ViewHolder();
holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
holder.tv_seller = (TextView) convertView.findViewById(R.id.tv_seller);
holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_title.setText(row_pos.getTv_title().toString());
holder.tv_seller.setText(row_pos.getTv_seller().toString());
holder.tv_price.setText(row_pos.getTv_price().toString());
Bitmap bitmap = imageCache.get(Integer.parseInt(row_pos.getItemID()));
holder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
if (bitmap != null){
holder.imageView.setImageBitmap(bitmap);
System.out.print("bitmap");
}else {
String imageURL = IMAGE_BASEURL + row_pos.getPath();
ImageRequest request = new ImageRequest(imageURL, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
holder.imageView.setImageBitmap(response);
imageCache.put(Integer.parseInt(row_pos.getItemID()), response);
System.out.print(row_pos.getItemID()+"this is image");
}
}, 90, 90,
Bitmap.Config.ARGB_8888,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});
queue.add(request);
}
return convertView;
}
}
这是Rowitem类
public class RowItem {
private String tv_title;
private String tv_seller;
private String tv_price;
private String image_view;
private Bitmap bitmap;
private String Path;
private String itemID;
public RowItem(String tv_title, String tv_seller, String tv_price, String image_view, String itemID){
this.tv_title = tv_title;
this.tv_seller = tv_seller;
this.tv_price = tv_price;
this.image_view = image_view;
this.Path = image_view;
this.itemID = itemID;
}
public String getTv_title() {
return tv_title;
}
public void setTv_title(String tv_title) {
this.tv_title = tv_title;
}
public String getTv_seller() {
return tv_seller;
}
public void setTv_seller(String tv_seller) {
this.tv_seller = tv_seller;
}
public String getTv_price() {
return tv_price;
}
public void setTv_price(String tv_price) {
this.tv_price = tv_price;
}
public String getImage_view() {
return image_view;
}
public void setImage_view(String image_view) {
this.image_view = image_view;
}
public Bitmap getBitmap(){
return bitmap;
}
public void setBitmap(Bitmap bitmap){
this.bitmap = bitmap;
}
public String getPath(){
return Path;
}
public void setPath(String Path){
this.Path = Path;
}
public String getItemID(){
return itemID;
}
public void setItemID(){
this.itemID = itemID;
}
}
只有图像无法显示其他图像正常。