Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中未调用AutoCompleteTextView的自定义阵列适配器的getView?_Java_Android - Fatal编程技术网

Java Android中未调用AutoCompleteTextView的自定义阵列适配器的getView?

Java Android中未调用AutoCompleteTextView的自定义阵列适配器的getView?,java,android,Java,Android,我想使用自定义的ArrayAdapter使用AutoCompleteTextView。我决定使用阵列适配器 但是在我的自定义ArrayAdaptergetView()中,适配器未设置AutoCompleteTextView 下面是我迄今为止所做的尝试: MainActivity.java: public类MainActivity扩展了AppCompatActivity{ 自动完成文本视图文本; String[]languages={“Android”、“java”、“IOS”、“SQL”、“JD

我想使用自定义的
ArrayAdapter
使用
AutoCompleteTextView
。我决定使用
阵列适配器

但是在我的自定义
ArrayAdapter
getView()
中,适配器未设置
AutoCompleteTextView

下面是我迄今为止所做的尝试:

MainActivity.java

public类MainActivity扩展了AppCompatActivity{
自动完成文本视图文本;
String[]languages={“Android”、“java”、“IOS”、“SQL”、“JDBC”、“Web服务”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
名称[]名称=this.initNameArray();
this.text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
this.text.setThreshold(1);
adapter adapter=新适配器(此,R.layout.recent_文本,名称);
this.text.setAdapter(适配器);
}
私有名称[]initNameArray(){
名称[]最近搜索=新名称[this.languages.length];
int i=0;
for(字符串s:this.languages){
名称=新名称();
名称。设置名称;
最近的搜索[i++]=名称;
}
返回最近的搜索;
}
//自定义适配器
公共类适配器扩展了ArrayAdapter{
姓名[];
语境;
国际布局资源;
公共适配器(上下文、int资源、名称[]对象){
超级(上下文、资源、对象);
this.names=对象;
this.context=上下文;
this.layoutResourceId=资源;
}
@凌驾
public int getCount(){
返回this.names.length;
}
@凌驾
公共名称getItem(int位置){
返回此。名称[位置];
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
试一试{
/*
*convertView参数本质上是一个“ScrapView”,如下所示
*我是卢卡斯·波斯特
* http://lucasr.org/2012/04/05/performance-tips-for-androids-
*listview/当listview为
*要求您回收行布局。因此,当使用convertView时
*不为null,只需更新其内容,而不是
*正在膨胀新的行布局。
*/
if(convertView==null){
//使布局膨胀
LayoutInflater充气器=(LayoutInflater)((this.context))
.getSystemService(上下文布局\充气机\服务);
convertView=充气机。充气(this.layoutResourceId,父项,false);
}
//基于位置的对象项
Names objectItem=this.getItem(位置);
//获取文本视图,然后设置文本(项目名称)和标记
//(项目ID)值
TextView textViewItem=(TextView)convertView.findViewById(R.id.recent_search);
textViewItem.setText(objectItem.getName());
//如果你想增加一些风格,你可以做一些事情
//比如:
textViewItem.setBackgroundColor(Color.CYAN);
}捕获(NullPointerException e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
返回视图;
}
}
//我的pojo课
公共类名{
字符串名;
公共字符串getName(){
返回此.name;
}
公共void集合名(字符串名){
this.name=名称;
}
}
}
以下代码:

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;
        }
    }
public类MainActivity扩展了AppCompatActivity{
自动完成文本视图文本;
String[]languages={“Android”、“java”、“IOS”、“SQL”、“JDBC”、“Web服务”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
列表名称=initNameArray();
text=(AutoCompleteTextView)findviewbyd(R.id.autocompleteTvId);
文本。设置阈值(1);
CustomArrayAdapter=新的CustomArrayAdapter(此,R.layout.recent_文本,名称);
setAdapter(适配器);
}
私有列表initNameArray(){
列出最近的搜索=新建ArrayList();
int i=0;
for(字符串s:语言){
名称=新名称();
名称。设置名称;
最近的_search.add(i++,名称);
}
返回最近的搜索;
}
//自定义阵列适配器
公共类CustomArrayAdapter扩展了ArrayAdapter{
列出姓名;
语境;
国际布局资源;
公共CustomArrayAdapter(上下文、int资源、列表对象){
超级(上下文、资源、对象);
名称=对象;
this.context=上下文;
layoutResourceId=资源;
}
@凌驾
public int getCount(){
返回super.getCount();
}
@凌驾
公共名称getItem(int位置){
返回name.get(位置);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
试一试{
/*
*convertView参数本质上是一个“ScrapView”,如Lucas post所述
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
*当ListView要求您回收行布局时,它将具有非空值。
*那么,什么时候