|
|
|
[Android] ListView มีปัญหาค่ะ เข้าหน้าที่แสดง Listview ไม่ได้เลย ไม่รู้ว่าผิดตรงไหน ช่วยดูด้วยนะคะ |
|
|
|
|
|
|
|
Code (Java)
package com.BT.buytrash;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
public class price extends Activity{
private EditText search;
private ImageButton type_iron;
private ImageButton type_plastic;
private ImageButton type_paper;
private ImageButton type_kew;
private ImageButton type_loha;
private ImageButton type_fifa;
private ImageButton type_other;
private ListView listview_product;
private TextView test;
//private String[][] data_listview;
private ArrayList<String> data_listview;
String url = "http://10.0.2.2/BuyTrash/price_product.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
//----------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.price);
search = (EditText) findViewById(R.id.EditText_search);
type_iron = (ImageButton) findViewById(R.id.Button_type_iron);
type_plastic = (ImageButton) findViewById(R.id.Button_type_plastic);
type_paper = (ImageButton) findViewById(R.id.Button_type_paper);
type_kew = (ImageButton) findViewById(R.id.Button_type_kew);
type_loha = (ImageButton) findViewById(R.id.Button_type_loha);
type_fifa = (ImageButton) findViewById(R.id.Button_type_fifa);
type_other = (ImageButton) findViewById(R.id.Button_type_other);
listview_product = (ListView) findViewById(R.id.listview_price);
params.add(new BasicNameValuePair("keyword", search.getText().toString()));
test = (TextView) findViewById(R.id.textViewtest);
ArrayList<HashMap<String, String>> re = new ArrayList<HashMap<String, String>>(); ;
re = getHttpPost(url,params);
String a = "id = " + re.get(0).get("p_id").toString() +
"p_name = " + re.get(0).get("p_name").toString() +
"p_type = " + re.get(0).get("p_type").toString() +
"p_price1 =" + re.get(0).get("p_price1").toString() +
"p_price2 = " + re.get(0).get("p_price2").toString() +
"p_price3 = " + re.get(0).get("p_price3").toString() +
"p_sale = " + re.get(0).get("p_sale").toString()+"size = "+re.size();
test.setText(a);
//------------------------------------------------------------
// Adapter
listview_product.setAdapter(new MyAdapters());
data_listview = new ArrayList<String>();
data_listview.add("ราคาซื้อและราคาขายสินค้า"); // 0
data_listview.add("ซื้อสินค้า"); // 1
data_listview.add("ข้อมูลลูกค้า");
/*
data_listview = new String[re.size()][7];
for(int i=0 ; i< re.size() ; i++){
data_listview[i][0] = re.get(i).get("p_id").toString();
data_listview[i][1] = re.get(i).get("p_name").toString();
data_listview[i][2] = re.get(i).get("p_type").toString();
data_listview[i][3] = re.get(i).get("p_price1").toString();
data_listview[i][4] = re.get(i).get("p_price2").toString();
data_listview[i][5] = re.get(i).get("p_price3").toString();
data_listview[i][6] = re.get(i).get("p_sale").toString();
}
*/
}
//-------------------------------------------------------------------------------------
private class MyAdapters extends BaseAdapter{
private Holder holder;
public int getCount() {
return data_listview.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
if( view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_price_item, null);
holder = new Holder();
holder.p_name = (TextView) view.findViewById(R.id.product_name);
holder.p_price1 = (TextView) view.findViewById(R.id.price1);
holder.p_price2 = (TextView) view.findViewById(R.id.price2);
holder.p_price3 = (TextView) view.findViewById(R.id.price3);
holder.p_sale = (TextView) view.findViewById(R.id.price_sale);
view.setTag(holder);
}else{
holder = (Holder) view.getTag();
}
holder.p_name.setText(data_listview.get(position));
holder.p_price1.setText(data_listview.get(position));
holder.p_price2.setText(data_listview.get(position));
holder.p_price3.setText(data_listview.get(position));
holder.p_sale.setText(data_listview.get(position));
return view;
}
private class Holder{ //จะบอกว่า view แต่ละรายการมีอะไรบ้าง
public TextView p_name;
public TextView p_price1;
public TextView p_price2;
public TextView p_price3;
public TextView p_sale;
}
}
//----------------------------------------------------------------------------------------
public ArrayList<HashMap<String, String>> getHttpPost(String url, List<NameValuePair> params) {
// public ArrayList<String> getHttpPost(String url, List<NameValuePair> params) {
ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
//String[][] re ;
InputStream is = null;
String result = "";
ArrayList<String> name = new ArrayList<String>();
// -------------------------------connect----------------------------------------------------
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// -------------------------------set UTF8 --------------------------------------------------
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// -------------------------------to String --------------------------------------------------
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-11"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// -------------------------------JSON to JAVA --------------------------------------------------
try {
JSONArray jArray = new JSONArray(result);
//re = new String[jArray.length()][7];
HashMap<String, String> map;
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
map = new HashMap<String, String>();
map.put("p_id",json_data.getString("p_id") );
map.put("p_name", json_data.getString("p_name") );
map.put("p_type", json_data.getString("p_type") );
map.put("p_price1", json_data.getString("p_price1") );
map.put("p_price2", json_data.getString("p_price2") );
map.put("p_price3", json_data.getString("p_price3") );
map.put("p_sale", json_data.getString("p_sale") );
myArrList.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return myArrList;
// return name;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
Tag : Mobile, Android, JAVA
|
|
|
|
|
|
Date :
2012-11-21 21:47:29 |
By :
amiamika |
View :
1293 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แสดง Error หรือว่ายังไงครับ
|
|
|
|
|
Date :
2012-11-22 06:24:28 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แล้วการใช้ debug ดูนี่ทำยังไงคะ เพิ่งศึกษาค่ะ ไม่ค่อยเข้าใจเลยค่ะ
|
|
|
|
|
Date :
2012-11-23 12:49:39 |
By :
amiamika |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อ๋อ ได้แล้วค่ะ ขอบคุณมากนะคะ ^^
|
|
|
|
|
Date :
2012-11-23 14:38:46 |
By :
amiamika |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|