2012-08-15 59 views
0

我正在执行一个searchView到一个与android 3.0兼容的应用程序,并且正在努力适当地使用listView reloaing。 我认为支持库存在一些问题,甚至有可能通过我无法使用的onTextTextChangedListener的EditText istea上的textChangeListener实现。 有什么帮助吗?CursorLoader不更新listView,但光标有正确的结果

下面是代码:

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); 

} 
    }  
+0

我发现的问题是,当mCurFilter为开头空。仍然不知道如何解决它。它在开始时必须是空的。 – vandus 2012-08-16 21:02:30

回答

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"); 
    } 
});