Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8209041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:35:01+00:00 2026-06-07T09:35:01+00:00

Fetching data from database into LisFragment. I need to use this ListFragment file content

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:35:04+00:00Added an answer on June 7, 2026 at 9:35 am

    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();
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am fetching this data from database : $sql = SELECT * FROM articles
i am fetching data from database into table view in database table some roe
I am fetching the two column data from the database into the String Array
I am fetching the data from a database and I am appending it to
This is my php script for fetching data from a db. <?php mysql_connect(host,user,pass); mysql_select_db(db);
Should application expect the unexpected when fetching data from database? Lets say that we
I am fetching some data from database and want to show these values in
I have a function for fetching data from a database via a dataset public
While fetching Boolean data from database with hibernate SQL query gives the following error
I am fetching data from database and i am showing it in multiple select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.