Android自动完成功能无法正常运行...!

时间:2012-04-15 21:42:30

标签: java android autocomplete android-arrayadapter custom-adapter

我有这个自动完成类,我正在修改适配器,以便它不会显示文本,而是会在显示的文本前面显示一个图标。所有可用选项都存储在一个字符串数组中。

现在,出于某种原因,无论我输入什么内容,它都只会显示5个项目。

public class AutoCompleteText extends Activity{

public class CustomAdapter extends ArrayAdapter<String>{

    public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId, objects);
    }

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

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.arow,parent, false);
        TextView label = (TextView) row.findViewById(R.id.element);
        label.setText(listItems[position]);
        ImageView icon = (ImageView) row.findViewById(R.id.image);

        if("Item1".equals(listItems[position])|| something else || something else){
            icon.setImageResource(R.drawable.img1);
        }
        else if("Item2".equals(listItems[position])|| something else || something else){
            icon.setImageResource(R.drawable.img2);
        }
        else if("Item3".equals(listItems[position])|| something else || something else){
            icon.setImageResource(R.drawable.img3);
        }
        else if("Item4".equals(listItems[position])|| something else || something else){
            icon.setImageResource(R.drawable.img4);
        }
        else if("Item5".equals(listItems[position])|| something else || something else){
            icon.setImageResource(R.drawable.img5);
        }
        else if("Item6".equals(listItems[position])|| something else || something else) {
            icon.setImageResource(R.drawable.img6);
        }
        else{
            icon.setImageResource(R.drawable.whatever);
        }
        return row;
    }
}


AutoCompleteTextView acTextView;
private static String listItems[] = { "Item1", "Item2","Item3","Item4","Item5", "Item6", "Item7", "Item8", "Item9", "Item10"
                                        "Item11", "Item12","Item13","Item14","Item15", "Item16", "Item17", "Item18", "Item19", "Item20");

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

    acTextView = (AutoCompleteTextView)findViewById(R.id.searchfield);
    acTextView.setThreshold(1);
    acTextView.setAdapter(new CustomAdapter(AutoCompleteText.this,R.layout.arow, listItems));
    acTextView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                try {
                    //random staff
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } 
        }

    );
}   

}

应用程序编译并运行正常,没有任何错误。谁可以帮我这个事? 谢谢。

1 个答案:

答案 0 :(得分:0)

不要将String==进行比较,因为它会检查它是否是相同的对象而不是相同的文本。请改用.equals

if ("Item6".equals(listItems[position]) { 

它可能适用于您的情况,因为编译器可能足够聪明,可以使用相同的对象来表示文本,但这通常不是一个好主意。

此外,您的代码中只有5个(+默认)项。你看到了吗?