0

Android 3.0より前の互換性のあるアプリにsearchViewを実装していて、適切なlistViewの再配置に苦労しています。サポートライブラリに問題があると思います。あるいは、使用できないonQueryTextChangedListenerのEditTextisteaの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 に答える