2013-11-15 4 views
3

Я хочу реализовать поиск по типу google в моем приложении для Android, для этого я использовал автозаполнение textview, он работает, когда я набираю символ один за другим, но проблема возникает, когда я печатаю одновременно с несколькими символами, мое приложение показывает диалог и принудительно закрывается. Заранее спасибоДинамическое добавление данных с сервера на автоматическое завершение Textview

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 
+1

увидеть мой ответ в этой теме http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving -chara – pskink

+0

@pskink: я не понимаю, что такое SimpleCursorAdaptor. –

+0

Его списокAdapter – pskink

ответ

1

Здесь работает решение я получил предложил мой друг pskink

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 
1

Вы можете использовать asynytask для него, как этот

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; 
     } 
    } 
0

Вы можете установить глобальное логическое в методе preExecute() для AsyncTask. И отмените предыдущую AsynTask, как только булев установлен (то есть, если уже запущена программа AsyncTask). Это оно.

Пожалуйста, обратитесь http://developer.android.com/reference/android/os/AsyncTask.html для использования AsyncTask

Смежные вопросы