Question

I have the following piece of code for an autocomplete text view being populated remotely, everything works fine till doInBackground but after that neither onProgressUpdate nor onPostExecute are called. I tried using the @Override annotation but Eclipse complains to remove it, and I also read that it is not required. Can someone tell me what I am missing? Why is it not working?

public class MainActivity extends Activity {
private Spinner pricelistSpinner;
private AutoCompleteTextView productsView;
private List<String> pricelist = new ArrayList<String>();
private List<String> products = new ArrayList<String>();
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    productsView = (AutoCompleteTextView) findViewById(R.id.autosuggestProducts);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, products);
    adapter.setNotifyOnChange(true);
    productsView.setAdapter(adapter);

    productsView.addTextChangedListener(new TextWatcher(){
         public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub  
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub  
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (productsView.getText().toString().length() > 1) {
                new PricelistAsyncTask().execute();
            }
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class PricelistAsyncTask extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        String val = productsView.getText().toString();
        HttpGet httpGet = new HttpGet("........" + val);
        try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String text = EntityUtils.toString(entity);

        if (!text.equals("[]")) {
            String pls = text.substring(1, text.length()-1);

            List<String> list = Arrays.asList(pls.split(","));
            for (int i=0; i<list.size(); i++) {
                String prod = list.get(i).substring(1, list.get(i).length()-1);
                list.set(i, prod);
            }
            products.clear();
            products.addAll(list);
            for (int i=0; i<list.size(); i++) {
                Log.d("sometag", products.get(i)); //Works fine till here.
            }

        }

        } catch (Exception e) {
            e.printStackTrace();
        }
        publishProgress();
        return null;
    }

     protected void onProgressUpdate(String... item) {
         Log.d("sometag", "Inside onProgressUpdate"); //Not logged, don't know if it is called or not
     }
    protected void onPostExecute() {
        Log.d("sometag", "Inside onPostExecute"); //Not logged, don't know if it is called or not
        Toast.makeText(getBaseContext(), "Done!", Toast.LENGTH_SHORT).show(); //not shown
        adapter.clear();
        adapter.addAll(products);
        adapter.notifyDataSetChanged(); //changes don't reflect
    }
}
}
Was it helpful?

Solution

The problem is with the signatures of the methods. The signatures should be

  • protected void onPostExecute(Void arg)
  • protected void onProgressUpdate(Void... void)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top