0

我是 android 开发的菜鸟,我正在尝试实现搜索栏,以便我可以搜索列表视图。让搜索视图出现我没有问题。但是,当我在搜索视图中输入文本时,应用程序崩溃说存在“非法状态异常:我无法使用不可过滤的适配器调用 onTextChanged。我不明白为什么我会收到此错误,因为我的适配器类实现了可过滤。任何帮助非常感谢。

主要活动

public class StoresActivity extends SherlockActivity implements OnQueryTextListener {
 ArrayList<HashMap<String, String>> storeList;
 ListView list;
 LazyAdapter adapter;
 EditText inputSearch;

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

    storeList = new ArrayList<HashMap<String, String>>();
    list=(ListView)findViewById(R.id.list);
    SharedPreferences preferences = getSharedPreferences("Oleref", 2);      

    String employee = preferences.getString("employee", null);
    ActionBar bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.bigmoney.com="+ employee});
    try {
        storeList = task.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    adapter=new LazyAdapter(this, storeList);
    list.setAdapter(adapter);
    list.setItemsCanFocus(true); 

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    //Create the search view
    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());

    //list = getListView();
    list.setTextFilterEnabled(true);

    setupSearchView(searchView);

    menu.add(0, 1, 1, "Search Text")
        .setIcon(R.drawable.ic_launcher)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return super.onCreateOptionsMenu(menu);
}

private void setupSearchView(SearchView mSearchView) {
    mSearchView.setIconifiedByDefault(false);
    mSearchView.setSubmitButtonEnabled(false);
    mSearchView.setOnQueryTextListener(StoresActivity.this);
    mSearchView.setQueryHint("Search Text");
}

@Override
public boolean onQueryTextChange(String newText) {
    if (TextUtils.isEmpty(newText)) {
        list.clearTextFilter();
    } else {
        list.setFilterText(newText);
    }
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) 
    {        
      case android.R.id.home:
          Intent chart = new Intent();
          chart.setClass(StoresActivity.this, StoresActivity.class);


          chart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

      StoresActivity.this.startActivity(chart); 
            return true;
      case 2:

          Intent chart2 = new Intent();
          chart2.setClass(StoresActivity.this, WalkInActivity.class);

          chart2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

      StoresActivity.this.startActivity(chart2); 
            return true;


    }
    return true;
  }

}

适配器类

public class LazyAdapter extends BaseAdapter implements Filterable {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public String sn;
StoresActivity storesActivity;
HashMap<String, String> stores;
HashMap<String, String> storedata;
 public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    }
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.store_row, null);

        TextView store = (TextView)vi.findViewById(R.id.storeText); // title
        TextView store_num = (TextView)vi.findViewById(R.id.store_num); // artist name
        TextView street = (TextView)vi.findViewById(R.id.address);
        TextView city = (TextView)vi.findViewById(R.id.city);
        Button newbutton=(Button)vi.findViewById(R.id.newser);
        Button historybutton=(Button)vi.findViewById(R.id.history);
        storedata = new HashMap<String, String>();
        storedata = data.get(position);
        newbutton.setTag(position);
         newbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent chart = new Intent();         
                  chart.setClass(v.getContext(), SectionActivity.class); 
                 storedata = data.get((Integer)v.getTag());
                  sn = storedata.get("Store_Num");
                  String store = storedata.get("Store");
                  chart.putExtra("sn", sn);
                  chart.putExtra("store", store);
                  chart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

                  v.getContext().startActivity(chart); 
            }
        });

         historybutton.setTag(position);
         historybutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     Intent chart = new Intent();         
                      chart.setClass(v.getContext(), HistoryList.class); 
                     storedata = data.get((Integer)v.getTag());
                      sn = storedata.get("Store_Num");
                      String store = storedata.get("Store");
                      chart.putExtra("sn", sn);
                      chart.putExtra("store", store);
                      chart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

                      v.getContext().startActivity(chart); 
                }
            });



        // Setting all values in listview
        store.setText(storedata.get("Store"));
        store_num.setText(storedata.get("Store_Num"));
        street.setText(storedata.get("Address"));
        city.setText(storedata.get("City") + " " + storedata.get("State") + " " + storedata.get("Zip"));

        return vi;
}
public boolean onLoadClass(Class arg0) {
    // TODO Auto-generated method stub
    return false;
}

//Added for search BJR 5-16-2013
@Override
public android.widget.Filter getFilter() {
    // TODO Auto-generated method stub
    return null;
}

}
4

1 回答 1

0

由于您使用的是 ActionBarSherlock,我假设您的最小 SDK 低于 11,但 SearchView 小部件需要 Android 3.0(API 级别 11)及更高版本。您可能想查看搜索对话框。请查看文档http://developer.android.com/guide/topics/search/search-dialog.html

于 2013-05-16T18:49:01.783 回答