3

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 happens when i type multiple characters concurrently,my application shows a dialog and forced close. Thanks in Advance

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
Dhiraj Choudhary
  • 195
  • 5
  • 16
  • 1
    see my answer in this thread http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara – pskink Nov 15 '13 at 10:11
  • @pskink: i am not getting it , what is SimpleCursorAdaptor. – Dhiraj Choudhary Nov 15 '13 at 10:16
  • I guess each time the text changes,it starts a new Thread/AsyncTask. Don't do it like that. Stop the current executing Asynctask and start a new one. So that at any given point in time only one background thread is executing for fetching data. – Abhishek V Nov 15 '13 at 10:27
  • @AbhishekV how i would i know that previous Asyntask is running and how to stop it. – Dhiraj Choudhary Nov 15 '13 at 10:30
  • use my solution: you dont need to deal with any Threads/AsyncTasks etc – pskink Nov 15 '13 at 10:34
  • @dhairya instead of `AsyncTask` you can use `Thread`. Make a global object of the thread and use that object everytime you want to run a thread.You can check if a thread is running using `isAlive()` method of the thread.And to stop the thread you can refer this question http://stackoverflow.com/questions/4756862/how-to-stop-a-thread – Abhishek V Nov 15 '13 at 10:51
  • @dhairya According to your code, it will start many threads and upon that each thread will try to **access the UI concurrently**. That is the cause of the force close i guess. – Abhishek V Nov 15 '13 at 10:54
  • dont use any Threads/AsyncTasks: use system build-in mechanism for filtering data out – pskink Nov 15 '13 at 10:56
  • @pskink: what is from and to in your solution and why are using fix array of items, in my case i have to get data from server , then have to add on adaptor. – Dhiraj Choudhary Nov 15 '13 at 11:00
  • try to run my code: copy/paste it in onCreate – pskink Nov 15 '13 at 11:03
  • @pskink : your code is working fine but it is showing dropdown when i type atleast 3 or 4 characters, where you have set threshold to autocompletetext view. – Dhiraj Choudhary Nov 15 '13 at 11:11
  • no, it should work after one character typed - setThreshold(1) – pskink Nov 15 '13 at 11:15
  • @pskink: i have implemented your code for my webservices but it is not working, on typing each time different characters it always shows same drop down list – Dhiraj Choudhary Nov 15 '13 at 11:27
  • does the code work with wikipedia search? if so, then you did something wrong with your server – pskink Nov 15 '13 at 11:32
  • @pskink: you have saved my life brother, can i have your emailId please. – Dhiraj Choudhary Nov 15 '13 at 11:32
  • @pskink: i forgot to add constraint on server Url.Now Its work Bro... – Dhiraj Choudhary Nov 15 '13 at 11:35
  • its fine, it is working now – pskink Nov 15 '13 at 11:35

3 Answers3

1

Here is working solution i got suggested by my friend 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
Akshay
  • 2,506
  • 4
  • 34
  • 55
Dhiraj Choudhary
  • 195
  • 5
  • 16
1

You can use asynytask for it like this

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

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

Sripathi
  • 1,760
  • 15
  • 20