记录未显示在Listview android Sqlite上

时间:2019-03-03 12:07:08

标签: android

我正在使用android studio创建一个简单的crud系统。我将下面插入代码的代码写到数据库中。插入记录后,如果我单击按钮记录未显示到列表视图中,将显示名称为视图的按钮。我不为什么。我没有收到任何错误运行项目时。不幸的是,单击视图按钮时,crud系统已停止。

插入记录设计activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity = "center"
    android:orientation="vertical">
    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Crud System"
        android:textColor="@color/colorAccent"
        android:textSize="40dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Name" />
        <EditText
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            />
    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Age" />
    <EditText
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Clear" />
</LinearLayout>
</LinearLayout>

MainActivity.java

 public class MainActivity extends AppCompatActivity {

        EditText name1,age1;
        Button btn1,btn2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            name1 = findViewById(R.id.name);
            age1 = findViewById(R.id.age);

            btn1 = findViewById(R.id.btn1);
            btn2 = findViewById(R.id.btn2);
           btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   insert();
                }
            });

     public  void  insert()
        {
            try
            {
                String naame = name1.getText().toString();
                String aage = age1.getText().toString();

                SQLiteDatabase db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
                db.execSQL("CREATE TABLE IF NOT EXISTS record (name VARCHAR(50),age VARCHAR(50))");
                String sql = "insert into record(name,age)values(?,?)";
                SQLiteStatement statement = db.compileStatement(sql);
                statement.bindString(1,naame);
                statement.bindString(2,aage);
                statement.execute();
                Toast.makeText(getApplicationContext(),"Sucesss",Toast.LENGTH_LONG).toString();
            }
            catch (Exception e)
            {
                   e.printStackTrace();
            }
        }

查看记录设计

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".view">

    <ListView
        android:id="@+id/lst1"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</LinearLayout>

view.java

  public class view extends AppCompatActivity {
        ListView lst1;
        ArrayList<String> titles = new ArrayList<String>();
        ArrayAdapter arrayAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view);

            SQLiteDatabase db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
            lst1 = findViewById(R.id.lst1);
            arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,titles);
            lst1.setAdapter(arrayAdapter);
            Cursor c = db.rawQuery("select * from record",null);
            int name = c.getColumnIndex("name");
            int age = c.getColumnIndex("age");
            c.moveToFirst();
            titles.clear();
              while (c != null)
              {
                  titles.add(c.getString(age));
                  c.moveToNext();
              }
            arrayAdapter.notifyDataSetChanged();

        }
    }

0 个答案:

没有答案