[英]Display the data in the ListView in Android Studio
I am new to Android, and i am trying to display the data from sqlite to new Activity in List view.我是 Android 的新手,我正在尝试将 sqlite 中的数据显示到列表视图中的新活动。
**MainActivity.xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative"
android:background="@color/App_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Fill the Details Here"
android:textSize="30sp"
android:layout_marginLeft="80dp"
android:layout_marginTop="25dp"
android:textColor="@color/color1"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="35sp"
android:layout_marginTop="105sp"
android:layout_marginLeft="30sp"/>
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="35sp"
android:layout_marginLeft="30sp"
android:layout_marginTop="10sp"
android:layout_below="@+id/edit1"/>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email"
/>
<TextView
android:id="@+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile"
android:textSize="35sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="30sp"
android:layout_below="@+id/edit2"/>
<EditText
android:id="@+id/edit3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mobile"
/>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="35sp"
android:layout_marginTop="10sp"
android:layout_marginLeft="30sp"
android:layout_below="@+id/edit3"/>
<EditText
android:id="@+id/edit4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/password"
/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit4"
android:text="Sign Up"
android:background="@color/teal_700"
android:textStyle="bold"
android:layout_marginTop="25dp" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Data"
android:background="@color/teal_700"
android:textStyle="bold"
android:layout_below="@id/btn1"
android:layout_marginTop="25dp" />
</RelativeLayout>
**Show_data.xml**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".show_data">
<ListView
android:id="@+id/lv_customerList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
**MainActivity.java**
package com.example.databaseconnection;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button signup,showdata;
EditText name,email,Mobile,Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signup = findViewById(R.id.btn1);
name = findViewById(R.id.edit1);
email = findViewById(R.id.edit2);
Mobile = findViewById(R.id.edit3);
Password = findViewById(R.id.edit4);
showdata = findViewById(R.id.btn2);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomerModel customerModel = null;
try{
customerModel = new CustomerModel(-1,
name.getText().toString(),
email.getText().toString(),
Integer.parseInt(Mobile.getText().toString()),
Password.getText().toString());
}
catch (Exception e){
Toast.makeText(MainActivity.this, "Error in Registration",Toast.LENGTH_SHORT).show();
}
DataBaseHelper dataBaseHelper = new DataBaseHelper(MainActivity.this); ///check why MainActivity. this
boolean b = dataBaseHelper.addOne(customerModel);
Toast.makeText(MainActivity.this,"Data Added Sccessfully", Toast.LENGTH_SHORT).show();
}
});
showdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,show_data.class);
startActivity(intent);
}
});
}
}
**DatabaseHelper.java**
package com.example.databaseconnection;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String CUSTOMER_TABLE = "CUSTOMER_TABLE";
public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String MOBILE = "MOBILE";
public static final String PASSWORD = "PASSWORD";
public static final String ID = "ID";
public DataBaseHelper(@Nullable Context context)
{
super(context, "customer.db", null, 1);
}
// This is called the first time a database is accessed. there should be code in here to create a new database
@Override
public void onCreate(SQLiteDatabase db) {
String createTableStatement = "CREATE TABLE " + CUSTOMER_TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT, " + EMAIL + " TEXT, " + MOBILE + " TEXT, " + PASSWORD + " TEXT)";
db.execSQL(createTableStatement);
}
// this is called if hte database version number changes.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean addOne(CustomerModel customerModel)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(NAME,customerModel.getName());
cv.put(EMAIL, customerModel.getEmail());
cv.put(MOBILE,customerModel.getMobile());
cv.put(PASSWORD,customerModel.getPassword());
db.insert(CUSTOMER_TABLE,null,cv);
return true;
}
public List<CustomerModel> getEveryOne()
{
List<CustomerModel> returnList = new ArrayList<>();
String queryString = " Select * FROM " + CUSTOMER_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString,null); // rawQuery returns a Cursor.
if(cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
String Name = cursor.getString(1);
String Email = cursor.getString(2);
int Mobile = cursor.getInt(3);
String Password = cursor.getString(4);
CustomerModel newcustomer = new CustomerModel(id,Name,Email,Mobile,Password);
returnList.add(newcustomer);
} while (cursor.moveToNext());
}
else {
//If nothing in the database, do not add anything to the list.
}
cursor.close();
db.close();
return returnList;
}
}
**CustomerModel.java**
package com.example.databaseconnection;
public class CustomerModel {
int id;
private String name;
private String email;
private int mobile;
private String password;`enter code here`
@Override
public String toString() {
return "CustomerModel{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", mobile=" + mobile +
", password='" + password + '\'' +
'}';
}
public CustomerModel(int id, String name, String email, int mobile, String password) {
this.id = id;
this.name = name;
this.email = email;
this.mobile = mobile;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
**show_data. java**
public class show_data extends AppCompatActivity {
ListView lv_customerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_data);
DataBaseHelper dataBaseHelper = new DataBaseHelper(show_data.this); //check why MainActivity.this
List<CustomerModel> everyone = dataBaseHelper.getEveryOne();
// Toast.makeText(MainActivity.this, everyone.toString(),Toast.LENGTH_SHORT).show();
ArrayAdapter customerArrayAdapter = new ArrayAdapter<CustomerModel>(this, android.R.layout.simple_list_item_1, everyone);
lv_customerList.setAdapter(customerArrayAdapter);
}}`enter code here`
lt Adds the data successfully but the the functionality to view the data does not work. lt 添加数据成功,但查看数据的功能不起作用。 My app crashes an throws the below error.我的应用程序崩溃并引发以下错误。
Caused by: java.lang.NullPointerException
: Attempt to invoke virtual method void android.widget.ListView.setAdapter(android.widget.ListAdapter)
on a null object reference Caused by: java.lang.NullPointerException
: Attempt to invoke virtual method void android.widget.ListView.setAdapter(android.widget.ListAdapter)
on a null object reference
You haven't called findViewById()
on lv_customerList
你还没有在lv_customerList
上调用findViewById()
after setContentView(R.layout.activity_show_data)
in show_data.java
add below code在show_data.java
中的setContentView(R.layout.activity_show_data)
之后添加以下代码
lv_customerList = findViewById(R.id.lv_customerList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.