Xenix Putra Sasongko Xenix Putra Sasongko - 1 month ago 15
Android Question

CustomListview Volley with Image doesn't works in real device

Someone, help me with this problem. i have stuck in 3 days because this problem -_-!

i can make listview with image and text using volley library, its works in emulator (i use genymotion emulator) the image and text show. but when i run it in my device (android Jelly 4.3.0) the listview is empty. the layer is blank (empty). i don't know why.

heres my piece code

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);
}


im sure the url is fine, and image return is http://192.168.117.1:808/Koen_CI/gambarbaju/batik.jpg.

the problem is, why in real device the listview is not show, but in emulator the listview is show..

sorry for my bad english, but thanks anyway.. :)

heres my 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;
}

Answer

Add this code to your onCreate or init method.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

If this doesn't work please post logcat showing warning or error.