Android: Fetch data from database into ListFragment -


is bringing Lasprgament data from the database I need to use the content of this ListFragment file MainActivity.java < / P>

SecondActivity which applies ListFragment:.

  string DB = "TestDB"; String TABLE_NAME = "edekamera"; SQLiteDatabase sampleDB = null; ArrayList & LT; String & gt; Results = New Arreelist & lt; String & gt; (); Override public null on @reate (bundle saved instainstate) {super.naught (savedinstenstate); SampleDB = SQLiteDatabase.openOrCreateDatabase (DB, blank); Cursor c = sampleDB.rawQuery ("select cameraman FROM" + TABLE_NAME, empty); If (c! = Null) {if (c.moveToFirst ()) {do string {Name string = c.getString (c.getColumnIndex ("CameraName")); Results.add (CameraName); } While (c.moveToNext ()); }} ArrayAdapter & lt; String & gt; Adapter = new array adapter & lt; String & gt; (GetActivity (), android.r.layout.simple_list_item_1, results); }   

main.xml:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; LinearLayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" & gt; & Lt; Piece 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" & gt; & Lt; / Piece & gt; & Lt; / LinearLayout & gt;   

Can anyone help?

OK, first of the first thing - your activity to ListFragment should not be expanded, your own structure class should do this. Hopefully I have not had any misunderstanding. : -)

However, you can easily see as your ListFragment derived from a ListActivity , which means you getListView () < / Code> Method to get the ListView related element ( @android: id / list ) from your XML

Given that you have previously From a cursor is ready, you should consider using the SimpleCursorAdapter and it should consider the need for the cursor and * all * you * what to do To specify the elements to fill with the

example: ..

  getListView () setAdapter (New SimpleCursorAdapter (getActivity (), R.layout.your_list_item_layout, cursors, new String [] { "database_table_1", "Detabes_ table_2"}, new Int [] {R .dillement_1, rd.litm_2_2}));   

Update:

What it may seem like your custom ListFragment:

  public class CustomListFragment extends ListFragment {// Private Reference Properties mContext; Private LayoutInflator Malout Inflighter; Private SQLiteDatabase mSampleDb; Private listing & lt; String & gt; MResults; Private cursor amcursor; // Private list view of elements mListView; Personal Simple Adapter MLIF Adapter; // Static Private Final String DB = "TestDB"; Private end string TABLE_NAME = "edekamera"; @ Override Public View Crate View (LayoutEfter Influator, View Group Container, Bundle Saved InstantState) {// Set up our properties mContext = getActivity (); MLayoutInflater = inflater; // Let's move & amp; View View View = mLayoutInflater.inflate (R.layout.your_fragment_layout, container, incorrect); // Get database handler & amp; Cursor mSampleDb = SQLiteDatabase.openOrCreateDatabase (db, blank); MCursor = mSampleDb.rawQuery ("Select cameraman FROM" + TABLE_NAME, empty); // Init init (see); // Return withdrawal view; } Public void init (v View) {// setup listAdapter mListAdapter = new SimpleCursorAdapter (mContext, R.layout.your_list_item_layout, cursors, new String [] { "database_table_1", "database_table_2"}, new int [] {R Id. Element_1, r.id.element_2})); MListView.setAdapter (mListAdapter); } @Override Public Zero onListItemClick (ListView L, See V, Integer Status, Long ID) {Toast.makeText (mContext, + Status + # on status ", Toast.LENGTH_SHORT"! ") .show (); }}    

Comments