我是Android开发的完全新手。我的目的是创建一个应用程序,将在EditText中收集的用户输入存储到SQLite数据库中,然后将它们作为ListView显示在下一个活动中。还有一些按钮涉及,其中一些是冗余的,因为应用仍处于早期阶段。
在构建Gradle时,没有错误,logcat
似乎完全没问题。出现问题的第一个迹象是检查/data/data/[projectname]
文件夹,该文件夹中没有包含任何.db
文件或数据库目录。我已经设法使活动之间的过渡完美无瑕,但由于我的项目中显然没有数据库,所以ListView中没有任何数据库。
我要求帮助确定我的代码有什么问题。
MainActivity:
package com.example.peroalex.trackofworkinghours;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
MyDatabaseHelper databaseHelper;
Button ADD, DISPLAY, DELETE, MODIFY;
EditText DESCRIPTION, LOCATION, START, FINISH, COMMENT, ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ADD = (Button) findViewById(R.id.addButton);
DISPLAY = (Button) findViewById(R.id.displayButton);
DELETE = (Button) findViewById(R.id.deleteButton);
MODIFY = (Button) findViewById(R.id.modifyButton);
DESCRIPTION = (EditText) findViewById(R.id.descriptionEdit);
LOCATION = (EditText) findViewById(R.id.locationEdit);
START = (EditText) findViewById(R.id.startEdit);
FINISH = (EditText) findViewById(R.id.finishEdit);
COMMENT = (EditText) findViewById(R.id.commentEdit);
ID = (EditText) findViewById(R.id.idEdit);
databaseHelper = new MyDatabaseHelper(this, null, null, 1);
ADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseAddData();
}
});
}
public void databaseAddData() {
String addDSC = DESCRIPTION.getText().toString();
String addLOC = LOCATION.getText().toString();
String addSTR = START.getText().toString();
String addFNS = FINISH.getText().toString();
String addCOM = COMMENT.getText().toString();
databaseHelper.addData(addDSC, addLOC, addSTR, addFNS, addCOM);
}
public void Action (View view) {
Intent intent = new Intent(MainActivity.this, showRecords.class);
startActivity(intent);
}
}
MyDatabaseHelper:
package com.example.peroalex.trackofworkinghours;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by peroalex on 4/1/18.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
//Define database properties
private static final int database_version = 1;
private static final String database_name = "DatabaseRecords.db";
private static final String table_name = "tasks";
private static final String column_id = "_id";
private static final String column_desc = "description";
private static final String column_loc = "location";
private static final String column_strt = "start";
private static final String column_fnsh = "finish";
private static final String column_comm = "comment";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, database_name, factory, database_version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//This code will be executed when creating a database, it includes the SQLite query which initiates a table
String query = " CREATE TABLE " + table_name + " ( "
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT "
+ column_loc + " TEXT "
+ column_strt + " TEXT "
+ column_fnsh + " TEXT "
+ column_comm + " TEXT " + ");";
sqLiteDatabase.execSQL(query);
}
//This method is used for deleting data according to given ID
public void removeData(Integer ID) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.execSQL("DELETE FROM " + table_name + " WHERE " + column_id + " = " + ID + ";");
sqLiteDatabase.close();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + table_name);
onCreate(sqLiteDatabase);
}
//This method is used for adding data to the SQLite database
public void addData(String dsc, String loc, String st, String fi, String comm) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(column_desc, dsc);
values.put(column_loc, loc);
values.put(column_strt, st);
values.put(column_fnsh, fi);
values.put(column_comm, comm);
sqLiteDatabase.insert(table_name, null, values);
sqLiteDatabase.close();
}
public Cursor getData() {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor data = sqLiteDatabase.rawQuery("SELECT * FROM " + table_name, null);
return data;
}
}
showRecords:
package com.example.peroalex.trackofworkinghours;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by peroalex on 4/2/18.
*/
public class showRecords extends AppCompatActivity {
ListView listView;
MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_records);
listView = (ListView) findViewById(R.id.displayDBListview);
myDatabaseHelper = new MyDatabaseHelper(this, null, null, 1);
displayDB();
}
public void displayDB() {
Cursor data = myDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
listData.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.peroalex.trackofworkinghours.MainActivity">
<EditText
android:id="@+id/descriptionEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_description_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="@string/description"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="@+id/locationEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_location_on_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="@string/location"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />
<EditText
android:id="@+id/startEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="@string/start"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.249" />
<EditText
android:id="@+id/finishEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="@string/finish"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.337" />
<EditText
android:id="@+id/commentEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_comment_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="@string/comments"
android:inputType="textMultiLine"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.448" />
<Button
android:id="@+id/addButton"
style="@android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="@+id/deleteButton"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/delete"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.291"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<Button
android:id="@+id/displayButton"
style="@android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/display"
android:onClick="Action"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.702"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="@+id/modifyButton"
style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/modify"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.708"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/insert_an_id_to_either_modify_or_delete_the_query"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.731" />
<EditText
android:id="@+id/idEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:drawableStart="@drawable/ic_update_black_24dp"
android:fontFamily="serif"
android:hint="@string/id"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.829" />
</android.support.constraint.ConstraintLayout>
activity_show_records:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<ListView
android:id="@+id/displayDBListview"
android:layout_width="344dp"
android:layout_height="551dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您创建数据库的查询不正确。您忘记在,
之后添加TEXT
,因此您的数据库从未创建过。看看下面的代码
String query = " CREATE TABLE " + table_name + " ("
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT,"
+ column_loc + " TEXT,"
+ column_strt + " TEXT,"
+ column_fnsh + " TEXT,"
+ column_comm + " TEXT" + ");";
sqLiteDatabase.execSQL(query);
}