AutoCompleteTextView search;
Button goSearch_Button;
private ArrayAdapter<String> autoCompAdapter;
Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
autoCompAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
autoCompAdapter.setNotifyOnChange(true);
search= (AutoCompleteTextView)findViewById(R.id.serch);
search.setThreshold(2);
search.addTextChangedListener(textChecker);
search.setAdapter(autoCompAdapter);
goSearch_Button = (Button)findViewById(R.id.searchbutton);
goSearch_Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(this, Search_list.class);
intent.putExtra("name",search.getText().toString());
startActivity(intent);
}
});
}
final TextWatcher textChecker = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Log.d("onTextChanged","1");
//autoCompAdapter.clear();
HttpGetTask httpGetTask = new HttpGetTask();
httpGetTask.execute();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
private void callPHP(){
Log.d("callPHP","callPHP");
String result ="";
InputStream is =null;
try{
ArrayList<NameValuePair> nameValPairs = new ArrayList<NameValuePair>();
nameValPairs.add(new BasicNameValuePair("hotel_name", search_hotel.getText().toString()));
String hotel_name=URLEncoder.encode(search_hotel.getText().toString(),"UTF-8");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("
http://test.com/text/json.php"
+"?flag=6"+"&"+"hotel_name="+hotel_name);
httppost.setEntity(new UrlEncodedFormEntity(nameValPairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
Log.e("Log_TAG","Error in http connection " + e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),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 in http connection " + e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.d("json_data",json_data.getString("name"));
autoCompAdapter.add(json_data.getString("name"));
}
}catch(JSONException e){
Log.e("Log_TAG","Error parsing data"+e.toString());
}
}
public class HttpGetTask extends AsyncTask<Void, Void, String> {
String url = "";
protected String doInBackground(Void... Void) {
String result = "";
callPHP();
return result;
}
protected void onPreExecute(){
자동완성 목록이 뜨질않네요 ...불러오는건 되는데 목록이 뜨질 않아서, 뭐가 문제인지 잘모르겠네요.