|
|
|
Android - แสดงข้อมูลบนลิสได้แล้ว แต่พอคลิกมันไม่ยอมแสดง Dialog Popup แล้วก็ค้างไปเลย |
|
|
|
|
|
|
|
เป็นโค๊ดจากเว็บthaicreateเนี้ยหละครับแสดงข้อมูลตอนเซิทได้แล้วแต่พอกดที่ลิสมันไม่ยอมแสดง
Dialog Popupแต่ค้างแล้วปิิดโปรแกรมเองเลยครับไม่ทราบว่าโค๊ดผิดตรงไหน
Code (Android-Java)
MainActivity.java
package com.example.listviewjson;
import java.io.BufferedReader;
import java.io.IOException;
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.StatusLine;
import org.apache.http.client.ClientProtocolException;
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.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final Button btn1 = (Button) findViewById(R.id.button1);
// Perform action on click
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SearchData();
}
});
}
public void SearchData()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// editText1
final EditText inputText = (EditText)findViewById(R.id.editText1);
/**
* [{"CustomerID":"C001","Name":"Win Weerachai","Email":"[email protected]" ,"CountryCode":"TH","Budget":"1000000","Used":"600000"},
* {"CustomerID":"C002","Name":"John Smith","Email":"[email protected]" ,"CountryCode":"EN","Budget":"2000000","Used":"800000"},
* {"CustomerID":"C003","Name":"Jame Born","Email":"[email protected]" ,"CountryCode":"US","Budget":"3000000","Used":"600000"},
* {"CustomerID":"C004","Name":"Chalee Angel","Email":"[email protected]" ,"CountryCode":"US","Budget":"4000000","Used":"100000"}]
*/
String url = "http://www.likehistory.com/getJSON.php";
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtKeyword", inputText.getText().toString()));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("CustomerID", c.getString("CustomerID"));
map.put("Name", c.getString("Name"));
map.put("Email", c.getString("Email"));
map.put("CountryCode", c.getString("CountryCode"));
map.put("Budget", c.getString("Budget"));
map.put("Used", c.getString("Used"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column,
new String[] {"CustomerID", "Name", "Email"}, new int[] {R.id.ColCustomerID, R.id.ColName, R.id.ColEmail});
lisView1.setAdapter(sAdap);
final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
// OnClick Item
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int position, long mylng) {
String strCustomerID = MyArrList.get(position).get("CustomerID")
.toString();
String sName = MyArrList.get(position).get("Name")
.toString();
String strEmail = MyArrList.get(position).get("Email")
.toString();
String strCounryCode = MyArrList.get(position).get("CounryCode")
.toString();
String strBudget = MyArrList.get(position).get("Budget")
.toString();
String strUsed = MyArrList.get(position).get("Used")
.toString();
viewDetail.setIcon(android.R.drawable.btn_star_big_on);
viewDetail.setTitle("Member Detail");
viewDetail.setMessage("MemberID : " + strCustomerID + "\n"
+ "Name : " + sName + "\n"
+ "strEmail : " + strEmail + "\n"
+ "strCounryCode : " + strCounryCode + "\n"
+ "strBudget : " + strBudget + "\n"
+ "Used : " + strUsed);
viewDetail.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
viewDetail.show();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Tag : Mobile, Android, Web Service
|
|
|
|
|
|
Date :
2013-10-31 02:16:41 |
By :
opassawad |
View :
1071 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันจะต้องมี Error ครับ เอา LogCat มาดู
|
|
|
|
|
Date :
2013-10-31 06:20:02 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เนี้ยครับขอโทดที่ตอบช้านะครับพอดีติดงานไปต่างจังหวัดครับ
|
|
|
|
|
Date :
2013-11-03 19:41:40 |
By :
opassawad |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จาก Error เหมือนมีค่าว่างครับ อาจจะต้องใช้การ Debug ดูครับ
|
|
|
|
|
Date :
2013-11-03 20:35:26 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับเดียวทดลองทำดูก่อน ^^
|
|
|
|
|
Date :
2013-11-03 21:04:35 |
By :
opassawad |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เดียวก็แก้ไขก่อนครับ
|
|
|
|
|
Date :
2013-11-03 22:11:28 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|