Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio的ListView中显示数据_Java_Android - Fatal编程技术网

Java 在Android Studio的ListView中显示数据

Java 在Android Studio的ListView中显示数据,java,android,Java,Android,我是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

我是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`
**MainActivity.xml**
**Show_data.xml**
**MainActivity.java**
包com.example.databaseconnection;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入androidx.appcompat.app.appcompat活动;
公共类MainActivity扩展了AppCompatActivity{
按钮注册,显示数据;
编辑文本名称、电子邮件、手机、密码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
注册=findViewById(R.id.btn1);
name=findviewbyd(R.id.edit1);
email=findviewbyd(R.id.edit2);
Mobile=findviewbyd(R.id.edit3);
密码=findviewbyd(R.id.edit4);
showdata=findviewbyd(R.id.btn2);
signup.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
CustomerModel CustomerModel=null;
试一试{
customerModel=新customerModel(-1,
name.getText().toString(),
email.getText().toString(),
Integer.parseInt(Mobile.getText().toString()),
Password.getText().toString());
}
捕获(例外e){
Toast.makeText(MainActivity.this,“注册错误”,Toast.LENGTH_SHORT.show();
}
DataBaseHelper DataBaseHelper=新的DataBaseHelper(MainActivity.this);///检查为什么MainActivity.this
布尔b=dataBaseHelper.addOne(customerModel);
Toast.makeText(MainActivity.this,“成功添加数据”,Toast.LENGTH_SHORT.show();
}
});
showdata.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(MainActivity.this,show_data.class);
星触觉(意向);
}
});
}
}
**DatabaseHelper.java**
包com.example.databaseconnection;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入androidx.annotation.Nullable;
导入java.util.ArrayList;
导入java.util.List;
公共类DataBaseHelper扩展了SQLiteOpenHelper{
公共静态最终字符串CUSTOMER\u TABLE=“CUSTOMER\u TABLE”;
公共静态最终字符串NAME=“NAME”;
公共静态最终字符串EMAIL=“EMAIL”;
公共静态最终字符串MOBILE=“MOBILE”;
公共静态最终字符串PASSWORD=“PASSWORD”;
公共静态最终字符串ID=“ID”;
公共DataBaseHelper(@Nullable上下文)
{
super(上下文“customer.db”,null,1);
}
//这称为第一次访问数据库。这里应该有创建新数据库的代码
@凌驾
public void onCreate(SQLiteDatabase db){
字符串createTableStatement=“CREATE TABLE”+CUSTOMER_TABLE+”(“+ID+”整型主键自动递增,“+NAME+”文本,“+EMAIL+”文本,“+MOBILE+”文本,“+PASSWORD+”文本)”;
execSQL(createTableStatement);
}
//如果数据库版本号发生更改,则调用此函数。
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
}
公共布尔addOne(CustomerModel CustomerModel)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=新的ContentValues();
cv.put(NAME,customerModel.getName());
cv.put(EMAIL,customerModel.getEmail());
cv.put(MOBILE,customerModel.getMobile());
cv.put(密码,customerModel.getPassword());
db.insert(客户表,空,cv);
返回true;
}
公共列表getEveryOne()
{
List returnList=new ArrayList();
字符串queryString=“Select*FROM”+客户表;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(queryString,null);//rawQuery返回一个游标。
if(cursor.moveToFirst()){
做{
int id=cursor.getInt(0);
字符串名称=cursor.getString(1);
字符串Email=cursor.getString(2);
int Mobile=cursor.getInt(3);
字符串密码=cursor.getString(4);
CustomerModel newcustomer=新CustomerModel(id、姓名、电子邮件、手机、密码);
returnList.add(新客户);
}while(cursor.moveToNext());
}
否则{
//如果数据库中没有任何内容,请不要向列表中添加任何内容。
}
cursor.close();
db.close();
退货清单;
}
}
**CustomerModel.java**
包com.example.databaseconnection;
公共类CustomerModel{
int-id;
私有字符串名称;
私人字符串电子邮件;
私人int移动电话;
私有字符串密码;`在此处输入代码`
@凌驾
公共字符串toString(){
返回“CustomerModel{”+
“id=”+id+
“,name=”“+name+”\“””+
“,email='”+email+'\''+
“,mobile=“+mobile”+
,password=''+password+'\''+
'}';
}
公共CustomerModel(int id、字符串名称、字符串电子邮件、int mobile、字符串密码){
this.id=id;
this.name=名称;
this.email=电子邮件;
this.mobile=mobile;
this.password=密码;
}
公共int getId(){
返回id;
}
公共v
lv_customerList = findViewById(R.id.lv_customerList)