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 9307495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:24:20+00:00 2026-06-19T00:24:20+00:00

i am create simple database and value of database i show to ListView, but

  • 0

i am create simple database and value of database i show to ListView, but my ListView Can’t show my database value

this is my code for create database

public class MyDb extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "MyDb.db";
       private SQLiteDatabase myDataBase; 
    public MyDb(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }


    public void createTable(SQLiteDatabase db){
        db.execSQL("DROP TABLE IF EXISTS users");
        db.execSQL("CREATE TABLE if not exists users (id_user INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR);");

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
    }
    public ArrayList<ArrayList<Object>> getAllRows(SQLiteDatabase db) {
        ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
        Cursor cur;

        final String ROW_ID = "id_user";
        final String ROW_NAME = "username";
        try {
            cur = db.query("users", new String[] { ROW_ID, ROW_NAME }, null, null, null, null, null);
            cur.moveToFirst();
            if (!cur.isAfterLast()) {
                do {
                    ArrayList<Object> dataList = new ArrayList<Object>();
                    dataList.add(cur.getString(0));
                    dataList.add(cur.getString(1));

                    dataArray.add(dataList);

                } while (cur.moveToNext());

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("DEBE ERROR", e.toString());
        }
        return dataArray;

    }
     public void insert(String table, ContentValues values) throws SQLException {
            myDataBase.insert(table, null, values);
        }

}

and this is my code for show value of database to ListView

public class DbScriptActivity extends Activity {

    private MyDb dbHelper;
    SQLiteDatabase db;
    Cursor customAdapter;
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db_script);
        dbHelper = new MyDb (this);
        db = dbHelper.getWritableDatabase();
        dbHelper.createTable(db);
        //dbHelper.openDataBase();
        //ListView list = (ListView)findViewById(R.id.list);
        list= (ListView)findViewById(R.id.list);
        getData();


        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            ContentValues values = new ContentValues();
            public void onClick(View v) {
                LayoutInflater inflater = DbScriptActivity.this.getLayoutInflater();
                final View v1 =inflater.inflate(R.layout.dialog , null) ;

                new AlertDialog.Builder(DbScriptActivity.this)
                    .setTitle("input your name:")
                    .setView(v1)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dlg, int sumthin) {
                            EditText name= (EditText)v1.findViewById(R.id.name);


                            String yourname = name.getText().toString();
                            values.put("username",yourname);

                           Log.v("aaaa", yourname);

                            db.insert("users",null,values);
                        }
                    })
                    .show();

            }
        }); 
    }

    private void getData() {

        String[] name = { "" };
        ArrayList<ArrayList<Object>> data = dbHelper.getAllRows(db) ;
        name = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {

            ArrayList<Object> row = data.get(i);
            name[i] = row.get(1).toString();

        }
        ArrayAdapter<String> aname = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, name);
        list.setAdapter(aname);

    }
}

this is my xml file to view database value into my ListView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DbScriptActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Register Name" />

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" >
    </ListView>

</RelativeLayout>

what is wrong my code?

  • 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-19T00:24:21+00:00Added an answer on June 19, 2026 at 12:24 am

    You can use SimpleCursorAdapter to load the data into the ListView. here is Good tutorial.

    http://www.vogella.com/articles/AndroidSQLite/article.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this should be simple but i can't work out how to
I need some help with this one.... I have this simple model: public class
I am getting strange errors while trying to create a simple database using isql
i am newbie of c# and win phone 7 i create a simple database
I need to create a simple mySQL database with a field for an image.
I'm using C# to create simple application that will check the database repeatedly after
I need to create a simple application that would generate reports based on database
I want to create and maintain a relatively simple relational database in my java
I'm trying to create a simple button that deletes a row from the database.
Hello, i'm trying to take detailed information from the database, i create a simple

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.