Firebase Чтение данных из базы данных

MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


public class MainActivity extends AppCompatActivity {
private EditText edName, edSecName, edEmail;
private DatabaseReference mDataBase;
private String USER_KEY = "User";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
edName = findViewById(R.id.edName);
edSecName = findViewById(R.id.edSecName);
edEmail = findViewById(R.id.edEmail);
mDataBase = FirebaseDatabase.getInstance().getReference(USER_KEY);
}

public void onClickSave(View view)
{
String id = mDataBase.getKey();
String name = edName.getText().toString();
String sec_name = edSecName.getText().toString();
String email = edEmail.getText().toString();
User newUser = new User(id,name,sec_name,email);
if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(sec_name) && !TextUtils.isEmpty(email) )
{
mDataBase.push().setValue(newUser);
Toast.makeText(this, "Сохранено", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Пустое поле", Toast.LENGTH_SHORT).show();
}




}
public void onClickRead(View view)
{
Intent i = new Intent(MainActivity.this, ReadActivity.class);
startActivity(i);
}

}

activity_main.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=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="96dp"
android:onClick="onClickSave"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edEmail" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:onClick="onClickRead"
android:text="Read"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<EditText
android:id="@+id/edName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edSecName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:ems="10"
android:hint="Second Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edName" />

<EditText
android:id="@+id/edEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edSecName" />
</androidx.constraintlayout.widget.ConstraintLayout>

User

public class User {
public String id, name, sec_name, email;

public User() {
}

public User(String id, String name, String sec_name, String email) {
this.id = id;
this.name = name;
this.sec_name = sec_name;
this.email = email;
}
}

ReadActivity

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class ReadActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> listData;
private List<User> listTemp;

private DatabaseReference mDataBase;
private String USER_KEY = "User";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_layout);
init();
getDataFromDB();
setOnClickItem();
}
private void init()
{
listView = findViewById(R.id.listView);
listData = new ArrayList<>();
listTemp = new ArrayList<>();
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
mDataBase = FirebaseDatabase.getInstance().getReference(USER_KEY);
}
private void getDataFromDB()
{
ValueEventListener vListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if(listData.size() > 0)listData.clear();
if(listTemp.size() > 0)listTemp.clear();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
User user = ds.getValue(User.class);
assert user != null;
listData.add(user.name);
listTemp.add(user);
}
adapter.notifyDataSetChanged();

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
};
mDataBase.addValueEventListener(vListener);
}
private void setOnClickItem()
{
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
User user = listTemp.get(position);
Intent i = new Intent(ReadActivity.this, ShowActivity.class);
i.putExtra(Constant.USER_NAME,user.name);
i.putExtra(Constant.USER_SEC_NAME,user.sec_name);
i.putExtra(Constant.USER_EMAIL,user.email);
startActivity(i);

}
});
}

}

read_layout.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">

<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ShowActivity

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ShowActivity extends AppCompatActivity {
private TextView tvName, tvSecName, tvEmail;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_layout);
init();
getIntentMain();
}
private void init()
{
tvName = findViewById(R.id.tvName);
tvSecName = findViewById(R.id.tvSecName);
tvEmail = findViewById(R.id.tvEmail);
}
private void getIntentMain()
{
Intent i = getIntent();
if(i != null)
{
tvName.setText(i.getStringExtra(Constant.USER_NAME));
tvSecName.setText(i.getStringExtra(Constant.USER_SEC_NAME));
tvEmail.setText(i.getStringExtra(Constant.USER_EMAIL));
}
}
}

show_layout.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">

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvSecName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvName" />

<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvSecName" />
</androidx.constraintlayout.widget.ConstraintLayout>

Constant

public class Constant {
public final static String USER_NAME = "user_name";
public final static String USER_SEC_NAME = "user_sec_name";
public final static String USER_EMAIL = "user_email";
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neco_desarrollo.basedatosneco1">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ReadActivity" />
<activity android:name=".ShowActivity" />
</application>

</manifest>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *