android autocompletetextview显示无建议

x6h2sr28  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(405)

错误:span\u exclusive\u exclusive span的长度不能为零
应用程序运行但在autocompletetextview中不显示输出
英语是一门学科。我在translatormodel类中为json对象提供了getter和setter方法。

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView autoCompleteTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

        new HttpGetTask().execute("http://192.168.0.107/abc/translator.php");

    }

    public class HttpGetTask extends AsyncTask<String, String, List<TranslatorModel>> {

        @Override
        protected List<TranslatorModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String finalJson = buffer.toString();
                JSONArray parentArray = new JSONArray(finalJson);

                List<TranslatorModel> translatorModelList = new ArrayList<>();

                for(int i= 0; i<parentArray.length();i++) {

                    JSONObject finalObject = parentArray.getJSONObject(i);

                    TranslatorModel translatorModel = new TranslatorModel();

                        translatorModel.setEnglish(finalObject.getString("englishSentence"));

                        translatorModelList.add(translatorModel);
                    }

                return translatorModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }

                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<TranslatorModel> data) {

            AutoCompleteAdapter adapter = new AutoCompleteAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,data);

            autoCompleteTextView.setAdapter(adapter);
            autoCompleteTextView.setThreshold(1);

            super.onPostExecute(data);
        }
    }

    public class AutoCompleteAdapter extends ArrayAdapter {

        private List<TranslatorModel> translatorModelList;
        private int resource;
        private LayoutInflater inflater;

        public AutoCompleteAdapter(Context context, int resource, List<TranslatorModel> objects) {
            super(context, resource, objects);

            translatorModelList = objects;
            this.resource = resource;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {
                convertView = inflater.inflate(resource, null);

                holder = new ViewHolder();

                holder.autoCompleteTextView = (AutoCompleteTextView) convertView.findViewById(R.id.autoCompleteTextView);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.autoCompleteTextView.setText(translatorModelList.get(position).getEnglish());

            return convertView;
        }

        class ViewHolder {

            private AutoCompleteTextView autoCompleteTextView;

        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题