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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:32:24+00:00 2026-06-12T22:32:24+00:00

Could you please tell me what’s wrong with this code. It runs but results

  • 0

Could you please tell me what’s wrong with this code. It runs but results are not listed. I just want a simple SQLiteDB running test to finalize my another project. I assume that something wrong with ArrayList use.

public class MainActivity extends Activity {

String names;
ListView lvMain;
ArrayList<String> values;
ArrayAdapter<String> adapter;

DBHelper dbHelper;
EditText et;
Button btnAdd;
Button btnRead;
Button btnClear;
Button btnShow;

Cursor c;
int nameColIndex;
int idColIndex;

/**
 * Called when the activity is first created.
 */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnRead = (Button) findViewById(R.id.btnRead);
    btnClear = (Button) findViewById(R.id.btnClear);
    btnShow = (Button) findViewById(R.id.btnShow);

    dbHelper = new DBHelper(this);
    lvMain = (ListView) findViewById(R.id.lvMain);
    values = new ArrayList<String>();

}

public void onButtonClick(View v) {

    ContentValues cv = new ContentValues();
    String name = et.getText().toString();
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    switch (v.getId()) {

        case R.id.btnAdd:
            cv.put("name", name);

            // вставляем запись и получаем ее ID
            long rowID = db.insert("mytable", null, cv);

            break;

        case R.id.btnRead:

            // делаем запрос всех данных из таблицы mytable, получаем Cursor 
            c = db.query("mytable", null, null, null, null, null, null);

            // ставим позицию курсора на первую строку выборки
            // если в выборке нет строк, вернется false
            if (c.moveToFirst()) {

                // определяем номера столбцов по имени в выборке
                idColIndex = c.getColumnIndex("id");
                nameColIndex = c.getColumnIndex("name");

                do {
                    // получаем значения по номерам столбцов

                    c.getInt(idColIndex);

                names = c.getString(nameColIndex);

                values.add(names);

                    // переход на следующую строку 
                    // а если следующей нет (текущая - последняя), то false - выходим из цикла
                } while (c.moveToNext());

            } else {
                c.close();
            }

            break;

        case R.id.btnClear:

            // удаляем все записи
            int clearCount = db.delete("mytable", null, null);

            break;

        case R.id.btnShow:
            break;
    }

// закрываем подключение к БД

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

    lvMain.setAdapter(adapter);


    dbHelper.close();     

    adapter.notifyDataSetChanged();

}

 class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {
  // конструктор суперкласса
  super(context, "myDB", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

  // создаем таблицу с полями
  db.execSQL("create table mytable ("
      + "id integer primary key autoincrement," 
      + "name text,"
      + "email text" + ");");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}
}

Here is main, may be I am missing something?

    `<?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"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp">
            </TextView>
            <EditText
                android:id="@+id/et"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <requestFocus>
                </requestFocus>
            </EditText>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/btnAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:text="Add">
            </Button>
            <Button
                android:id="@+id/btnRead"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:text="Read">
            </Button>
            <Button
                android:id="@+id/btnClear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:text="Clear">
            </Button>
            <Button
                android:id="@+id/btnShow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:text="Show">
            </Button>
        </LinearLayout>
         <ListView
            android:id="@+id/lvMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    </LinearLayout>`
  • 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-12T22:32:25+00:00Added an answer on June 12, 2026 at 10:32 pm
    values = new ArrayList<String>();
    

    values is null.

    Change:

    dbHelper = new DBHelper(this);
               lvMain = (ListView) findViewById(R.id.lvMain);
               values = new ArrayList<String>();
    
        }
    
        public void onButtonClick(View v) {
    
            ContentValues cv = new ContentValues();
            String name = et.getText().toString();
            SQLiteDatabase db = dbHelper.getWritableDatabase();
    
            switch (v.getId()) {
                case R.id.btnAdd:
                    cv.put("name", name);
                    long rowID = db.insert("mytable", null, cv);
                    break;
    
                case R.id.btnRead:
                    c = db.query("mytable", null, null, null, null, null, null);
                    if (c.moveToFirst()) {
                        idColIndex = c.getColumnIndex("id");
                        nameColIndex = c.getColumnIndex("name");
    
                        do {
                            c.getInt(idColIndex);
                        names = c.getString(nameColIndex);
                        values.add(names);
                        } while (c.moveToNext());
                    } else {
                        c.close();
                    }
                    break;
    
                case R.id.btnClear:
                    int clearCount = db.delete("mytable", null, null);
                    break;
    
                case R.id.btnShow:
                    break;
            }
    adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, values);
            lvMain.setAdapter(adapter);
        dbHelper.close();     
        adapter.notifyDataSetChanged(); 
    
    
    
    }
    

    }

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

Sidebar

Related Questions

Could someone please tell me whats wrong with this code because I cannot find
could anyone please tell me what is wrong this this code? I get a
could please anybody tell me what is wrong with this regexp ? ((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\:([0-9]{2,5}) for
Could someone please tell me what I am doing wrong? I'm not a newbie
could somebody please tell me why this code fails. url is nil after calling
Could someone please tell me why my code for the jquery autocomplete is not
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please tell me what I am doing wrong here. I have compiled
Could you please tell me what is the meaning of Could not find method
Could anyone please tell me why this happens, $a = 0.000022 echo $a //

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.