Reputation: 253
Fetching data from database into LisFragment. I need to use this ListFragment file content into MainActivity.java.
SecondActivity which extends ListFragment:
String DB = "TestDB";
String TABLE_NAME = "addcamera";
SQLiteDatabase sampleDB = null;
ArrayList<String> results = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleDB = SQLiteDatabase.openOrCreateDatabase(DB, null);
Cursor c = sampleDB.rawQuery("SELECT CameraName FROM " + TABLE_NAME , null);
if (c != null) {
if (c.moveToFirst()) {
do {
String CameraName = c.getString(c.getColumnIndex("CameraName"));
results.add(CameraName);
}while (c.moveToNext());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, results);
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.exercise.FragmentTest.SecondActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_list_fragment">
</fragment>
</LinearLayout>
Can anyone helpme?
Upvotes: 2
Views: 7134
Reputation: 6071
Well, first thing's first - your Activity
should not be extending ListFragment
, your own Fragment
class should be doing so. Hopefully I haven't misunderstood anything. :-)
However, you could easily see your ListFragment
derivative as a ListActivity
, ie you can use the getListView()
method to get the ListView
corresponding element (@android:id/list
) from your XML.
Seeing that you already have a Cursor
ready, you should consider using a SimpleCursorAdapter
and let it manage the cursor for you and *all* you need to do is to specify what fields to populate elements with.
Example:
getListView().setAdapter(
new SimpleCursorAdapter(
getActivity(),
R.layout.your_list_item_layout,
cursor,
new String[] { "database_table_1", "database_table_2" },
new int[] { R.id.element_1, R.id.element_2 }
)
);
Update:
Here's what your custom ListFragment could look like:
public class CustomListFragment extends ListFragment {
// Attributes
private Context mContext;
private LayoutInflater mLayoutInflater;
private SQLiteDatabase mSampleDb;
private List<String> mResults;
private Cursor mCursor;
// Elements
private ListView mListView;
private SimpleCursorAdapter mListAdapter;
// Constants
private final String DB = "TestDB";
private final String TABLE_NAME = "addcamera";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set our attributes
mContext = getActivity();
mLayoutInflater = inflater;
// Let's inflate & return the view
View view = mLayoutInflater.inflate(R.layout.your_fragment_layout, container, false);
// Get the database handler & the cursor
mSampleDb = SQLiteDatabase.openOrCreateDatabase(DB, null);
mCursor = mSampleDb.rawQuery("SELECT CameraName FROM " + TABLE_NAME , null);
// Init
init(view);
// Return
return view;
}
public void init(View v) {
// Setup the listAdapter
mListAdapter = new SimpleCursorAdapter(
mContext,
R.layout.your_list_item_layout,
cursor,
new String[] { "database_table_1", "database_table_2" },
new int[] { R.id.element_1, R.id.element_2 }
)
);
mListView.setAdapter(mListAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
Toast.makeText(mContext, "CLICKED ON POS #" + pos + "!", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 3