有人,帮我解决这个问题。因为这个问题-_-,我已经坚持了3天。
我可以使用volley库制作带有图像和文本的列表视图,它可以在模拟器中工作(我使用genymotion模拟器)的图像和文本显示。但是当我在我的设备中运行它(android Jelly 4.3.0)时,listview是空的。图层为空(空)。我不知道为什么。
继承我的作品代码
public class DaftarBarang_Layout extends Activity{
private List<Produk> produkList = new ArrayList<Produk>();
private ListView listView;
private CustomListAdapter adapter;
private ProgressDialog pDialog;
private ServerRequest serverRequest;
JSONArray member = null;
private static final String url = "http://192.168.117.1:808/Koen_CI/index.php/daftar_barang_control";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.daftarbarang_layout);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, produkList);
listView.setAdapter(adapter);
btnBack = (Button)findViewById(R.id.btnBackDaftarBarang);
setBehavior();
// Creating volley request obj
JsonArrayRequest produkReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Produk produk = new Produk();
produk.setNamaProduk(obj.getString("nama_produk"));
produk.setHargaProduk(obj.getString("harga_produk"));
produk.setFotoProduk(obj.getString("foto_produk"));
Log.d("TAG", "TAG : " + produk.getNamaProduk());
// adding movie to movies array
produkList.add(produk);
} 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(produkReq);
}
我确定网址很好,图片返回为http://192.168.117.1:808/Koen_CI/gambarbaju/batik.jpg。
问题是,为什么在真实设备中listview不显示,但在模拟器中列表视图显示..
抱歉我的英语不好,但不管怎样,谢谢.. :)继承我的CustomListAdapter:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Produk> produkItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Produk> produkItems) {
this.activity = activity;
this.produkItems = produkItems;
}
@Override
public int getCount() {
return produkItems.size();
}
@Override
public Object getItem(int location) {
return produkItems.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);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
// getting movie data for the row
Produk p = produkItems.get(position);
// thumbnail image
thumbNail.setImageUrl(p.getFotoProduk(), imageLoader);
// title
title.setText(p.getNamaProduk());
// rating
rating.setText("Harga: " + p.getHargaProduk());
return convertView;
}
答案 0 :(得分:1)
将此代码添加到onCreate或init方法。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
如果这不起作用,请发布显示警告或错误的logcat。