简体   繁体   中英

getView of custom ArrayAdapter for AutoCompleteTextView not called in Android?

I want to use AutoCompleteTextView using a custom ArrayAdapter . I decided to use an Arrayadapter .

But in my custom ArrayAdapter getView() is not called, AutoCompleteTextView is not set by the adapter.

Below one can see what I tried so far:

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView text;

    String[] languages = { "Android ", "java", "IOS", "SQL", "JDBC", "Web services" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Names[] names = this.initNameArray();
        this.text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        this.text.setThreshold(1);
        adapter adapter = new adapter(this, R.layout.recent_text, names);

        this.text.setAdapter(adapter);

    }

    private Names[] initNameArray() {

        Names[] recent_search = new Names[this.languages.length];
        int i = 0;
        for (String s : this.languages) {
            Names names = new Names();
            names.setName(s);
            recent_search[i++] = names;
        }

        return recent_search;
    }
    // custom adapter

    public class adapter extends ArrayAdapter<Names> {
        Names   names[];
        Context context;
        int     layoutResourceId;

        public adapter(Context context, int resource, Names[] objects) {
            super(context, resource, objects);
            this.names = objects;
            this.context = context;
            this.layoutResourceId = resource;
        }

        @Override
        public int getCount() {
            return this.names.length;
        }

        @Override
        public Names getItem(int position) {
            return this.names[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            try {
                /*
                 * The convertView argument is essentially a "ScrapView" as
                 * described is Lucas post
                 * http://lucasr.org/2012/04/05/performance-tips-for-androids-
                 * listview/ It will have a non-null value when ListView is
                 * asking you recycle the row layout. So, when convertView is
                 * not null, you should simply update its contents instead of
                 * inflating a new row layout.
                 */
                if (convertView == null) {
                    // inflate the layout
                    LayoutInflater inflater = (LayoutInflater) ((this.context))
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(this.layoutResourceId, parent, false);
                }

                // object item based on the position
                Names objectItem = this.getItem(position);

                // get the TextView and then set the text (item name) and tag
                // (item ID) values
                TextView textViewItem = (TextView) convertView.findViewById(R.id.recent_search);
                textViewItem.setText(objectItem.getName());

                // in case you want to add some style, you can do something
                // like:
                textViewItem.setBackgroundColor(Color.CYAN);

            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return convertView;
        }
    }

    // my pojo class
    public class Names {
        String name;

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

The following code:

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView text;

    String[] languages = {"Android ", "java", "IOS", "SQL", "JDBC", "Web services"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Names> names = initNameArray();
        text = (AutoCompleteTextView) findViewById(R.id.autocompleteTvId);
        text.setThreshold(1);
        CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.recent_text, names);

        text.setAdapter(adapter);
    }

    private List<Names> initNameArray() {

        List<Names> recent_search = new ArrayList<>();
        int i = 0;
        for (String s : languages) {
            Names names = new Names();
            names.setName(s);
            recent_search.add(i++, names);
        }

        return recent_search;
    }
    //custom CustomArrayAdapter

    public class CustomArrayAdapter extends ArrayAdapter<Names> {
        List<Names> names;
        Context context;
        int layoutResourceId;

        public CustomArrayAdapter(Context context, int resource, List<Names> objects) {
            super(context, resource, objects);
            names = objects;
            this.context = context;
            layoutResourceId = resource;
        }


        @Override
        public int getCount() {
            return super.getCount();
        }

        @Override
        public Names getItem(int position) {
            return names.get(position);
        }

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

            /*
             * The convertView argument is essentially a "ScrapView" as described is Lucas post
             * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
             * It will have a non-null value when ListView is asking you recycle the row layout.
             * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
             */
                if (convertView == null) {
                    // inflate the layout
                    LayoutInflater inflater = (LayoutInflater) ((context)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(layoutResourceId, parent, false);
                }

                // object item based on the position
                Names objectItem = getItem(position);

                // get the TextView and then set the text (item name) and tag (item ID) values
                TextView textViewItem = (TextView) convertView.findViewById(R.id.tv_recent_text);
                textViewItem.setText(objectItem.getName());

                // in case you want to add some style, you can do something like:
                textViewItem.setBackgroundColor(Color.CYAN);

            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return convertView;
        }
    }

    //my pojo class

    public class Names {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}

should work just fine for you. The only difference from your code is the List instead of array. In order to make it work, delete hello world and start type 'An', 'Ja', etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM