我尝试在android studio中使用ormlite开发一个简单的crud应用程序。我已经实现了插入和查看部分。我创建了一个列表视图来查看项目。但是在一条记录中只显示一个属性。我想将一条记录的所有属性都显示到列表视图中。我是android开发的初学者。请告诉我如何做。谢谢。:)
首先,我调用模型类中的一个方法
@NonNull
@Override
public String toString(){
return this.courseDescription;
}
在这个方法中,我只能返回一个属性。我需要返回字符串数据类型的所有属性。
下面是我的代码。
Course.java 模型类)
import androidx.annotation.NonNull;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "course")
public class Course {
@DatabaseField(generatedId = true)
private int courseId;
@DatabaseField(columnName = "course_name")
private String courseName;
@DatabaseField(columnName = "course_duration")
private String courseDuration;
@DatabaseField(columnName = "course_description")
private String courseDescription;
public Course() {
}
public Course(String courseName, String courseDuration,
String courseDescription) {
this.courseName = courseName;
this.courseDuration = courseDuration;
this.courseDescription = courseDescription;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseDuration() {
return courseDuration;
}
public void setCourseDuration(String courseDuration) {
this.courseDuration = courseDuration;
}
public String getCourseDescription() {
return courseDescription;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
@NonNull
@Override
public String toString(){
return this.courseDescription;
}
}
DBA.java
import android.content.Context;
import android.util.Log;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.oss.datahelper.DataBaseManager;
import java.sql.SQLException;
import java.util.List;
public class DBA<T> {
private static final int DB_VERSION = 1;
private Dao<T, Integer> dao;
public void init(Context context, Class clazz){
String dbname = context.getApplicationInfo()
.loadLabel(context.getPackageManager())
.toString()+".sqlite";
Log.i("INFO NAME", dbname);
DataBaseManager.init(context, dbname, DB_VERSION);
ConnectionSource source = DataBaseManager.getInstance()
.getHelper()
.getConnectionSource();
try{
TableUtils.createTableIfNotExists(source, clazz);
this.dao = DataBaseManager.getInstance()
.getHelper()
.getDao(clazz);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Dao<T, Integer> getDao(){
return dao;
}
public T saveCourse(T entity){
try {
this.getDao().create(entity);
return entity;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public List<T> getCourse(){
try {
return this.getDao().queryForAll();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public T getCourseByID(int id){
try {
return this.getDao().queryForId(id);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
CourseDao.java
import android.content.Context;
import com.ss.ormlite3.Model.Course;
public class CourseDao extends DBA<Course>{
public CourseDao(Context context){
init(context,Course.class);
}
}
MainActivity.java
mport androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.ss.ormlite3.Database.CourseDao;
import com.ss.ormlite3.Model.Course;
import com.ss.ormlite3.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText courseNameEdt,courseDurationEdt, courseDescriptionEdt;
private Button addCourseBtn;
ListView courseListView;
CourseDao courseDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
addCourseBtn = findViewById(R.id.idBtnAddCourse);
courseListView = findViewById(R.id.listviewCourse);
courseDao = new CourseDao(this);
loadSavedCourses();
}
private void loadSavedCourses() {
List<Course> courses = courseDao.getCourse();
ArrayAdapter<Course> arrayAdapter = new ArrayAdapter<>(this
, android.R.layout.simple_list_item_1, courses);
courseListView.setAdapter(arrayAdapter);
}
//private void addCourse(){}
public void saveNote(View view) {
try {
Course course = new Course();
course.setCourseName(courseNameEdt.getText().toString());
course.setCourseDuration(courseDurationEdt.getText().toString());
course.setCourseDescription(courseDescriptionEdt.getText().toString());
courseDao.saveCourse(course);
loadSavedCourses();
Toast.makeText(this, "Data addes", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".Activity.MainActivity">
<!--Edit text to enter course name-->
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter course Name"
android:minHeight="48dp" />
<!--edit text to enter course duration-->
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Course Duration"
android:minHeight="48dp" />
<!--edit text for course description-->
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Course Description"
android:minHeight="48dp" />
<!--button for adding new course-->
<Button
android:id="@+id/idBtnAddCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Add Course"
android:textAllCaps="false"
android:onClick="saveNote"/>
<ListView
android:id="@+id/listviewCourse"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
1条答案
按热度按时间cld4siwp1#
您可以尝试以下代码: