简体   繁体   English

如何在 Android 的 ListView 中显示来自 SQLite 数据库的多个数据

[英]How to show multiple data from SQLite database inside ListView in Android

I am a newcomer to Android development.我是Android开发的新手。

I can't figure out how to get Android Studio to just take what's in my SQLite database and paste its contents into a listview.我不知道如何让 Android Studio 获取我的 SQLite 数据库中的内容并将其内容粘贴到列表视图中。 I thought there would be a easy way after Log the output but can able to display everything there is in a database in a ListView but it appears to be much more in a different way of doing it.我认为在记录输出之后会有一种简单的方法,但可以在 ListView 中显示数据库中的所有内容,但它似乎以不同的方式进行。

    public class MainActivity extends AppCompatActivity {

    EditText nameEditText;
    EditText phoneEditText;
    ListView updateListView;

    SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEditText = findViewById(R.id.nameEditText);
        phoneEditText = findViewById(R.id.phoneEditText);
        updateListView = findViewById(R.id.updatedListView);



        sqLiteDatabase = this.openOrCreateDatabase("Contact", MODE_PRIVATE, null);

        sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS users(name VARCHAR, phone INT(11),id INTEGER PRIMARY KEY)");


    }


    public void OnClick(View view) {


        if (nameEditText.equals("") || phoneEditText.equals("")) {
            Toast.makeText(this, "Fields shouldn't be empty", Toast.LENGTH_SHORT).show();
        } else {

            sqLiteDatabase.execSQL("INSERT INTO users(name, phone) VALUES('" + nameEditText.getText().toString() + "','" + phoneEditText.getText().toString() + "')");

            Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM users", null);
            int nameIndex = c.getColumnIndex("name");
            int phoneIndex = c.getColumnIndex("phone");
            int idIndex = c.getColumnIndex("id");
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                for (int i = 0; i < c.getCount(); i++) {
                    String str = c.getString(0);

                    String UpdatedList = (c.getString(nameIndex));
                    String phonelist = (c.getString(phoneIndex));

                    String list = (UpdatedList+phonelist);
                    ArrayList<String> arrayList = new ArrayList<String>(Integer.parseInt(list));
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,arrayList);
                    updateListView.setAdapter(arrayAdapter);              


                    Log.i("User Name", c.getString(nameIndex));
                    Log.i("userResults - age", Integer.toString(c.getInt(phoneIndex)));
                    Log.i("userResults - id", Integer.toString(c.getInt(idIndex)));
                    c.moveToNext();
                }




            }
        }
    }
    }

My Logput我的日志输入

2020-01-22 20:22:48.353 28783-28783/? D/AndroidRuntime: Shutting down VM
2020-01-22 20:22:48.357 28783-28783/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mysqlapp, PID: 28783
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6669) 
        at android.view.View.performClickInternal(View.java:6638) 
        at android.view.View.access$3100(View.java:789) 
        at android.view.View$PerformClick.run(View.java:26145) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6898) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.lang.NumberFormatException: For input string: ""
        at java.lang.Integer.parseInt(Integer.java:627)
        at java.lang.Integer.parseInt(Integer.java:650)
        at com.example.mysqlapp.MainActivity.OnClick(MainActivity.java:65)

call setAdapter method outside, after you declare whole arrayList from sql, outside of for loop从 sql 声明整个 arrayList 之后,在 for 循环之外调用 setAdapter 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM