2

アプリケーションでオートコンプリートテキストビューを使用していくつかのことをしていました。ユーザーが何かを入力すると、提案が表示されます。しかし、1 つのクエリがあります。ユーザーがオートコンプリート テキストビューの推奨リストを入力してタップすると、さらに候補が表示されます。ユーザーがリストをタップしたときに検索結果を制限したいのですが、そこにそれ以上のオプションを表示したくありません。どうすればこの状況を克服できますか。

これは私が取り組んでいる私の完全なクラスです:

public class WikiSuggestActivity extends Activity {
public String data;
public List<String> suggest;
public AutoCompleteTextView autoComplete;
public ArrayAdapter<String> aAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    suggest = new ArrayList<String>();
    autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    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);
        }

    });

}

class getJson extends AsyncTask<String, String, String> {

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

}
}

任意のガイダンスをいただければ幸いです。

4

1 に答える 1

1
public class WikiSuggestActivity extends Activity {
    public String data;
    public List<String> suggest;
    public AutoCompleteTextView autoComplete;
    public ArrayAdapter<String> aAdapter;
    public TextWatcher mTextWatcher = 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);
        }
    };
    public boolean watch = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        suggest = new ArrayList<String>();
        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        autoComplete.addTextChangedListener( mTextWatcher );
        autoComplete.setOnItemClickListener( new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                autoComplete.removeTextChangedListener( mTextWatcher );
                aAdapter.clear();
                watch = false;
            }
        });
    }

    class getJson extends AsyncTask<String, String, String> {

        @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());
            }
            if( watch )
                runOnUiThread(new Runnable() {
                    public void run() {
                        aAdapter = new ArrayAdapter<String>(
                            getApplicationContext(), R.layout.item, suggest);
                        autoComplete.setAdapter(aAdapter);
                        aAdapter.notifyDataSetChanged();
                    }
                });

            return null;
        }

    }
}
于 2012-10-22T09:31:57.620 に答える