- Beranda
- Komunitas
- Tech
- Programmer Forum
[ASK][JAVA] Minta Saran / Pencerahan / Bantuan / seikhlasnya untuk coding


TS
iinlovenie
[ASK][JAVA] Minta Saran / Pencerahan / Bantuan / seikhlasnya untuk coding
Basa-Basi : Sebelummnya ane minta maaf dulu sama agan" ,sesepuh,aganwati,master,guru,dll kalo ada yang salah dengan cara ane bertanya dan mohon jangan di 
Cerita : ane mau buat program yang mirip dengan spoiler program pertama dibawah,tapi isinya dari database mysql yang udah ane buat .
Masalah : di program pertama nilainya product 1 price 100 udah ditentukan di MainActivity filldata() ,gimana caranya supaya datanya diambil dari database program kedua ?
Sekian Dari Thread Ane Mohon Bimbinganya ya ......

Cerita : ane mau buat program yang mirip dengan spoiler program pertama dibawah,tapi isinya dari database mysql yang udah ane buat .
Masalah : di program pertama nilainya product 1 price 100 udah ditentukan di MainActivity filldata() ,gimana caranya supaya datanya diambil dari database program kedua ?
Spoiler for Program pertama:
Quote:
ListAdapter.Java
package com.amit.listview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.box);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
}
package com.amit.listview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.box);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
}
Quote:
MainActivity.java
package com.amit.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
ListAdapter boxAdapter;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillData();
boxAdapter = new ListAdapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
for (int i = 1; i <= 20; i++) {
products.add(new Product("Product " + i, i * 100,
R.drawable.ic_launcher, false));
}
}
public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Product p : boxAdapter.getBox()) {
if (p.box){
result += "\n" + p.name;
totalAmount+=p.price;
}
}
Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
}
}
package com.amit.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
ListAdapter boxAdapter;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillData();
boxAdapter = new ListAdapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
for (int i = 1; i <= 20; i++) {
products.add(new Product("Product " + i, i * 100,
R.drawable.ic_launcher, false));
}
}
public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Product p : boxAdapter.getBox()) {
if (p.box){
result += "\n" + p.name;
totalAmount+=p.price;
}
}
Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
}
}
Quote:
Product.java
package com.amit.listview;
public class Product {
String name;
int price;
int image;
boolean box;
Product(String _describe, int _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
package com.amit.listview;
public class Product {
String name;
int price;
int image;
boolean box;
Product(String _describe, int _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
Spoiler for Program kedua:
Quote:
JSONActivity.java
package com.agus.android.php;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class JSONActivity extends Activity {
private JSONObject jObject;
private String xResult = "";
// Seusuaikan url dengan nama domain yang anda gunakan
private String url = "http://10.0.2.2/db_makanan/daftarmakanan.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftarmakanan);
TextView txtResult = (TextView) findViewById(R.id.TextViewResult);
xResult = getRequest(url);
try {
parse(txtResult);
} catch (Exception e) {
e.printStackTrace();
}
}
private void parse(TextView txtResult) throws Exception {
jObject = new JSONObject(xResult);
JSONArray menuitemArray = jObject.getJSONArray("makanan");
String sret = "";
for (int i = 0; i < menuitemArray.length(); i++) {
sret += menuitemArray.getJSONObject(i).getString("nama_makanan")
.toString()
+ " : ";
System.out.println(menuitemArray.getJSONObject(i)
.getString("nama_makanan").toString());
System.out.println(menuitemArray.getJSONObject(i)
.getString("harga").toString());
sret += menuitemArray.getJSONObject(i).getString("harga")
.toString()
+ "\n";
}
txtResult.setText(sret);
}
/**
* Method untuk Mengirimkan data kes erver event by button login diklik
*
* @param view
*/
public String getRequest(String Url) {
String sret = "";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(Url);
try {
HttpResponse response = client.execute(request);
sret = request(response);
} catch (Exception ex) {
Toast.makeText(this, "Gagal " + sret, Toast.LENGTH_SHORT).show();
}
return sret;
}
/**
* Method untuk Menenrima data dari server
*
* @param response
* @return
*/
public static String request(HttpResponse response) {
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}
}
package com.agus.android.php;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class JSONActivity extends Activity {
private JSONObject jObject;
private String xResult = "";
// Seusuaikan url dengan nama domain yang anda gunakan
private String url = "http://10.0.2.2/db_makanan/daftarmakanan.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftarmakanan);
TextView txtResult = (TextView) findViewById(R.id.TextViewResult);
xResult = getRequest(url);
try {
parse(txtResult);
} catch (Exception e) {
e.printStackTrace();
}
}
private void parse(TextView txtResult) throws Exception {
jObject = new JSONObject(xResult);
JSONArray menuitemArray = jObject.getJSONArray("makanan");
String sret = "";
for (int i = 0; i < menuitemArray.length(); i++) {
sret += menuitemArray.getJSONObject(i).getString("nama_makanan")
.toString()
+ " : ";
System.out.println(menuitemArray.getJSONObject(i)
.getString("nama_makanan").toString());
System.out.println(menuitemArray.getJSONObject(i)
.getString("harga").toString());
sret += menuitemArray.getJSONObject(i).getString("harga")
.toString()
+ "\n";
}
txtResult.setText(sret);
}
/**
* Method untuk Mengirimkan data kes erver event by button login diklik
*
* @param view
*/
public String getRequest(String Url) {
String sret = "";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(Url);
try {
HttpResponse response = client.execute(request);
sret = request(response);
} catch (Exception ex) {
Toast.makeText(this, "Gagal " + sret, Toast.LENGTH_SHORT).show();
}
return sret;
}
/**
* Method untuk Menenrima data dari server
*
* @param response
* @return
*/
public static String request(HttpResponse response) {
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}
}
Sekian Dari Thread Ane Mohon Bimbinganya ya ......



nona212 memberi reputasi
1
1.3K
Kutip
3
Balasan


Komentar yang asik ya
Urutan
Terbaru
Terlama


Komentar yang asik ya
Komunitas Pilihan