我的应用程序显示 RecyclerView 但现在显示它的项目
my app shows RecyclerView but does now show it's items
实际上我的应用程序显示 RecyclerView 但现在显示它的项目。
之后我在字段中输入数据并单击 btn_add,它们成功保存在数据库中,但是当我单击我的应用程序中的 Btn-show 按钮显示它们时,一切正常并且它显示 recyclerview,但它只显示“id”项,其他项不显示。
SecondActivity.java
class SecondActivity : AppCompatActivity() {
private var dbHandle:DatabaseHandler?=null
lateinit var btn_show:Button
lateinit var rec:RecyclerView
lateinit var btn_add:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
dbHandle = DatabaseHandler(this)
btn_show=findViewById(R.id.show)
rec=findViewById(R.id.rec)
btn_add=findViewById(R.id.add)
var edit_name=findViewById<EditText>(R.id.edit_name)
var edit_age =findViewById<EditText>(R.id.edit_age)
var edit_heigth=findViewById<EditText>(R.id.edit_heigth)
var edit_city=findViewById<EditText>(R.id.edit_city)
var edit_b_day=findViewById<EditText>(R.id.edit_b_day)
var edit_b_city=findViewById<EditText>(R.id.edit_b_city)
var edit_education=findViewById<EditText>(R.id.edit_education)
val actors=Actors()
actors.Name=edit_name.text.toString()
actors.Age=edit_age.text.toString()
actors.heigth=edit_heigth.text.toString()
actors.city=edit_city.text.toString()
actors.bdate=edit_b_day.text.toString()
actors.bcity=edit_b_city.text.toString()
actors.education=edit_education.text.toString()
btn_add.setOnClickListener {
var success = false
success=dbHandle!!.addActors(actors)
Toast.makeText(this,"result: $success", Toast.LENGTH_SHORT).show()
}
btn_show.setOnClickListener {
rec.layoutManager = LinearLayoutManager(this)
val myList= dbHandle!!.getAllActorsList()
rec.adapter=RecyclerAdapter(myList,this)}
}
我的class:
var Name: String = ""
var Age: String = ""
var heigth: String = ""
var city: String = ""
var bdate:String = ""
var bcity: String = ""
var education: String =""
constructor()
constructor(
id: Int?,
Name: String,
Age: String,
heigth: String,
city: String,
b_date: String,
b_city: String,
education: String,
) {
this.id = id
this.Name = Name
this.Age = Age
this.heigth = heigth
this.city = city
this.bdate = b_date
this.bcity = b_city
this.education = education
}
constructor(
Name: String,
Age: String,
heigth: String,
city: String,
b_date: String,
b_city: String,
education: String,
) {
this.Name = Name
this.Age = Age
this.heigth = heigth
this.city = city
this.bdate = b_date
this.bcity = b_city
this.education = education
回收器适配器:
class RecyclerAdapter( val items:ArrayList<Actors>, val context1: Context):RecyclerView.Adapter<viewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolder {
return viewHolder(LayoutInflater.from(context1).inflate(R.layout.recycler_item,parent,false))
}
override fun onBindViewHolder(holder: viewHolder, position: Int) {
holder!!.name.text = items.get(position).Name
holder!!.age.text = items.get(position).Age
holder!!.id.text = items.get(position).id.toString()
holder!!.height.text = items.get(position).heigth
holder!!.city.text = items.get(position).city
holder!!.bdate.text = items.get(position).bdate
holder!!.bcity.text = items.get(position).bcity
holder!!.education.text = items.get(position).education
holder.itemView.setOnClickListener {
Toast.makeText(context1, "position:$position....",Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int {
return items.size
}
}
class viewHolder(view: View):RecyclerView.ViewHolder(view){
val name = view.findViewById<TextView>(R.id.name)
var age =view.findViewById<TextView>(R.id.age)
val height = view.findViewById<TextView>(R.id.height)
val id = view.findViewById<TextView>(R.id.id)
var city =view.findViewById<TextView>(R.id.city)
val bdate = view.findViewById<TextView>(R.id.b_date)
var bcity =view.findViewById<TextView>(R.id.b_city)
val education = view.findViewById<TextView>(R.id.education)
DatabseHandler class:
class DatabaseHandler(context: Context):SQLiteOpenHelper(context,DB_Name,null,DB_Version){
companion object{
private val DB_Name = "DB"
private val DB_Version=1
private val TABLE_NAME="actors"
private val ID ="id"
private val Name = "Name"
private val Age = "Age"
private val Heigth="Height"
private val City = "City"
private val b_date ="bDate"
private val b_city="bCity"
private val Education = "Education"
}
override fun onCreate(p0: SQLiteDatabase?) {
var CREATE_TABLE = "CREATE TABLE $TABLE_NAME" + "($ID INTEGER PRIMARY KEY, $Name TEXT,$Age TEXT, $Heigth TEXT, $City TEXT, $b_date TEXT, $b_city TEXT,$Education TEXT)"
p0?.execSQL(CREATE_TABLE)
}
fun addActors(actors:Actors): Boolean {
val po = this.writableDatabase
val values = ContentValues()
values.put(Name, actors.Name)
values.put(Age, actors.Age)
values.put(Heigth,actors.heigth)
values.put(City, actors.city)
values.put(b_date, actors.bdate)
values.put(b_city, actors.bcity)
values.put(Education, actors.education)
var success=po.insert(TABLE_NAME,null,values)
po.close()
return (Integer.parseInt("$success")!=-1)
}
@SuppressLint("Range")
fun getAllActorsList():ArrayList<Actors>{
val list =ArrayList<Actors>()
val p0=readableDatabase
var selectAll = "SELECT * FROM $TABLE_NAME"
var cursor = p0.rawQuery(selectAll,null)
if (cursor!=null){
if (cursor.moveToFirst()){
do{
val id = cursor.getString(cursor.getColumnIndex(ID))
val name = cursor.getString(cursor.getColumnIndex(Name))
val age = cursor.getString(cursor.getColumnIndex(Age))
val height = cursor.getString(cursor.getColumnIndex(Heigth))
val city = cursor.getString(cursor.getColumnIndex(City))
val bdate = cursor.getString(cursor.getColumnIndex(b_date))
val bcity = cursor.getString(cursor.getColumnIndex(b_city))
val education = cursor.getString(cursor.getColumnIndex(Education))
list.add(Actors(id.toInt(),name,age,height,city,bdate,bcity,education))
}
while (cursor.moveToNext())
}
}
cursor.close()
p0.close()
return list
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SecondActivity"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="v">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_id"
tools:ignore="Autofill,LabelFor,TextFields"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_name"
tools:ignore="Autofill,LabelFor,TextFields" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_age"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_heigth"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_city"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_b_day"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_b_city"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_education"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="btn_add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="btn_show"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rec"/>
</LinearLayout>
Recycler_item.xml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id"
android:layout_marginLeft="4dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:padding="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/height"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city"
android:layout_marginLeft="4dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b_date"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:padding="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b_city"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/education"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
点击“添加”按钮保存您的数据。试试这个方法。
btn_add.setOnClickListener {
val actors = Actors()
actors.Name = edit_name.text.toString()
actors.Age = edit_age.text.toString()
actors.heigth = edit_heigth.text.toString()
actors.city = edit_city.text.toString()
actors.bdate = edit_b_day.text.toString()
actors.bcity = edit_b_city.text.toString()
actors.education = edit_education.text.toString()
var success = false
success = dbHandle!!.addActors(actors)
Toast.makeText(this, "result: $success", Toast.LENGTH_SHORT).show()
}
实际上我的应用程序显示 RecyclerView 但现在显示它的项目。 之后我在字段中输入数据并单击 btn_add,它们成功保存在数据库中,但是当我单击我的应用程序中的 Btn-show 按钮显示它们时,一切正常并且它显示 recyclerview,但它只显示“id”项,其他项不显示。
SecondActivity.java
class SecondActivity : AppCompatActivity() {
private var dbHandle:DatabaseHandler?=null
lateinit var btn_show:Button
lateinit var rec:RecyclerView
lateinit var btn_add:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
dbHandle = DatabaseHandler(this)
btn_show=findViewById(R.id.show)
rec=findViewById(R.id.rec)
btn_add=findViewById(R.id.add)
var edit_name=findViewById<EditText>(R.id.edit_name)
var edit_age =findViewById<EditText>(R.id.edit_age)
var edit_heigth=findViewById<EditText>(R.id.edit_heigth)
var edit_city=findViewById<EditText>(R.id.edit_city)
var edit_b_day=findViewById<EditText>(R.id.edit_b_day)
var edit_b_city=findViewById<EditText>(R.id.edit_b_city)
var edit_education=findViewById<EditText>(R.id.edit_education)
val actors=Actors()
actors.Name=edit_name.text.toString()
actors.Age=edit_age.text.toString()
actors.heigth=edit_heigth.text.toString()
actors.city=edit_city.text.toString()
actors.bdate=edit_b_day.text.toString()
actors.bcity=edit_b_city.text.toString()
actors.education=edit_education.text.toString()
btn_add.setOnClickListener {
var success = false
success=dbHandle!!.addActors(actors)
Toast.makeText(this,"result: $success", Toast.LENGTH_SHORT).show()
}
btn_show.setOnClickListener {
rec.layoutManager = LinearLayoutManager(this)
val myList= dbHandle!!.getAllActorsList()
rec.adapter=RecyclerAdapter(myList,this)}
}
我的class:
var Name: String = ""
var Age: String = ""
var heigth: String = ""
var city: String = ""
var bdate:String = ""
var bcity: String = ""
var education: String =""
constructor()
constructor(
id: Int?,
Name: String,
Age: String,
heigth: String,
city: String,
b_date: String,
b_city: String,
education: String,
) {
this.id = id
this.Name = Name
this.Age = Age
this.heigth = heigth
this.city = city
this.bdate = b_date
this.bcity = b_city
this.education = education
}
constructor(
Name: String,
Age: String,
heigth: String,
city: String,
b_date: String,
b_city: String,
education: String,
) {
this.Name = Name
this.Age = Age
this.heigth = heigth
this.city = city
this.bdate = b_date
this.bcity = b_city
this.education = education
回收器适配器:
class RecyclerAdapter( val items:ArrayList<Actors>, val context1: Context):RecyclerView.Adapter<viewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolder {
return viewHolder(LayoutInflater.from(context1).inflate(R.layout.recycler_item,parent,false))
}
override fun onBindViewHolder(holder: viewHolder, position: Int) {
holder!!.name.text = items.get(position).Name
holder!!.age.text = items.get(position).Age
holder!!.id.text = items.get(position).id.toString()
holder!!.height.text = items.get(position).heigth
holder!!.city.text = items.get(position).city
holder!!.bdate.text = items.get(position).bdate
holder!!.bcity.text = items.get(position).bcity
holder!!.education.text = items.get(position).education
holder.itemView.setOnClickListener {
Toast.makeText(context1, "position:$position....",Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int {
return items.size
}
}
class viewHolder(view: View):RecyclerView.ViewHolder(view){
val name = view.findViewById<TextView>(R.id.name)
var age =view.findViewById<TextView>(R.id.age)
val height = view.findViewById<TextView>(R.id.height)
val id = view.findViewById<TextView>(R.id.id)
var city =view.findViewById<TextView>(R.id.city)
val bdate = view.findViewById<TextView>(R.id.b_date)
var bcity =view.findViewById<TextView>(R.id.b_city)
val education = view.findViewById<TextView>(R.id.education)
DatabseHandler class:
class DatabaseHandler(context: Context):SQLiteOpenHelper(context,DB_Name,null,DB_Version){
companion object{
private val DB_Name = "DB"
private val DB_Version=1
private val TABLE_NAME="actors"
private val ID ="id"
private val Name = "Name"
private val Age = "Age"
private val Heigth="Height"
private val City = "City"
private val b_date ="bDate"
private val b_city="bCity"
private val Education = "Education"
}
override fun onCreate(p0: SQLiteDatabase?) {
var CREATE_TABLE = "CREATE TABLE $TABLE_NAME" + "($ID INTEGER PRIMARY KEY, $Name TEXT,$Age TEXT, $Heigth TEXT, $City TEXT, $b_date TEXT, $b_city TEXT,$Education TEXT)"
p0?.execSQL(CREATE_TABLE)
}
fun addActors(actors:Actors): Boolean {
val po = this.writableDatabase
val values = ContentValues()
values.put(Name, actors.Name)
values.put(Age, actors.Age)
values.put(Heigth,actors.heigth)
values.put(City, actors.city)
values.put(b_date, actors.bdate)
values.put(b_city, actors.bcity)
values.put(Education, actors.education)
var success=po.insert(TABLE_NAME,null,values)
po.close()
return (Integer.parseInt("$success")!=-1)
}
@SuppressLint("Range")
fun getAllActorsList():ArrayList<Actors>{
val list =ArrayList<Actors>()
val p0=readableDatabase
var selectAll = "SELECT * FROM $TABLE_NAME"
var cursor = p0.rawQuery(selectAll,null)
if (cursor!=null){
if (cursor.moveToFirst()){
do{
val id = cursor.getString(cursor.getColumnIndex(ID))
val name = cursor.getString(cursor.getColumnIndex(Name))
val age = cursor.getString(cursor.getColumnIndex(Age))
val height = cursor.getString(cursor.getColumnIndex(Heigth))
val city = cursor.getString(cursor.getColumnIndex(City))
val bdate = cursor.getString(cursor.getColumnIndex(b_date))
val bcity = cursor.getString(cursor.getColumnIndex(b_city))
val education = cursor.getString(cursor.getColumnIndex(Education))
list.add(Actors(id.toInt(),name,age,height,city,bdate,bcity,education))
}
while (cursor.moveToNext())
}
}
cursor.close()
p0.close()
return list
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SecondActivity"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="v">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_id"
tools:ignore="Autofill,LabelFor,TextFields"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_name"
tools:ignore="Autofill,LabelFor,TextFields" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_age"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_heigth"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_city"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_b_day"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_b_city"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_education"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="btn_add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="btn_show"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rec"/>
</LinearLayout>
Recycler_item.xml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id"
android:layout_marginLeft="4dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:padding="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/height"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city"
android:layout_marginLeft="4dp"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b_date"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:padding="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b_city"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/education"
android:textSize="18sp"
android:paddingLeft="20dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
点击“添加”按钮保存您的数据。试试这个方法。
btn_add.setOnClickListener {
val actors = Actors()
actors.Name = edit_name.text.toString()
actors.Age = edit_age.text.toString()
actors.heigth = edit_heigth.text.toString()
actors.city = edit_city.text.toString()
actors.bdate = edit_b_day.text.toString()
actors.bcity = edit_b_city.text.toString()
actors.education = edit_education.text.toString()
var success = false
success = dbHandle!!.addActors(actors)
Toast.makeText(this, "result: $success", Toast.LENGTH_SHORT).show()
}