I have a spinner which is populated using a CursorLoader. It gets populated correctly. However spinner.getAdapter().getCount() keeps returning 0. why is that :(
My spinner class is
public class CategorySpinner extends Spinner implements LoaderManager.LoaderCallbacks<Cursor> {
Context myContext;
private SimpleCursorAdapter adapter;
public CategorySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
myContext=context;
}
public void fillData(int projectId) {
String[] from = new String[] { Category.CATEGORYNAME };
int[] to = new int[] { R.id.label1 };
int layout = R.layout.category_list_item;
Bundle bundle=new Bundle();
bundle.putInt("ID", projectId);
EditExpenseActivity mActivity=(EditExpenseActivity)myContext;
mActivity.getLoaderManager().initLoader(0, bundle,this);
adapter = new SimpleCursorAdapter(myContext, layout, null, from, to, 0);
adapter.setDropDownViewResource(R.layout.category_list_item);
this.setAdapter(adapter);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { Category.FULL_ID,Category.CATEGORYNAME };
Uri uri = Uri.parse(MyContentProvider.CATEGORY_LIST_PATH +args.getInt("ID"));
CursorLoader cursorLoader = new CursorLoader(myContext, uri, projection,null, null, null);
return cursorLoader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}
}
The activity that is calling the spinner is EditExpenseActivity
public class EditExpenseActivity extends Activity {
private Context myContext;
private CategorySpinner sp_Category;
private Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_expense);
intent=getIntent();
final int mProjectId=intent.getIntExtra("ID",0);
myContext = this;
sp_Category = (CategorySpinner) findViewById(R.id.input_category);
//fill spinner with categories of the project with Id mProjectId
sp_Category.fillData(mProjectId);
//so far so good .. spinner is populated perfectly
int i=sp_Category.getAdapter().getCount();
}
}
Debug shows i value to be 0. why???
Related
I have strange problem with my android app. I have some data and I saved that data in SQLite Database. And in this fragment I try to read my data from table using SimpleCursorAdapter
public class LogFragment extends Fragment{
private static SQLiteDatabase db;
private static SQLiteOpenHelper helper;
private static Context context;
private static ListView listView;
private static String senderOrReceiver;
private static SimpleCursorAdapter adapter;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
adapter = new SimpleCursorAdapter(context, R.layout.log_message, null,
new String[]{Constants.COMMAND, Constants.VALUE, Constants.TIME_STAMP, Constants.MESSAGE_ID, Constants.SESSION_ID, Constants.PARAMS},
new int[]{R.id.command, R.id.value, R.id.time_stamp, R.id.message_id, R.id.session_id, R.id.params}, 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_log, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
listView = (ListView) view.findViewById(R.id.listView);
}
#Override
public void onDestroy() {
super.onDestroyView();
if(db != null){
db.close();
}
}
public static void readFromDatabase(String sender){
senderOrReceiver = sender;
new DatabaseTalker().execute();
}
private static class DatabaseTalker extends AsyncTask <Void, Void, Cursor>{
#Override
protected Cursor doInBackground(Void... params) {
helper = new Database(context);
db = helper.getReadableDatabase();
return db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
}
#Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
adapter.changeCursor(cursor);
listView.setAdapter(adapter);
}
}
}
and here's what I got in my ListView . I have six fields (Command, Value, Time Stamp, MessageID, SessionID, Params) and as you can see only one field is filled (for example) Command: On, Value: , Time Stamp: , MessageID: , SessionID: , Params: . and so on... Why I get this result?
EDIT:
Here how I write my data to database
public void addInfo(Information info){
SQLiteDatabase db = this.getWritableDatabase();
addToTable(db, Constants.COMMAND, info.getCommand());
addToTable(db, Constants.VALUE, info.getValue());
addToTable(db, Constants.TIME_STAMP, info.getTimeStamp());
addToTable(db, Constants.MESSAGE_ID, info.getMessageID());
addToTable(db, Constants.SESSION_ID, info.getSessionID());
addToTable(db, Constants.PARAMS, info.getParams());
db.close();
}
private static void addToTable(SQLiteDatabase db, final String TAG, String value){
ContentValues values = new ContentValues();
values.put(TAG, value);
db.insert(Constants.TABLE_NAME, null, values);
}
Your each addToTable() call inserts a new row that contains just one column value.
To insert a row with all the values, add the values to the same ContentValues and call insert() once.
I have created an ExpandableListAdapter by extending SimpleCursorTreeAdapter. The cursors are managed by loaders. When the list is displayed to user , I start a background service to fetch latest data from server. If the server returns new data I add it to DB and notify the children cursors. The cursors gets requeried and the list updates. At this point if the user has scrolled down in the list, the list scrolls up to top. This is very annoying. I have gone through the entire API for *TreeAdapters and do not see any method to prevent it. This must be a very common problem. How can I fix it ?
Try this code:
public class GroupsAdapter extends SimpleCursorTreeAdapter {
private final String TAG = getClass().getSimpleName().toString();
private final FragmentActivity mActivity;
private final ContactsFragment mFragment;
private static final String[] CONTACTS_PROJECTION = new String[] {
ContactsContract.Users._ID, ContactsContract.Users.USER_ID,
ContactsContract.Users.NAME, ContactsContract.Users.STATUS_TYPE,
ContactsContract.Users.STATUS_MESSAGE,
ContactsContract.Users.HAS_ALERT };
// Note that the constructor does not take a Cursor. This is done to avoid
// querying the database on the main thread.
public GroupsAdapter(final Context context, final ContactsFragment glf,
final int groupLayout, final int childLayout,
final String[] groupFrom, final int[] groupTo,
final String[] childrenFrom, final int[] childrenTo) {
super(context, null, groupLayout, groupFrom, groupTo, childLayout,
childrenFrom, childrenTo);
mActivity = (FragmentActivity) context;
mFragment = glf;
}
#Override
protected Cursor getChildrenCursor(final Cursor groupCursor) {
final String id = groupCursor.getString(groupCursor
.getColumnIndex(ContactsContract.Groups.GROUP_ID));
final CursorLoader cursorLoader = new CursorLoader(mActivity,
ContactsContract.Users.CONTENT_URI, CONTACTS_PROJECTION, "("
+ ContactsContract.UserGroupColumns.GROUP_ID + "=?)",
new String[] { id }, null);
Cursor childCursor = null;
try {
childCursor = cursorLoader.loadInBackground();
childCursor.moveToFirst();
} catch (final Exception e) {
Log.e(TAG, e.getMessage());
}
return childCursor;
}
}
and the fragment:
public class ContactsFragment extends Fragment implements
LoaderCallbacks<Cursor> {
private static final String[] GROUPS_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.NAME,
ContactsContract.Groups.GROUP_ID,
ContactsContract.Groups.USERS_COUNT };
private static final String TAG = "ContactsFragment";
ExpandableListView listView;
GroupsAdapter mAdapter;
public ContactsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container, final Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_contacts, container, false);
}
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.contacts_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ExpandableListView) getView().findViewById(
android.R.id.list);
// listView.setEmptyView();
// Set up our adapter
mAdapter = new GroupsAdapter(getActivity(), R.layout.group,
R.layout.user, new String[] { ContactsContract.Groups.NAME,
ContactsContract.Groups.USERS_COUNT }, // Name
// for group layouts
new int[] { R.id.group, R.id.count }, new String[] {
ContactsContract.Users.NAME,
ContactsContract.Users.STATUS_MESSAGE }, // Name
// for child layouts
new int[] { R.id.user_name, R.id.status_message });
listView.setAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
final Loader<Cursor> loader = getLoaderManager().getLoader(-1);
if (loader != null && !loader.isReset()) {
getLoaderManager().restartLoader(-1, null, this);
} else {
getLoaderManager().initLoader(-1, null, this);
}
getActivity().getContentResolver().registerContentObserver(
ContactsContract.Users.CONTENT_URI, false,
new ContentObserver(null) {
#Override
public void onChange(final boolean selfChange) {
Log.w(TAG, "Change");
}
});
}
#Override
public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
// This is called when a new Loader needs to be created.
// group cursor
final CursorLoader cl = new CursorLoader(getActivity(),
ContactsContract.Groups.CONTENT_URI, GROUPS_PROJECTION, null,
null, null);
return cl;
}
#Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
// Swap the new cursor in.
final int id = loader.getId();
if (id == -1) {
mAdapter.setGroupCursor(cursor);
}
}
#Override
public void onLoaderReset(final Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// is about to be closed.
final int id = loader.getId();
if (id != -1) {
// child cursor
try {
mAdapter.setChildrenCursor(id, null);
} catch (final NullPointerException e) {
Log.w("TAG", "Adapter expired, try again on the next query: "
+ e.getMessage());
}
} else {
mAdapter.setGroupCursor(null);
}
}
}
I try to populate the suggestions list with the data of a db table. However I get StaleDataExceptions. It throws quite randomly, but always when I enter a character into the textview.
Here is my code:
CursorLoader extending Cristian's SimpleCursorLoader class
public class TagCursorLoader extends SimpleCursorLoader {
private String mSelection;
private TagDbLoader mDbLoader;
public TagCursorLoader(Context context, TagDbLoader dBLoader, String selection) {
super(context);
this.mDbLoader = dBLoader;
this.mSelection = selection;
}
#Override
public Cursor loadInBackground() {
return mDbLoader.fetchContainingString(mSelection);
}
}
The Loader callbacks:
public class TagCursorLoaderCallback implements LoaderCallbacks<Cursor>, CursorToStringConverter {
private Context mContext;
private TagDbLoader mdDbLoader;
private SimpleCursorAdapter mAdapter;
private String mSelection;
public TagCursorLoaderCallback(Context context, TagDbLoader dBLoader, SimpleCursorAdapter adapter) {
this.mContext = context;
this.mdDbLoader = dBLoader;
mAdapter = adapter;
mSelection = "";
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new TagCursorLoader(mContext, mdDbLoader, mSelection);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (!data.isClosed()) {
mAdapter.swapCursor(data);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
public void setSelection(String mSelection) {
this.mSelection = mSelection;
}
#Override
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(cursor.getColumnIndexOrThrow(DbConstants.Tags.KEY_TAG));
}
}
And finally when I set up the AutoCompleteTextView:
private void initializeAutoComplete() {
mTagDbLoader = new TagDbLoader(getActivity());
mTagDbLoader.open();
mTagInput = (AutoCompleteTextView) mLayout.findViewById(R.id.autoComplete);
mTagInput.addTextChangedListener(new TextWatcherAdapter() {
#Override
public void afterTextChanged(Editable s) {
mLoaderCallback.setSelection(s.toString());
getLoaderManager().restartLoader(0, null, mLoaderCallback);
}
});
mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1,
null, new String[] { DbConstants.Tags.KEY_TAG }, new int[] { android.R.id.text1 },
0);
mLoaderCallback = new TagCursorLoaderCallback(getActivity(), mTagDbLoader, mAdapter);
mAdapter.setCursorToStringConverter(mLoaderCallback);
mTagInput.setAdapter(mAdapter);
getLoaderManager().initLoader(0, null, mLoaderCallback);
}
After some investigation, it seems that SimpleCursorAdapter inherits from ResourceCursorAdapter, which inherits from CursorAdapter. CursorAdapter uses CursorFilter for filtering, and this class calls changeCursor() in its publishResults(). changeCursor closes the old cursor... So that's why my cursors were closed automatically.
I dropped the loaders, and changed the implementation to the code below, and it works greatly:
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
mTagDbLoader.fetchAll(), new String[] { DbConstants.Tags.KEY_TAG },
new int[] { android.R.id.text1 }, 0);
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
#Override
public Cursor runQuery(CharSequence constraint) {
if (constraint == null || constraint.equals(""))
return mAdapter.getCursor();
return mTagDbLoader.fetchContainingString(constraint.toString());
}
});
mAdapter.setCursorToStringConverter(new CursorToStringConverter() {
#Override
public CharSequence convertToString(Cursor c) {
return c.getString(c.getColumnIndexOrThrow(DbConstants.Tags.KEY_TAG));
}
});
I have an app with a fairly standard fragment layout. An expandable listview fragment on the left and a panel on the right that is used for different things depending on what the user chooses to do with the list on the left (displaying data, adding new data, etc).
I'm using the LoaderManager (first time using loaders) with CommonWare's loaderex library as I have no need or desire to create a Content Provider for my database just so I can use a standard CursorLoader. This setup works great for displaying my list.
The issue I am having is when I use the second fragment to add data to the database. I cannot figure out how to trigger a re-load of the list in the first fragment. For the life of me I cannot figure out how to grab the loader from the first fragment in the second so that it will be aware that the data needs to be pulled again, nor can I seem to figure how to manually trigger a re-load.
As this is my first attempt at using Loaders, if I'm doing something improperly I'd be happy to be (gently) re-directed down a better path.
Fragment 1
public class StudentListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private TAOpenHelper mDbHelper = null;
private MyExpandableListAdapter mAdapter = null;
private ExpandableListView lv = null;
private Button addStudentButton;
public static long mRowId = 0;
public SQLiteCursorLoader studentLoader=null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.leftlistfragment_entry, container,
false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addStudentButton = (Button) getActivity().findViewById(R.id.AddButton);
addStudentButton.setText(getResources().getString(
R.string.button_add_student));
addStudentButton.setOnClickListener(addStudentButtonHandler);
lv = (ExpandableListView) getListView();
mDbHelper = TAOpenHelper.getInstance(getActivity());
fillData();
getLoaderManager().getLoader(-1);
if (studentLoader != null && !studentLoader.isReset()) {
getLoaderManager().restartLoader(-1, null, this);
} else {
getLoaderManager().initLoader(-1, null, this);
}
}
private void fillData() {
mAdapter = new MyExpandableListAdapter(getActivity(), this,
R.layout.listlayout_exp_double_group,
R.layout.listlayout_exp_double_child,
new String[] { TeacherAidDB.STUDENT_FIRST,
TeacherAidDB.STUDENT_LAST }, new int[] {
R.id.ListItem1, R.id.ListItem2 }, new String[] {
TeacherAidDB.CLASS_NAME, TeacherAidDB.CLASS_LEVEL },
new int[] { R.id.ListItem1, R.id.ListItem2 });
lv.setAdapter(mAdapter);
}
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
protected final SparseIntArray mGroupMap;
private StudentListFragment mFragment;
public MyExpandableListAdapter(Context context,
StudentListFragment clf, int groupLayout, int childLayout,
String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, null, groupLayout, groupFrom, groupTo, childLayout,
childrenFrom, childrenTo);
mFragment = clf;
mGroupMap = new SparseIntArray();
}
#Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
int groupPos = groupCursor.getPosition();
int groupId = groupCursor.getInt(groupCursor
.getColumnIndex(TeacherAidDB.CLASS_ROWID));
mGroupMap.put(groupId, groupPos);
Loader<Cursor> loader = getActivity().getLoaderManager().getLoader(
groupId);
if (loader != null && !loader.isReset()) {
getActivity().getLoaderManager().restartLoader(groupId, null,
mFragment);
} else {
getActivity().getLoaderManager().initLoader(groupId, null,
mFragment);
}
return null;
}
public SparseIntArray getGroupMap() {
return mGroupMap;
}
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != -1) { // Child Cursor
studentLoader = new SQLiteCursorLoader(getActivity(), mDbHelper,
TeacherAidDB.STUDENT_LIST_CLASS_QUERY + id, null);
} else { // Group Cursor
studentLoader = new SQLiteCursorLoader(getActivity(), mDbHelper,
TeacherAidDB.STUDENT_LIST_QUERY, null);
}
return studentLoader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
int id = loader.getId();
if (id != -1) { // Child cursor
if (!data.isClosed()) {
SparseIntArray groupMap = mAdapter.getGroupMap();
int groupPos = groupMap.get(id);
mAdapter.setChildrenCursor(groupPos, data);
}
} else { // Groups cursor
mAdapter.setGroupCursor(data);
}
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.changeCursor(null);
}
View.OnClickListener addStudentButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
AddPerson personadd = AddPerson.newInstance(AddPerson.STUDENT, AddPerson.CREATE, mRowId);
getFragmentManager().beginTransaction()
.replace(R.id.rightpane, personadd).commit();
}
};
}
Fragment 2
public class AddPerson extends Fragment {
public static int STUDENT = 0;
public static int TEACHER = 1;
public static int CREATE = 0;
public static int EDIT = 1;
private int mRowId;
private TAOpenHelper mDbHelper;
private Cursor personedit;
private Button commit;
private Button cancel;
int who;
int what;
long rowId;
static AddPerson newInstance(int type, int action, long rowid) {
AddPerson f = new AddPerson();
Bundle args = new Bundle();
args.putInt("type", type);
args.putInt("action", action);
args.putLong("rowid", rowid);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
who = getArguments().getInt("type");
what = getArguments().getInt("action");
rowId = getArguments().getInt("rowid");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_person_add, container, false);
mDbHelper = TAOpenHelper.getInstance(getActivity());
if (what == EDIT) {
if (who == STUDENT) {
// Student Edit stuff here
} else {
// Teacher Edit stuff here
}
} else {
if (who == STUDENT) {
// Student Create stuff here
} else {
// Teacher Create stuff here
}
}
// Code to gather data from user goes here
commit = (Button) v.findViewById(R.id.commitbutton);
commit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
first = firstTxt.getText().toString();
last = lastTxt.getText().toString();
street = streetTxt.getText().toString();
city = cityTxt.getText().toString();
zip = zipTxt.getText().toString();
phone = phoneTxt.getText().toString();
email = emailTxt.getText().toString();
if (what == CREATE) {
processAdd(who);
} else {
processUpdate(who);
}
}
});
cancel = (Button) v.findViewById(R.id.cancelbutton);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Fragment check = getFragmentManager().findFragmentById(
R.id.rightpane);
getFragmentManager().beginTransaction().remove(check).commit();
}
});
return v;
}
private void processAdd(int who) {
ContentValues initialValues = new ContentValues();
if (who == STUDENT) {
initialValues.put(TeacherAidDB.STUDENT_FIRST, first);
initialValues.put(TeacherAidDB.STUDENT_LAST, last);
initialValues.put(TeacherAidDB.STUDENT_STREET, street);
initialValues.put(TeacherAidDB.STUDENT_CITY, city);
initialValues.put(TeacherAidDB.STUDENT_STATE, state);
initialValues.put(TeacherAidDB.STUDENT_ZIP, zip);
initialValues.put(TeacherAidDB.STUDENT_PHONE, phone);
initialValues.put(TeacherAidDB.STUDENT_EMAIL, email);
initialValues.put(TeacherAidDB.STUDENT_BDAY, birthday);
// How to get studentLoader from fragment 1?
//studentLoader.insert(TeacherAidDB.STUDENT_TABLE, null, initialValues);
}
}
}
With a regular CursorLoader, this would happen automagically via the ContentObserver framework, which eventually boils down to a bunch of static data members.
With SQLiteCursorLoader, ContentObserver is not available, with the closest simulacrum being to route your CRUD operations through the Loader so it knows to reload the Cursor. And that is really only designed for use within a single activity.
So, as Luksprog suggested, your best option is to delegate CRUD work to the containing activity.
If these fragments might be hosted by disparate activities (e.g., for small/normal vs. large/xlarge screen sizes), define a common interface for handling this work, and have the fragments delegate to the interface.
My goal is to swipe lists horizontally , which are filled by public class ClientsManagerOpenHandler extends SQLiteOpenHelper.
I try to reach this goal by working with viewpager and ListFragments. If you have an other solution please tell me.
Now the problem:
If I try to call from the following PageListFragment.java, data from ClientsManagerOpenHandler the program crashes at:
dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");
Maybe I cannot call an extended SQLiteOpenHelper within ListFragment? But how I get the data from sqlite into my lists, and when I swipe horizontally to change data...
Please help. I have tried anything, but I really need help now.
public class PageListFragment extends ListFragment implements OnClickListener,
LoaderCallbacks<Cursor> {
private Calendar cal = Calendar.getInstance();
private ClientsManagerOpenHandler openHandler;
public static final String PREFS_NAME ="MyPrefsFile";
SharedPreferences prefs;`
private Cursor dbCursorTerminAnsicht;
private Integer intVerdienst = 0;
private String queryVerdienst;
private SimpleCursorAdapter mCursorAdapter;
private ListView listViewTermine;
private final int listNr;
private final String[] fruit = { "Bananen", "Apfle", "Erdbeere",
"Kirschen", "Mangos" };
private Uri[] mMediaSource = {null, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI};
public PageListFragment(int nr) {
this.listNr = nr;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//!!!!! after the next line the program crashes. Even I have set breakpoints at ClientsManagerOpenHandler
//I cannot see anything in the debugger (Debugger: Source not found...)
dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");
if (listNr == 0) {
ArrayAdapter<String> openHandler = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, fruit);
setListAdapter(openHandler);
} else if (listNr == 1) {
mCursorAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { MediaStore.Audio.Artists.ARTIST },
new int[] { android.R.id.text1 }, 0);
setListAdapter(mCursorAdapter);
getLoaderManager().initLoader(0, null, this);
} else if (listNr == 2) {
openHandler = new ClientsManagerOpenHandler(getActivity());
String query = "projekte, klienten, termine WHERE termine.KLIENTID = klienten._id AND termine.PROJEKTID = projekte._id ORDER BY BEGINN ASC;";
MyDataAdapter myClientsadapter = new MyDataAdapter (
getActivity(),
R.layout.terminzeile,
// android.R.layout.two_line_list_item,
dbCursorTerminAnsicht,
new String[] { openHandler.BEGINN , openHandler.ENDE, openHandler.NACHNAME, openHandler.VORNAME, openHandler.PROJEKT, openHandler.BEZAHLT},
// fields,
// new int[] {R.id.editTextNachname, R.id.editTextVorname }
new int[] {R.id.textViewBeginn, R.id.textViewEnde, R.id.textViewNachname, R.id.textViewVorname, R.id.textViewProjekt,R.id.checkBoxBezahlt }
);
myClientsadapter.setViewBinder(new MyDataAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 13) {
String strBeginn = cursor.getString(columnIndex);
CheckBox cb = (CheckBox) view;
int intbezahlt = cursor.getInt(13);
int index = cursor.getColumnIndex("SATZ");
Integer intSatz = cursor.getInt(index);
if (index>0) {
if (intbezahlt>0){
intVerdienst = intVerdienst + intSatz;
}
}
cb.setChecked(intbezahlt > 0);
return true;
}
String str = cursor.getString(columnIndex);
return false;
}
});
//TerminlisteRefresh("");
setListAdapter(myClientsadapter);
getLoaderManager().initLoader(0, null, this);
}
}
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Loader<Cursor> loader = new CursorLoader(getActivity(), mMediaSource[listNr],
null, null, null, null);
return loader;
}
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> arg0) {
mCursorAdapter.swapCursor(null);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
It appears that you are trying to access openHandler before you have defined it:
dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");
...
openHandler = new ClientsManagerOpenHandler(getActivity());
This might work better:
openHandler = new ClientsManagerOpenHandler(getActivity());
dbCursorTerminAnsicht = openHandler.queryTabelle("terminansicht");