jerryleooo
jerryleooo

Reputation: 863

When I press backspace did the AutoCompleteTextView show the suggestion list

My code is following ...

public class NameListActivity extends Activity implements TextWatcher {
    private Button add = null;
    private AutoCompleteTextView editAuto = null;
    private Button chfrlist = null;
    private ImageView im = null;
    String access_token = new String();
    private ImageView infobtn = null;
    private PopupWindow popupWindow;
    private View view;
    private ProgressDialog pd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_name_list);
        access_token = MainService.readToken();

        add = (Button) findViewById(R.id.add_button);
        editAuto = (AutoCompleteTextView) findViewById(R.id.editAuto);
        chfrlist = (Button) findViewById(R.id.chfrlistbutton);
        im = (ImageView) findViewById(R.id.helpact);
        im.setOnClickListener(new ImageListener());
        infobtn = (ImageView) findViewById(R.id.informbtn);
        initPopupWindow();
        infobtn.setOnClickListener(new infobtnListener());
        editAuto.addTextChangedListener(this);
        add.setOnClickListener(new addListener());
        chfrlist.setOnClickListener(new ChfrListListener());
    }

    public class addListener implements OnClickListener {

        public void onClick(View v) {
            addTask task = new addTask();
            task.execute();
            editAuto.setText("");
        }
    }

    public void afterTextChanged(Editable arg0) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        onTextChangedTask task = new onTextChangedTask();
        task.execute();
    }

    public class onTextChangedTask extends AsyncTask<Void, Void, Void> {

        ArrayAdapter<String> adapter = null;
        String[] userName = null;
        String q = null;
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = null;
        ArrayList<String> userNameArrayList = new ArrayList<String>();
        Weibo weibo = new Weibo();

        @Override
        protected void onPreExecute() {
            weibo.setToken(access_token);
            q = editAuto.getText().toString();
            System.out.println("start onTextChanged");
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            if (q.length() != 0) {
                System.out.println("q is " + q);

                String s1 = "https://api.weibo.com/search/suggestions/users.json";
                try {
                    jsonArray = Weibo.client.get(s1,
                            new PostParameter[] { new PostParameter("q", q) })
                            .asJSONArray();
                } catch (Throwable e) {
                    System.out.println("这里有个神马异常呢 。。。" + e);
                }

                System.out.println("return length is " + jsonArray.length());

                for (int i = 0; i < jsonArray.length(); i++) {
                    try {
                        jsonObject = jsonArray.getJSONObject(i);
                        String sname = jsonObject.getString("screen_name");
                        userNameArrayList.add(sname);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                userName = (String[]) userNameArrayList
                        .toArray(new String[userNameArrayList.size()]);

                adapter = new ArrayAdapter<String>(NameListActivity.this,
                        android.R.layout.simple_dropdown_item_1line, userName);

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            System.out.println("post");
            editAuto.setAdapter(adapter);
        }
    }

    void showToast(String s) {
        Toast toast = Toast.makeText(getApplicationContext(), s,
                Toast.LENGTH_LONG);
        toast.show();
    }

    public class addTask extends AsyncTask<Void, Void, Void> {

        String s = null;
        boolean flag = false;
        User user = null;
        Weibo weibo = new Weibo();
        String screen_name = null;

        protected void onPreExecute() {
            Toast tt = Toast.makeText(getApplicationContext(), "正在将用户添加到备份名单",
                    Toast.LENGTH_LONG);
            tt.setGravity(Gravity.CENTER, 0, 0);
            tt.show();
            weibo.setToken(access_token);
            screen_name = editAuto.getText().toString();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            if (screen_name.length() != 0) {
                Users um = new Users();
                try {
                    user = new User(Weibo.client.get(
                            "https://api.weibo.com/users/show.json",
                            new PostParameter[] { new PostParameter(
                                    "screen_name", screen_name) })
                            .asJSONObject());
                } catch (Throwable e) {
                    e.printStackTrace();
                    flag = true;
                    s = new String("您输入的这个用户好像不存在唉");
                }
                if (user != null) {
                    ContentValues values = new ContentValues();
                    values.put("uid", user.getId());
                    values.put("user_name", user.getName());
                    SQLiteDatabase db = null;
                    try {
                        db = MainService.getDatabase();
                    } catch (Exception e) {
                        System.out.println("db error");
                        finish();
                    }
                    Cursor result = db.query("users", new String[] { "uid",
                            "user_name" }, "uid=?",
                            new String[] { user.getId() }, null, null, null);
                    if (result.getCount() == 0)
                        db.insert("users", null, values);
                } else {
                    flag = true;
                    s = new String("网络存在问题,检查一下吧");
                }
            } else {
                flag = true;
                s = new String("框里输入点东西才能添加啊");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            if (flag == true) {
                System.out.println("要打印的是" + s);
                showToast(s);
            }
        }
    }

    public class infobtnListener implements OnClickListener {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("点击了图片");
            ColorDrawable cd = new ColorDrawable(-0000);
            popupWindow.setBackgroundDrawable(cd);
            // popupWindow.showAsDropDown(v);
            popupWindow.showAtLocation(findViewById(R.id.informbtn),
                    Gravity.LEFT | Gravity.BOTTOM, 0, 100);
        }
    }

    public class ImageListener implements OnClickListener {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            // Intent t = new Intent(NameListActivity.this,
            // GridLayoutActivity.class);
            // startActivity(t);
            finish();
        }

    }

    public class ChfrListListener implements OnClickListener {

        public void onClick(View v) {
            if ((haveInternet() == true)
                    && (GridLayoutActivity.hasAccessToken() == true)) {
                // TODO Auto-generated method stub
                pd = ProgressDialog.show(NameListActivity.this, "",
                        "正在从服务器上获取数据,可能需要较长时间,请耐心等待 ...");
                /* 开启一个新线程,在新线程里执行耗时的方法 */
                new Thread(new Runnable() {
                    public void run() {
                        Intent t = new Intent(NameListActivity.this,
                                ChooseFromListActivity.class);
                        startActivity(t);
                        finish();

                        handler.sendEmptyMessage(0);// 执行耗时的方法之后发送消给handler
                    }
                }).start();
            } else {
                Intent t = new Intent(NameListActivity.this,
                        WebViewActivity.class);
                startActivity(t);
                finish();
            }
        }
    }

    private void initPopupWindow() {
        view = getLayoutInflater().inflate(R.layout.namewindow, null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        // 这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。
        popupWindow.setOutsideTouchable(true);// 不能在没有焦点的时候使用
    }

    private boolean haveInternet() {
        NetworkInfo info = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (info == null || !info.isConnected()) {
            return false;
        }
        if (info.isRoaming()) {
            // here is the roaming option you can change it if you want to
            // disable internet while roaming, just return false
            return true;
        }
        return true;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法
            pd.dismiss();// 关闭ProgressDialog
        }
    };

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_name_list, menu);
        return true;
    }
}

My question is : when I input words in the EditText, nothing happened. But when I press backspace did the AutoCompleteTextView show the suggestion list ... the problem is in the editAuto.setAdapter(adapter);

What is wrong?

Upvotes: 1

Views: 1773

Answers (2)

Rohan Kandwal
Rohan Kandwal

Reputation: 9336

I know this is way late but for those who face similar problems here is the solution to show the autoCompleteTextView's drop down whenever you want i.e on button click or onTextChanged. After you set the ArrayAdapter for the autoCompleteTextView just put the following line.

autoCompleteTextView.showDropDown();

Upvotes: 1

Tapan Desai
Tapan Desai

Reputation: 848

make the following changes in your code

1) Instead of

private AutoCompleteTextView editAuto = null; JUST WRITE private AutoCompleteTextView editAuto;

2) Add this line to onCrate()

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, <your array name here>);

and remove this line from onTextChangedTask()

ArrayList<String> userNameArrayList = new ArrayList<String>();

3) Add this line to onCrate()

editAuto.setAdapter(adapter);

Upvotes: 1

Related Questions