I have an app which does the following: an Activity with a ListView displays three items: Beginning, Intermediate, Advanced. Depending on the user's choice, the app should go to a Fragment with a ListView which displays all items from the underlying SQLiteDatabase where Level=Beginning or Intermediate or Advanced. This second step is not working for me. I've verified through the debugger that I have data in my database, so that's not the problem. Here's my code in the initial Activity - this is from the onItemClick method:

final String[] values = new String[] {

"Beginning", "Intermediate", "Advanced"

};

public void onItemClick(AdapterView> parent, View view, int position,

long id) {

Intent choice = new Intent(getApplicationContext(), com.MyKnitCards.project.StitchList.class);

Bundle dataBundle = new Bundle();

String chosenValue = values[position];

dataBundle.putString("Level",chosenValue);

choice.putExtras(dataBundle);

try {

startActivity(choice);

}

}

Here's the StitchList class, which is the Fragment class, and which is referenced in the code above:

public class StitchList extends FragmentActivity {

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.stitchfragment);

}

}

Here's the XML file for the Fragment, called stitchfragment.xml:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:layout_width="200dp"

android:layout_height="match_parent"

android:layout_marginTop="?android:attr/actionBarSize"

class="com.MyKnitCards.project.ListFrag" />

android:layout_width="match_parent"

android:layout_height="match_parent"

class="com.MyKnitCards.project.DetailFrag" />

Here's the ListFrag class. I'm using the Support Library and a CursorLoader with a ContentProvider, called StitchProvider; the SQLiteDatabase is called SQLData:

public class ListFrag extends ListFragment implements LoaderManager.LoaderCallbacks {

private static final String STITCHTABLE_BASEPATH = "MyStitches_tbl";

private static final String AUTHORITY = "com.MyKnitCards.project.SQLData";

public static final Uri STITCHES_URI = Uri.parse("content://" + AUTHORITY + "/" + STITCHTABLE_BASEPATH);

private static final String[] PROJECTION = new String[] { "_id", "stitchname" };

private SimpleCursorAdapter mAdapter;

private static final int LOADER_ID = 0;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

Intent myData = getActivity().getIntent();

Bundle info = myData.getExtras();

String[] dataColumns = { "stitchname", "_id" };

int[] viewIDs = { R.id.frag_stitchlist };

mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, null, dataColumns, viewIDs, 0);

setListAdapter(mAdapter);

getLoaderManager().initLoader(0, info, (LoaderCallbacks) this);

}

public void onListItemClick(ListView l, View v, int position, long id) {

String item = (String) getListAdapter().getItem(position);

DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);

if (frag != null && frag.isInLayout()) {

frag.setText(item);

}

}

public Loader onCreateLoader(int id, Bundle args) {

String selection = "stitchlevel='" + args.getString("Level") + "'";

return (Loader) new CursorLoader(getActivity(), STITCHES_URI,

PROJECTION, selection, null, null);

}

public void onLoadFinished(Loader loader, Cursor cursor) {

switch (loader.getId()) {

case LOADER_ID:

mAdapter.swapCursor((android.database.Cursor) cursor);

break;

}

}

public void onLoaderReset(Loader loader) {

mAdapter.swapCursor(null);

}

}

The onListItemClick method is for displaying data in the details Fragment, but I'm never getting this far. The list of items retrieved from the database based on the data in the Intent is never displayed.

Some possible issues/questions come to mind:

1) Is the syntax of my query in the onCreateLoader method correct? It should read, "stitchlevel='Beginning'", for example. I know the data in args.getString("Level") is valid because I've followed it through in the debugger, but is the syntax correct here? I'm assuming it should be standard SQL syntax, but...

2) In the assignment of value to viewIDs in the Fragment's onActivityCreated method, am I pointing to the correct place?

3) Similarly, in the next line, the SimpleCursorAdapter, is android.R.layout.simple_list_item_1 correct here? Or should it perhaps be R.layout.stitchfragment?

4) I put a breakpoint on the ContentProvider's query method, I never hit the breakpoint. Shouldn't I when the CursorLoader gets created? Furthermore, if I put a breakpoint on the List Fragment's onLoadFinished method, the cursor is null. What am I missing

5) I was told that dataColumns in the Fragment's onActivityCreated method should contain my table's _id column. Is that true? It seems odd to me, since the list will only display one piece of information: the stitchname. However, whether _id is included or not here, the Fragment list does not display anything

6) Is it proper for me to pass the Bundle, info, to the CursorLoader through getLoaderManager.initLoader, as I do in the Fragment's onActivityCreatedMethod?

7) I've seen examples of setListAdapter being called before getLoaderManager().initLoader() in the Fragment's onActivityCreated method and I've seen examples where setListAdapter is called after. Which is correct? Neither seems to help me....

I have been able to get a Fragment to display if I simply do this:

Intent myData = getActivity().getIntent();

Bundle info = myData.getExtras();

String level = info.getString("Level");

String[] values = new String[] {level, level};

ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, values);

setListAdapter(adapter);

So I know I can display data in the Fragment; it just doesn't seem to be working with the CursorLoader and ContentProvider. I hope this isn't information overload; my apologies if it is. If anyone has any ideas, I'd be most grateful.

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐