尝试将其与ListView一起使用时,ArrayAdapter会出错

时间:2011-12-28 20:33:40

标签: java android listview android-arrayadapter

因此,经过几个小时试图找出问题,尝试不同的测试,我似乎无法弄清楚错误是什么。每当我输入搜索字符串,并且此方法运行时,我会收到两个错误。 1是在for循环中我比较字符串,我似乎得到一个StringIndexOutOfBounds,ListView给我一个来自ArrayAdapter的NullPointerException

代码:

 public void search(String[] s)
{
    setContentView(R.layout.search);
    final EditText text = (EditText)findViewById(R.id.searchBox);
    Button go = (Button)findViewById(R.id.go);
    final int sizeString = s.length;
    final String[] a = new String[sizeString];
    for(int i = 0;i<sizeString;i++)
        a[i] = s[i];
    go.setOnClickListener(new View.OnClickListener(){
        public void onClick (View v)
        {
            List<String> li = new ArrayList<String>();
            String entered = text.getText().toString().trim();
            String[] query = entered.split(" ");
            int temp;
            for (int i = 0; i < sizeString;i++)
            {
                temp = a[i].indexOf(" ");
                if (query[0].toLowerCase().equalsIgnoreCase(a[i].substring(0, temp).toLowerCase()))
                    li.add(a[i]);
            }
            setContentView(R.layout.search_results);
            final ListView list= (ListView)findViewById(R.id.search_list);
            String[] results = new String[sizeString];
            for(int i = 0; i<li.size();i++)
                results[i]=li.get(i);
            list.setAdapter(new ArrayAdapter<String>(Browse.this,android.R.layout.simple_list_item_1 ,results));
        }
    });

}

0 个答案:

没有答案