Android 动态地将数据从服务器添加到自动完成文本视图
我想在我的android应用程序中实现google类型搜索,为此我使用了autocomplete textview,当我一个接一个地键入字符时,它非常有效,但当我同时键入多个字符时,问题就会出现,我的应用程序会显示一个对话框并强制关闭。 提前谢谢Android 动态地将数据从服务器添加到自动完成文本视图,android,Android,我想在我的android应用程序中实现google类型搜索,为此我使用了autocomplete textview,当我一个接一个地键入字符时,它非常有效,但当我同时键入多个字符时,问题就会出现,我的应用程序会显示一个对话框并强制关闭。 提前谢谢 public class Activity_ListItem extends Activity { public Context mContext; // views declaration public AutoCompleteTextView tx
public class Activity_ListItem extends Activity {
public Context mContext;
// views declaration
public AutoCompleteTextView txtAutoComplete;
public ListView lvItems;
// arrayList for Adaptor
ArrayList<String> listItems;
// getting input from AutocompleteTxt
String strItemName;
// making Adaptor for autocompleteTextView
ArrayAdapter<String> adaptorAutoComplete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// for showing full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_listitem);
mContext = this;
listItems = new ArrayList<String>();
// Declaring and getting all views objects
Button btnShare = (Button) findViewById(R.id.ListItem_btnShare);
Button btnSort = (Button) findViewById(R.id.ListItem_btnSort);
lvItems = (ListView) findViewById(R.id.ListItem_lvItem);
txtAutoComplete = (AutoCompleteTextView) findViewById(R.id.ListItem_autoComplete);
// adding listeners to button
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
btnSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
// setting adaptor to autoComplete TextView
// adaptorAutoComplete = new ArrayAdapter<String>(mContext,android.R.layout.simple_dropdown_item_1line, listItems);
txtAutoComplete.setThreshold(1);
// adding Listener to Auto CompleteText View
txtAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence charEnter, int start, int before,
int count) {
// TODO Auto-generated method stub
strItemName = charEnter.toString();
new FetchItemListFromServer().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
/* strItemName = txtAutoComplete.getText().toString();
new FetchItemListFromServer().execute();*/
// adaptorAutoComplete.notifyDataSetChanged();
}
});
} // on create ends
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__list_item, menu);
return true;
} // method ends
public SoapObject getDataFromServer(String product_name, String store_id) {
// all variables for Soap
String SOAP_ACTION = "http://www.SupermarketAPI.com/COMMERCIAL_SearchForItem";
String NAMESPACE = "http://www.SupermarketAPI.com";
String METHOD_NAME = "COMMERCIAL_SearchForItem";
String URL = "http://www.supermarketapi.com/api.asmx?WSDL";
SoapObject objSoap = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("APIKEY", "8b0e05b569");
request.addProperty("ItemName",strItemName);
request.addProperty("StoreID", "9829ae4237");
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// this is the actual part that will call the webservice
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
objSoap = (SoapObject) envelope.getResponse();
if (objSoap != null) {
String strData = objSoap.toString();
System.out.println("envelop.getResponse//////"
+ strData.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return objSoap;
} // method ends
public class FetchItemListFromServer extends AsyncTask<Void, Void, Void> {
SoapObject objSoap = null;
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
listItems.clear();
objSoap = getDataFromServer(strItemName, "");
if (objSoap != null) {
System.out.println("getPropertyCountinevents//////////"
+ objSoap.getPropertyCount());
for (int i = 0; i < objSoap.getPropertyCount(); i++) {
Object obj = objSoap.getProperty(i);
if (obj instanceof SoapObject) {
SoapObject objNew = (SoapObject) obj;
listItems
.add(objNew.getProperty("Itemname").toString());
}
}
}
System.out.println("ArrayList size//////////"
+ listItems.size());
runOnUiThread(new Runnable(){
public void run(){
adaptorAutoComplete = new ArrayAdapter<String>(mContext,android.R.layout.simple_dropdown_item_1line, listItems);
txtAutoComplete.setAdapter(adaptorAutoComplete);
adaptorAutoComplete.notifyDataSetChanged();
}
});
return null;
} // method ends
} // asyntask class ends
} // final class ends
公共类活动\u ListItem扩展活动{
公共语境;
//意见声明
公共自动完成文本视图txtAutoComplete;
公共列表项;
//适配器的arrayList
ArrayList列表项;
//从AutoCompleteText获取输入
字符串strItemName;
//为autocompleteTextView制作适配器
阵列适配器自动完成;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//用于全屏显示
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity\u列表项);
mContext=这个;
listItems=new ArrayList();
//声明和获取所有视图对象
按钮btnShare=(按钮)findViewById(R.id.ListItem_btnShare);
按钮b传感器=(按钮)findViewById(R.id.ListItem\u b传感器);
lvItems=(ListView)findViewById(R.id.ListItem\u lvItem);
txtAutoComplete=(AutoCompleteTextView)findViewById(R.id.ListItem\u autoComplete);
//向按钮添加侦听器
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
}
});
btSensor.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
}
});
//将适配器设置为自动完成文本视图
//AdapterAutoComplete=new ArrayAdapter(mContext,android.R.layout.simple\u下拉列表\u项目线,列表项目);
txtAutoComplete.setThreshold(1);
//将侦听器添加到自动完成文本视图
txtAutoComplete.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列charEnter、int start、int before、,
整数计数){
//TODO自动生成的方法存根
strItemName=charEnter.toString();
新建FetchItemListFromServer().execute();
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
/*strItemName=txtAutoComplete.getText().toString();
新建FetchItemListFromServer().execute()*/
//AdapterAutoComplete.notifyDataSetChanged();
}
});
}//关于创建端点
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u list\u项,菜单);
返回true;
}//方法结束
公共SoapObject getDataFromServer(字符串产品名称、字符串存储id){
//Soap的所有变量
字符串SOAP_ACTION=”http://www.SupermarketAPI.com/COMMERCIAL_SearchForItem";
字符串命名空间=”http://www.SupermarketAPI.com";
字符串方法\u NAME=“COMMERCIAL\u SearchForItem”;
字符串URL=”http://www.supermarketapi.com/api.asmx?WSDL";
soapobjectobjsoap=null;
SoapObject请求=新的SoapObject(名称空间、方法名称);
//使用此选项可以添加参数
请求添加属性(“APIKEY”、“8b0e05b569”);
request.addProperty(“ItemName”,strItemName);
请求。addProperty(“StoreID”,“9829ae4237”);
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第11版);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
试一试{
调用(SOAP_操作,信封);
//从信封正文获取SoapResult。
objSoap=(SoapObject)envelope.getResponse();
if(objSoap!=null){
字符串strData=objSoap.toString();
System.out.println(“envelope.getResponse//”
+strData.toString());
}
}捕获(例外e){
e、 printStackTrace();
}
返回objSoap;
}//方法结束
公共类FetchItemListFromServer扩展了异步任务{
soapobjectobjsoap=null;
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
listItems.clear();
objSoap=getDataFromServer(strItemName,“”);
if(objSoap!=null){
System.out.println(“GetPropertyCountineEvents//”
+getPropertyCount());
for(int i=0;i
<
public class TestActivity extends Activity {
public Context mContext;
// views declaration
public AutoCompleteTextView txtAutoComplete;
public ListView lvItems;
// arrayList for Adaptor
ArrayList<String> listItems;
// getting input from AutocompleteTxt
String strItemName;
// making Adaptor for autocompleteTextView
ArrayAdapter<String> adaptorAutoComplete;
private static final int ADDRESS_TRESHOLD = 2;
private Filter filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// for showing full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_listitem);
mContext = this;
listItems = new ArrayList<String>();
// Declaring and getting all views objects
Button btnShare = (Button) findViewById(R.id.ListItem_btnShare);
Button btnSort = (Button) findViewById(R.id.ListItem_btnSort);
lvItems = (ListView) findViewById(R.id.ListItem_lvItem);
txtAutoComplete = (AutoCompleteTextView) findViewById(R.id.ListItem_autoComplete);
// adding listeners to button
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
btnSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
String[] from = { "name" };
int[] to = { android.R.id.text1 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this,
android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
a.setStringConversionColumn(1);
FilterQueryProvider provider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// run in the background thread
if (constraint == null) {
return null;
}
String[] columnNames = { Columns._ID, "name" };
MatrixCursor c = new MatrixCursor(columnNames);
try {
// total code for implementing my way of auto complte
String SOAP_ACTION = "your action";
String NAMESPACE = "your name space";
String METHOD_NAME = "your method name";
String URL = "your Url";
SoapObject objSoap = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("KEY", yourkey);
request.addProperty("Key", constraint);
request.addProperty("Key", Id);
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
objSoap = (SoapObject) envelope.getResponse();
if (objSoap != null) {
String strData = objSoap.toString();
}
if (objSoap != null) {
System.out.println("getPropertyCountinevents//////////"
+ objSoap.getPropertyCount());
for (int i = 0; i < objSoap.getPropertyCount(); i++) {
Object obj = objSoap.getProperty(i);
if (obj instanceof SoapObject) {
SoapObject objNew = (SoapObject) obj;
c.newRow()
.add(i)
.add(objNew.getProperty("Itemname")
.toString());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
};
a.setFilterQueryProvider(provider);
txtAutoComplete.setAdapter(a);
} // on create ends
} // final class ends
public class GetLocations extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = manager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled) {
/*
* Criteria criteria = new Criteria(); String bestProvider =
* manager.getBestProvider(criteria, false); Location location =
* manager.getLastKnownLocation(bestProvider); double lat
* =location.getLatitude(); double longi
* =location.getLongitude();
* System.out.println("getting location continous ////// Lattti "
* +location.getLatitude() );
* System.out.println("getting location continous ////// LONGITU "
* + location.getLongitude());
*/
manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 3000, 0, mylistener);
} else {
Toast.makeText(MyService.this, "Please oN Gps ",
Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// getting current lattitude and longitude
return null;
}
}