在自动完成textview中选择的选项显示错误

时间:2016-03-30 06:39:28

标签: android android-adapter autocompletetextview

当我从自动填充文本视图中选择选项时,它会显示错误的值。请检查图像。enter image description here

TopupTransfer.java

jQuery

Autocompleteadapter2.java

public class TopupTransfer extends BaseActivity{

    AutoCompleteTextView memberView;
    ArrayList<AutocompletetextviewGeSe> name1= null;
    Button btnSubmit;
    String membercode2,amount;
    TextInputLayout smspin_textInputLayout;
    EditText amnt,smspin;;
    HashMap<String,String> memberDetail;
    AutoCompleteAdapter2 adapter;
    DatabaseHelper db;
    int amont;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topuptransfer);
        if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
        {
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        }
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CB5F5")));
        db = new DatabaseHelper(TopupTransfer.this);

        memberView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

        name1 = new ArrayList<AutocompletetextviewGeSe>();
        memberDetail = new HashMap<String,String>();
        amnt = (EditText) findViewById(R.id.topup_amnt);
        btnSubmit = (Button) findViewById(R.id.button);
        smspin = (EditText) findViewById(R.id.smspin);
        smspin_textInputLayout = (TextInputLayout) findViewById(R.id.topuptransfer_smspin);

        if(ResponseString.getRequiredSmsPin().equals("TRUE"))
        {
            smspin_textInputLayout.setVisibility(View.VISIBLE);
            smspin.setVisibility(View.VISIBLE);
        }
        else
        {
            smspin_textInputLayout.setVisibility(View.GONE);
            smspin.setVisibility(View.GONE);
        }
        memberView.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) {

                String text = s.toString();
                Log.d("text", "" + text);

                int len = text.length();
                if (memberView != null)
                {
                if( len >= 3) {
                    Log.d("text", "" + text);
                    try {
                           name1 = GetList2(text);
                           Log.d("ADPTER LIST size", "" + name1.size());
                           Log.d("ADPTER LIST", name1.toString());
                           adapter = new AutoCompleteAdapter2(TopupTransfer.this,R.layout.autocompletetextview_layout,name1);
                           memberView.setAdapter(adapter);

                        }catch(Exception e){
                            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(TopupTransfer.this));
                        }
                    }
                }
            }

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

当我添加下面的代码时,它会给出错误&#34; java.lang.IndexOutOfBoundsException:索引0无效,大小为0&#34;

public class AutoCompleteAdapter2 extends ArrayAdapter<AutocompletetextviewGeSe> implements Filterable {
    private ArrayList<AutocompletetextviewGeSe> locations;
    private ArrayList<AutocompletetextviewGeSe> allLocations;
    private Filter filter;

    public AutoCompleteAdapter2(Context context, int layout, ArrayList<AutocompletetextviewGeSe> locations) {
        super(context, layout, locations);
        this.locations = locations;
        this.allLocations = locations;
    }

    @Override
    public int getCount() {
        return locations.size();
    }
    @Override
    public AutocompletetextviewGeSe getItem(int position) {
        return locations.get(position);
    }

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.autocompletetextview_layout,parent,false);
        }

        AutocompletetextviewGeSe l = locations.get(position);

        if (l != null) {
            TextView firm,mob;

            firm = (TextView) v.findViewById(R.id.a_firm);
            mob = (TextView) v.findViewById(R.id.a_mobno);

            firm.setText(l.getAfirm());
            mob.setText(l.getAmob());
        }

        return v;
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new LocationFilter();
        }
        return filter;
    }

    private class LocationFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults results = new FilterResults();
            if (charSequence == null || charSequence.length() == 0 || charSequence.length() >= 3) {
                locations = allLocations;
                notifyDataSetChanged();
                Log.d("!!!", "cleared filter");
                results.values = locations;
                results.count = locations.size();
            } else {
                ArrayList<AutocompletetextviewGeSe> nLocations = new ArrayList<AutocompletetextviewGeSe>();
                Log.d("nlocations size","" + nLocations.size());

                for (AutocompletetextviewGeSe l : locations) {
                        nLocations.add(l);
                }
                results.values = nLocations;
                results.count = nLocations.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            if (filterResults.count == 0) {
                notifyDataSetInvalidated();
            } else {
                locations = (ArrayList<AutocompletetextviewGeSe>) filterResults.values;
                notifyDataSetChanged();
            }
        }
    }
}

0 个答案:

没有答案