0

我有这个 Autocomplete 类,我正在修改适配器,以便它不仅会显示文本,还会在出现的文本前显示一个图标。所有可用的选项都存储在字符串数组中。

现在,由于某种原因,无论我输入什么,它都只显示 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();
                }
            } 
        }

    );
}   

}

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

4

1 回答 1

0

不要比较Strings ,==因为它会检查它是否是同一个对象而不是同一个文本。改为使用.equals

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

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

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

于 2012-04-15T21:45:29.390 回答