I hope you can help me.
I have a MainActivity class that contains a ListFragment, and I'm trying to create a search bar in the action bar in MainActivity.
There is a search string, but I do not know how to filter the list in ListFragment.
There is a filterList method in the ListFragment that I'm going to call from MainActivity, but I'm not sure if this is correct.
Here is what MainActivity looks like:
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();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
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);
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);
}
}
And here is the 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);
}
@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);
}
}
Can you help me?