我希望你能帮助我。
我有一个包含ListFragment的MainActivity类,我正在尝试在MainActivity的Action Bar中构建一个搜索栏。
搜索栏在那里,但我不知道如何过滤ListFragment中的列表。
我将从MainActivity调用ListFragment中的filterList方法,但我不确定这是否正确。
以下是MainActivity的样子:
package dk.mmad.ma1;
public class MainActivity extends FragmentActivity {
private DAO daoAdapter;
private SimpleCursorAdapter cursorAdapter;
Fragment fragment;
public DAO getDAO() {
return daoAdapter;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
daoAdapter = new DAO(this);
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
FragmentManager fm = getSupportFragmentManager();
fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new BookListFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment).commit();
}
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// filter the list
return true;
case R.id.add_book:
Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onDestroy() {
super.onDestroy();
daoAdapter.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
}
这是ListFragment:
package dk.mmad.ma1;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import db.DAO;
import db.DAO.Books;
@SuppressLint("NewApi")
public class BookListFragment extends ListFragment {
private DAO daoAdapter;
private SimpleCursorAdapter cursorAdapter;
long currentId;
public void onResume() {
super.onResume();
refreshList();
}
public void filterList(String query) {
Cursor cursor = daoAdapter.getBooks(query);
cursorAdapter.changeCursor(cursor);
}
private void refreshList() {
Cursor cursor = daoAdapter.getBooks();
cursorAdapter.changeCursor(cursor);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
daoAdapter = ((MainActivity)getActivity()).getDAO();
daoAdapter.open();
daoAdapter.saveBook("testbook1", "author1", 123);
daoAdapter.saveBook("testbook2", "author3", 123);
daoAdapter.saveBook("testbook3", "author1", 123);
daoAdapter.saveBook("testbook4", "author3", 123);
daoAdapter.saveBook("testbook5", "author1", 123);
daoAdapter.saveBook("testbook6", "author3", 123);
daoAdapter.saveBook("testbook7", "author1", 123);
daoAdapter.saveBook("testbook8", "author3", 123);
daoAdapter.saveBook("testbook9", "author1", 123);
daoAdapter.saveBook("testbook10", "author3", 123);
daoAdapter.saveBook("testbook11", "author1", 123);
daoAdapter.saveBook("testbook13", "author3", 123);
daoAdapter.saveBook("testbook14", "author1", 123);
daoAdapter.saveBook("testbook15", "author3", 123);
Cursor cursor = daoAdapter.getBooks();
String[] fromColumns = { "title", "author" };
int[] toViews = { dk.mmad.ma1.R.id.titleTextView, dk.mmad.ma1.R.id.authorTextView };
cursorAdapter = new SimpleCursorAdapter(((MainActivity)getActivity()),
dk.mmad.ma1.R.layout.list_item_books, cursor, fromColumns,
toViews, 0);
setListAdapter(cursorAdapter);
//startManagingCursor(cursor);
}
/* @Override
public void onListItemClick(ListView l, View v, int position, long id) {
Long itemId = getListAdapter().getItemId(position);
Log.i("Testings", daoAdapter.getBookTitle(itemId));
//Intent intent = new Intent(getActivity(), BookDetailsActivity.class);
//intent.putExtra("", itemId);
//startActivity(intent);
}*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
currentId = id;
Cursor cursor = cursorAdapter.getCursor();
String idForIntent = cursor.getString(cursor.getColumnIndexOrThrow(Books.ID_COL));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String author = cursor.getString(cursor.getColumnIndexOrThrow("author"));
String pages = cursor.getString(cursor.getColumnIndexOrThrow("page_count"));
Intent intent = new Intent(getActivity().getApplicationContext(), BookDetailsActivity.class);
intent.putExtra("book", new String[] {idForIntent, title, author, pages});
startActivity(intent);
}
}
你能帮助我吗?