操作栏中的搜索功能不起作用

时间:2014-08-28 16:52:44

标签: android listview search

我正在尝试制作搜索栏并搜索列表视图项。 这是行不通的。 问题:当我输入查询并按确定/输入时,我的TrickPage(活动)不会被触发。

我正在开发基于官方教程的搜索小部件界面:

TrickPage(MainActivity)

public class TrickPage extends Activity implements OnQueryTextListener {

ListView listViewArticles;
String address;
String title;

    @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.search_menu, menu);
  SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
 }


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_trick);
    getActionBar().setDisplayHomeAsUpEnabled(true);


    /*
     * Data can be from your database. You have to know the number of list
     * items in advanced, in our example, we have 9
     */
    Folder[] folderData = new Folder[9];

    /*
     * @always start with index 0
     * 
     * @9 items in our list view.
     */
    folderData[0] = new Folder(R.drawable.icon_music_folder, "First Tut", "Trick number 2") ;

    folderData[1] = new Folder(R.drawable.icon_pictures_folder,
            "Second Tut", "Trick number 2");


    folderData[2] = new Folder(R.drawable.icon_spreadsheet_folder,
            "Third Tut", "Trick number 3");

    folderData[3] = new Folder(R.drawable.icon_music_folder, "FM Static",
            "One of my most favorite bands.");

    folderData[4] = new Folder(R.drawable.icon_pictures_folder,
            "Outing 2012", "Pictures in Boracay Island.");

    folderData[5] = new Folder(R.drawable.icon_spreadsheet_folder,
            "Business Spreadsheet", "Business accounts record.");

    folderData[6] = new Folder(R.drawable.icon_music_folder, "New Artists",
            "Cool songs by new artists.");

    folderData[7] = new Folder(R.drawable.icon_pictures_folder,
            "Anniversary 2012", "Company party.");

    folderData[8] = new Folder(R.drawable.icon_spreadsheet_folder,
            "Credit Spreadsheet", "Credit records every month.");

    // Pass the folderData to our ListView adapter
    FolderAdapter adapter = new FolderAdapter(this,R.layout.listview_item_row, folderData);

    // Set the adapter to our ListView
    listViewArticles = (ListView) findViewById(R.id.listView1);
    listViewArticles.setAdapter(adapter);

    /*
     * ListView item click listener. So we'll have the do stuff on click of
     * our ListItem
     */
    listViewArticles.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            switch (position) {
            case 0:
             Intent newActivity0 = new Intent(TrickPage.this,Trick_List_Article.class);     
             startActivity(newActivity0);
            break;
            case 1:
                address="file:///android_asset/test/res2/blog.html";
                 Intent i= new Intent(TrickPage.this,Trick_List_Article.class);  
                 i.putExtra("path", address);
                 startActivity(i);
            break;
            case 2:
                Intent newActivity2 = new Intent(TrickPage.this,TutorialsPage.class);     
                startActivity(newActivity2);;
            break;
            case 3:
             Intent newActivity3 = new Intent(TrickPage.this,TutorialsPage.class);     
             startActivity(newActivity3);
            break;
            default:
              // Nothing do!
          }

            // get the clicked folder name
            String listItemText = ((TextView) view
                    .findViewById(R.id.textViewFolderName)).getText()
                    .toString();

            // just toast it
            Toast.makeText(TrickPage.this,
                    "You clicked: " + listItemText, Toast.LENGTH_LONG)
                    .show();

        }
    });

    // Button HOME
    ImageButton ImageButton_home  = (ImageButton) findViewById(R.id.ImageButton_home);
    ImageButton_home.setOnClickListener(new View.OnClickListener() {

        @Override
           public void onClick(View v) {
            Intent intent0 = new Intent();
            intent0.setClass(TrickPage.this, Main.class);
            startActivity(intent0);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
           }
        });
    //Button Previous
    ImageButton ImageButton_previus  = (ImageButton) findViewById(R.id.ImageButton_previus);
    ImageButton_previus.setOnClickListener(new View.OnClickListener() {

        @Override
           public void onClick(View v) {
            //Closing SecondScreen Activity
                    finish();
           }
        });


}


 @Override
 public boolean onQueryTextChange(String newText)
 {
  // this is your adapter that will be filtered

      if (TextUtils.isEmpty(newText))
      {
          listViewArticles.clearTextFilter();
        }
      else
      {
          listViewArticles.setFilterText(newText.toString());
        }

      return true;
 }

 @Override
 public boolean onQueryTextSubmit(String query) {
  // TODO Auto-generated method stub
  return false;
 }

FolderAdapter

public class FolderAdapter extends ArrayAdapter<Folder> {

Context mContext;
int layoutResourceId;
Folder data[] = null;

/*
 * @mContext - app context
 * 
 * @layoutResourceId - the listview_item_row.xml
 * 
 * @data - the ListItem data
 */
public FolderAdapter(Context mContext, int layoutResourceId, Folder[] data) {

    super(mContext, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.data = data;
}

/*
 * @We'll overried the getView method which is called for every ListItem we
 * have.
 * 
 * @There are lots of different caching techniques for Android ListView to
 * achieve better performace especially if you are going to have a very long
 * ListView.
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItem = convertView;

    // inflate the listview_item_row.xml parent
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    listItem = inflater.inflate(layoutResourceId, parent, false);

    // get the elements in the layout
    ImageView imageViewFolderIcon = (ImageView) listItem
            .findViewById(R.id.imageViewFolderIcon);

    TextView textViewFolderName = (TextView) listItem
            .findViewById(R.id.textViewFolderName);

    TextView textViewFolderDescription = (TextView) listItem
            .findViewById(R.id.textViewFolderDescription);

    /*
     * Set the data for the list item. You can also set tags here if you
     * want.
     */
    Folder folder = data[position];

    imageViewFolderIcon.setImageResource(folder.folderIcon);
    textViewFolderName.setText(folder.folderName);
    textViewFolderDescription.setText(folder.folderDescription);

    return listItem;
}

}

班级文件夹

public class Folder {

public int folderIcon;
public String folderName;
public String folderDescription;

// Constructor.
public Folder(int folderIcon, String folderName,
        String folderDescription) {

    this.folderIcon = folderIcon;
    this.folderName = folderName;
    this.folderDescription = folderDescription;
}
}

清单:

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme" android:logo="@drawable/ic_launcher" android:restoreAnyVersion="false">



    <activity
        android:name="com.meygraph.illustratortutorials.SplashScreen"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar"
        android:windowSoftInputMode="stateHidden" >

        <meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable">
</meta-data>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.meygraph.illustratortutorials.Main" />

        </intent-filter>


      </activity>

请帮助我!!!!

2 个答案:

答案 0 :(得分:0)

考虑SplashScreen是您希望搜索小部件调用的活动,请将其添加到应用标记内:

<meta-data android:name="android.app.default_searchable"
           android:value="com.meygraph.illustratortutorials.SplashScreen" />

答案 1 :(得分:0)

我想您在添加适配器后忘记添加此调用:

listViewArticles.setTextFilterEnabled(true);

我希望这可以帮到你;)