0

我正在为一个 pre-android 3.0 兼容的应用程序实现一个 searchView,并且正在为正确的 listView 重新加载而苦苦挣扎。我认为支持库存在一些问题,甚至可能是通过我无法使用的 onQueryTextChangedListener 的 EditText istea 上的 textChangeListener 实现的罕见实现。有什么帮助吗?

这是代码:

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.Editable;

import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;

public class RecipesActivity extends SherlockFragmentActivity {
public static String category;
private ListMenu listMenu;
public static EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {        

    setTheme(R.style.Theme_Sherlock); 
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activity_layout);

    getSupportActionBar().setBackgroundDrawable(getResources()
    .getDrawable(R.drawable.ab_bg_black));

    category=getIntent().getExtras().getString("category");

    FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
    listMenu=new ListMenu();
    ft.add(R.id.listFragment,listMenu).commit();        
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Search")
    .setIcon(R.drawable.search)
    .setActionView(R.layout.collapsible_search)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |                                                                                     

    MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

et=(EditText)menu.getItem(0).getActionView();       
return true;
}   


public static class ListMenu extends ListFragment 
implements LoaderManager.LoaderCallbacks<Cursor> {

private String category;
private String mCurFilter;
private MyCursorAdapter adapter;    
private ContentResolver resolver; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_layout, container, false);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

//initialization
category=getActivity().getIntent().getStringExtra("category");

setHasOptionsMenu(true);            
resolver = this.getActivity().getContentResolver();
final Cursor cursor;
String[] projection=
    {BaseColumns._ID,Recipes.NAME,Recipes.KEYWORDS,Recipes.STARRED};
if(category.equals("xxx")){
cursor = resolver.query(Recipes.CONTENT_URI_DRINKS, projection, null, null,
    Recipes.STARRED);
} else 
if(category.equals("yyy")){
cursor = resolver.query(Recipes.CONTENT_URI_HANGOVERS, projection, null, null, 
    Recipes.STARRED);
} else
{cursor = resolver.query(Recipes.CONTENT_URI_GAMES, projection, null, null, 
    Recipes.STARRED);
    }
//getActivity().getSupportLoaderManager().initLoader(0, null, this);
getLoaderManager().initLoader(0, null, this);

adapter=new MyCursorAdapter(getActivity(), cursor, true);
setListAdapter(adapter);

et.addTextChangedListener(new TextWatcher() {               
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

String text=s.toString();
String newFilter = (String) (!TextUtils.isEmpty(text) ? text : null);
Log.i("text changed listener","ontextchanged:"+newFilter);

if (mCurFilter == null && newFilter == null){;}else
if(mCurFilter != null && mCurFilter.equals(newFilter)){;}else
{
mCurFilter = newFilter;
getLoaderManager().restartLoader(0, null, ListMenu.this);
Log.i("QueryListMenu","onQueryTextChange");
}                   
}               

@Override public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent=new Intent(getActivity(),Viewer.class);
String[] projection = {Recipes.PATH };
String selection= "("+BaseColumns._ID+"="+id+")";
Cursor c=resolver.query(Recipes.getUri(category), projection, selection, null, 
    Recipes.STARRED+"DESC"+","+Recipes.NAME+" ASC");
c.moveToFirst();
String path=c.getString(c.getColumnIndex("path"));
intent.putExtra("path", category+"/"+path);
Log.i("onlistitemclick","path:"+path+" with id:"+id);
    startActivity(intent);        
    }


@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {       
    Uri firstUri=Recipes.getUri(category);        
    String selection;
    String[] projection = {BaseColumns._ID,Recipes.NAME,Recipes.STARRED}; 

    if (mCurFilter != null) {            
        selection = "(" + Recipes.NAME + " NOTNULL) AND ("
                + Recipes.KEYWORDS + " like '%"+mCurFilter+"%')";
    } else {            
        selection=null;
    }

    CursorLoader cursorLoader = new CursorLoader(getActivity(),
            firstUri, projection, selection, null, Recipes.STARRED+" 
    DESC"+","+Recipes.NAME+" ASC");
    Log.i("recipes activity","mCurFilter="+mCurFilter);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.changeCursor(data); 
}


@Override
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.changeCursor(null); 

}
    }    
4

1 回答 1

0

所以魔法就在几行字里。您必须将 FilterQueryProvider 添加到适配器:

adapter=new MyCursorAdapter(this, c, true);
adapter.setFilterQueryProvider(new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
    String[] projection = { Recipes._ID, Recipes.NAME, Recipes.KEYWORDS, 
                            Recipes.PATH, Recipes.STARRED }; 
    String selection = Recipes.KEYWORDS + " like '%" + constraint.toString() +"%'";
    return getContentResolver().query(Recipes.getUri(cat),projection, 
                                      selection, null, Recipes.STARRED + " DESC"
                                      + "," + Recipes.NAME + " ASC");
    }
});
于 2013-03-10T11:23:55.737 回答