0

I'm trying to trap event listener on the auto-suggest for my application. i'm populating the list using wiki-suggest : http://en.wikipedia.org/w/api.php?action=opensearch&search=%22bike%22&limit=8&namespace=0&format=json.

I'm getting all the things right but now I want to just click on the item of the list and bring the string to the next activity. I have gone through : http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

But that is not giving me idea that how can I implement that.

I'm pasting my code here for the auto-suggest populating. Please suggest how to overcome this problem.

@Override
    protected String doInBackground(String... key) {
        String newText = key[0];
        newText = newText.trim();
        newText = newText.replace(" ", "+");
        try {
            HttpClient hClient = new DefaultHttpClient();
            HttpGet hGet = new HttpGet(
                    "http://en.wikipedia.org/w/api.php?action=opensearch&search="
                            + newText + "&limit=8&namespace=0&format=json");
            ResponseHandler<String> rHandler = new BasicResponseHandler();
            data = hClient.execute(hGet, rHandler);
            suggest = new ArrayList<String>();
            JSONArray jArray = new JSONArray(data);
            for (int i = 0; i < jArray.getJSONArray(1).length(); i++) {
                String SuggestKey = jArray.getJSONArray(1).getString(i);
                suggest.add(SuggestKey);
            }

        } catch (Exception e) {
            Log.w("Error", e.getMessage());
        }
        runOnUiThread(new Runnable() {
            public void run() {
                aAdapter = new ArrayAdapter<String>(
                        getApplicationContext(), R.layout.item, suggest);
                autoComplete.setAdapter(aAdapter);
                aAdapter.notifyDataSetChanged();
            }
        });

        return null;
    }

This is how I get new suggestions from wiki-suggest:

autoComplete = (AutoCompleteTextView) findViewById(R.id.text); 
autoComplete.addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable editable) {} 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
        String newText = s.toString(); 
        new getJson().execute(newText); 
    } 
}); 
Sam
  • 86,580
  • 20
  • 181
  • 179
Anupam
  • 3,742
  • 18
  • 55
  • 87

2 Answers2

1

Update from comments
Where you initialize autoComplete, probably in onCreate(), add your OnItemClickListener like this:

autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String text = ((TextView) view).getText().toString();
        new getJson().execute(text);
    }
});
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Sam, Thank you for the answer. Here I have to pass the string through json. For that right now I'm using TextWatcher on TextChangedListener. For this I will have to replace the whole TextChangedListener with onItemClickListener or what? – Anupam Sep 07 '12 at 17:35
  • Please post the code that passes your String through JSON, so I can see how you do this. – Sam Sep 07 '12 at 17:40
  • autoComplete = (AutoCompleteTextView) findViewById(R.id.text); autoComplete.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable editable) { // 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) { String newText = s.toString(); new getJson().execute(newText); } }); – Anupam Sep 07 '12 at 17:44
  • I have used the above code for passing string to the json which links up with the top mentioned code. And give me the result. Please help me to overcome this problem. – Anupam Sep 07 '12 at 17:46
  • Hey Sam! I have implemented that in the code, now my drop down list is not showing up. I think we will have to use both the functions, because without TextWatcher the drop down list will not work. And, I have used both the functions, but still no success. – Anupam Sep 07 '12 at 18:15
0

Solved that. Just has to pass the whole string to the URL on ItemClickListener.

Anupam
  • 3,742
  • 18
  • 55
  • 87