shopdropdown中的Android java数组适配器在100%的时间内无效

时间:2015-08-24 19:35:13

标签: java android android-arrayadapter autocompletetextview

我有一个从数据库中获取名称的AutoCompleteTextView,我的问题似乎是数据适配器或下拉列表在50%的时间内绑定。例如,如果有人输入John,那么你可能会得到Jo的建议而不是hn。即使我可以看到数据已经通过这是我的活动

,下拉列表也会随机显示
public class tester extends Activity
{
    EditText POST;
    AutoCompleteTextView autobox;
    String[] Arrays;
    ArrayAdapter<String> adapters;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // setContentView(R.layout.mypage);

       // set up Array with some initial values
       autobox= (AutoCompleteTextView) findViewById(R.id.autobox);
        String[] Arrays={"John Smith","Sam Austin"};
   // create the adapter inside the Oncreate method
     adapters = new ArrayAdapter<>(tester.this, 

android.R.layout.simple_dropdown_item_1line,Arrays);
        autobox.setAdapter(adapters);



        autobox.setThreshold(1);
        autobox.addTextChangedListener(new TextWatcher() {
            @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
             if(start>=1)
                autobox.getText().toString();
                new AutoText().execute();

            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

    }



    public class AutoText extends AsyncTask<String,String,String> {

        public String LOGIN_URL = "http://10.0.2.2:9999/api/namedata";


        HttpURLConnection connection;
        URL url;
        StringBuilder sb= new StringBuilder();
        String line="",nameList="";
        String names="";
        DataOutputStream DO;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            names= autobox.getText().toString();

        }




        @Override
        protected String  doInBackground(String[] jsonData) {
            String query= "names="+ names;

            try {
                url = new URL(LOGIN_URL);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.connect();
                connection.setReadTimeout(10000);
                connection.setConnectTimeout(15000);

                DO = new DataOutputStream(connection.getOutputStream());
                DO.writeBytes(query);
                DO.flush();
                DO.close();

                InputStream in = new BufferedInputStream(connection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));


                while ((line = reader.readLine()) != null) {

                    sb.append(line);

                }

               nameList= sb.toString();
            }
            catch (Exception e) {
                e.printStackTrace();
                System.err.println("Why I am getting errors:");
            }
            return nameList;
        }



        protected void onPostExecute(String results) {
            try
            {

                JSONObject jsonn = new JSONObject(nameList);
                JSONArray jArray = jsonn.getJSONArray("name_updates");
                JSONObject jobject = null;
                String fullnames="";
                JSONArray JA = new JSONArray();
                for (int i = 0; i < jArray.length(); i++) {
                   fullnames+= jobject.getString("fullnames")+"-";

                   JA.put(jobject);
                }
              Arrays= fullnames.split("-");
                  // check the output values
                System.out.println("ee " + java.util.Arrays.toString(Arrays));
                adapters.clear();
                adapters.addAll(Arrays);
                adapters.notifyDataSetChanged();
            }
            catch(Exception e)
            {

            }
        }
    }
}

如果您注意到我使用 System.out.println(“ee”+ java.util.Arrays.toString(Arrays)); onPostexecute并且可以看到所有数据都正确输入但是,下拉问题有时并不表示主要问题。当下拉菜单显示时,所有内容都会按原样更新。我试过把autobox.showDropDown();在notifyDataSetChanged()之后,当有新数据时它仍然随机消失。我尝试了Android - AutoCompleteTextView, showDropDown() doesn't always work但它对我不起作用,将适配器设为null并继续重新创建它似乎是一个坏主意。

0 个答案:

没有答案