Skip to content Skip to sidebar Skip to footer

Dynamically Adding Data From Server To Auto Complete Textview

i want to implement google type searching in my android application, for this i have used autocomplete textview,it is quite working when i type character one by one,but problem hap

Solution 1:

Here is working solution i got suggested by my friend pskink

publicclassTestActivityextendsActivity {
    public Context mContext;
    // views declarationpublic AutoCompleteTextView txtAutoComplete;
    public ListView lvItems;
    // arrayList for Adaptor
    ArrayList<String> listItems;
    // getting input from AutocompleteTxt
    String strItemName;
    // making Adaptor for autocompleteTextView
    ArrayAdapter<String> adaptorAutoComplete;
    privatestaticfinalintADDRESS_TRESHOLD=2;
    private Filter filter;

    @OverrideprotectedvoidonCreate(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 = newArrayList<String>();
        // Declaring and getting all views objectsButtonbtnShare= (Button) findViewById(R.id.ListItem_btnShare);
        ButtonbtnSort= (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(newView.OnClickListener() {

            @OverridepublicvoidonClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });
        btnSort.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });

        String[] from = { "name" };
        int[] to = { android.R.id.text1 };
        SimpleCursorAdaptera=newSimpleCursorAdapter(this,
                android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
        a.setStringConversionColumn(1);
        FilterQueryProviderprovider=newFilterQueryProvider() {
            @Overridepublic Cursor runQuery(CharSequence constraint) {
                // run in the background threadif (constraint == null) {
                    returnnull;
                }
                String[] columnNames = { Columns._ID, "name" };
                MatrixCursorc=newMatrixCursor(columnNames);
                try {

                    // total code for implementing my way of auto complteStringSOAP_ACTION="your action";
                    StringNAMESPACE="your name space";
                    StringMETHOD_NAME="your method name";
                    StringURL="your Url";
                    SoapObjectobjSoap=null;
                    SoapObjectrequest=newSoapObject(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 requestSoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    envelope.dotNet = true;

                    HttpTransportSEandroidHttpTransport=newHttpTransportSE(
                            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) {
                        StringstrData= objSoap.toString();

                    }

                    if (objSoap != null) {
                        System.out.println("getPropertyCountinevents//////////"
                                + objSoap.getPropertyCount());
                        for (inti=0; i < objSoap.getPropertyCount(); i++) {
                            Objectobj= objSoap.getProperty(i);
                            if (obj instanceof SoapObject) {
                                SoapObjectobjNew= (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

Solution 2:

You can set a global boolean in the preExecute() method of the AsyncTask. And cancel the previous AsynTask once the boolean is set(that is if a AsyncTask runs already). Thats it.

Please refer http://developer.android.com/reference/android/os/AsyncTask.html for the usage of AsyncTask

Post a Comment for "Dynamically Adding Data From Server To Auto Complete Textview"