@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lihat_barang); setOtherViews(); barangall = JSON_Response.getAllItems(); }
private void setListCategory() { // JSONArray daftarkategori = kategori;//JSON_Response.getJSONResponse(GET_ALL_KATEGORI); JSONArray daftarkategori = JSON_Response.getAllCategories(); String[] items = getKategoriBarangNames(daftarkategori); Spinner spinner = (Spinner) findViewById(R.id.choose_kategori); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub currentCategoryId = idcateglist.get(position); setContentItemsBarang(JSON_Response.getItemsByCategory(currentCategoryId)); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); }
// set isi tampilan barang di kategori yang sedang dipilih private void setContentItemsBarang(JSONArray jsonArray) { LinearLayout ll = (LinearLayout) findViewById(R.id.wrapperbarang); ll.removeAllViewsInLayout(); try { for (int i = 0; i < jsonArray.length(); i++) { final JSONObject jsonObject = jsonArray.getJSONObject(i); LinearLayout lytContainer = new LinearLayout(this); lytContainer.setOrientation(LinearLayout.VERTICAL); if (i % 2 == 1) { lytContainer.setBackgroundColor(Color.WHITE); } else { lytContainer.setBackgroundColor(Color.DKGRAY); } Drawable image = JSON_Response.ImageOperations(this, jsonObject.getString("path_gambar"), "image.jpg"); ImageView iv = new ImageView(this); iv.setAdjustViewBounds(true); iv.setMaxHeight(50); iv.setMaxWidth(50); iv.setImageDrawable(image); iv.setPadding(0, 10, 0, 0); lytContainer.addView(iv); TextView t = new TextView(this); t.setText(jsonObject.getString("nama")); t.setGravity(Gravity.CENTER); t.setTypeface(null, Typeface.BOLD); if (i % 2 == 1) { t.setTextColor(Color.BLACK); } else { t.setTextColor(Color.WHITE); } lytContainer.addView(t); TextView t2 = new TextView(this); t2.setGravity(Gravity.CENTER); t2.setText("Jumlah produk yang tersedia : " + jsonObject.getString("jumlah")); if (i % 2 == 1) { t2.setTextColor(Color.BLACK); } else { t2.setTextColor(Color.WHITE); } t2.setPadding(0, 0, 0, 10); lytContainer.addView(t2); lytContainer.setOnClickListener( new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub try { System.gc(); Intent intent = new Intent(LihatBarangActivity.this, LihatItemBarangActivity.class); intent.putExtra("id", jsonObject.getInt("id")); intent.putExtra("kategori_id", jsonObject.getInt("kategori_id")); intent.putExtra("nama", jsonObject.getString("nama")); intent.putExtra("deskripsi", jsonObject.getString("deskripsi")); intent.putExtra("jumlah", jsonObject.getString("jumlah")); intent.putExtra("harga", jsonObject.getString("harga_satuan")); intent.putExtra("path_gambar", jsonObject.getString("path_gambar")); startActivity(intent); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); ll.addView(lytContainer); } } catch (Exception e) { Log.d("ancret", e.toString()); e.printStackTrace(); } }