I have a question. 🙂
In an app that I am working on, in one part I have two tabs, both run by their respective fragment and a listview in each one.
Now, I need to populate those lists with the data I am reading in an SQLite database. I created almost all necessary classes for the DB part, but I am stuck on a part where I need to get this data
public List<String> getAllRockBands() {
List<String> bandList = new ArrayList<String>();
String selection = BAND_GENRE + "=1";
String orderBy = BAND_NAME+" ASC";
SQLiteDatabase db = sqLiteHelper.getReadableDatabase();
Cursor cursor = db.query(BANDDATABASE_TABLE,new String[]{BAND_NAME}, selection, null, null, null, orderBy);
int index = cursor.getColumnIndex(BAND_NAME);
if (cursor.moveToFirst()) {
do {
String bandName = cursor.getString(index);
bandList.add(bandName);
} while (cursor.moveToNext());
}
return bandList;
}
from my sqlhelper class to my listfragment extended class. The code for it is currently:
public class RockAllFragment extends SherlockListFragment {
private String[] bandList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_list, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getSherlockActivity(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, bandList));
}
}
Going with private BandDatabaseAdapter mySQLiteAdapter; and using the method through that works only for Activity extended classes and not ListFragment? What would be the best way to acomplish what I want to do? (P.S. I am pretty new to programming for Android, sorry if I made some trivial mistakes 🙂
Thanks,
Aleksa
At the end I found out it was a clumsy starter mistake, just going with
mySQLiteAdapter = new DatabaseAdapter(getSherlockActivity());atonViewCreatedand using an array adapter worked just fine.