Reputation:
In my app my user is entering the category and that category is stored in the FirebseDB. After storing the value in the DB the user can view the inserted value in the Spinner.
At this point all works fine.
I want user to enter only unique value .so , if user wants to enter the value which is already there he should get a Toast.
Following is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.lenovo.baicactivitytest.MainActivity">
<Spinner
android:id="@+id/sp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="Add" />
</RelativeLayout>
Following is my inputdialogue.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="50dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint= "Name" />
</android.support.design.widget.TextInputLayout>
<Button android:id="@+id/saveBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
android:clickable="true"
android:background="@color/colorAccent"
android:layout_marginTop="40dp"
android:textColor="@android:color/white"/>
</LinearLayout>
</LinearLayout>
package com.example.lenovo.baicactivitytest;
/**
* Created by LENOVO on 29-12-2017.
*/
public class Spacecraft {
String name;
public Spacecraft() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.lenovo.baicactivitytest;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import java.util.ArrayList;
/**
* Created by LENOVO on 29-12-2017.
*/
public class FirebaseHelper {
DatabaseReference db;
Boolean saved = null;
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//SAVE
public Boolean save(Spacecraft spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Spacecraft").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
//READ
public ArrayList<String> retrieve()
{
final ArrayList<String> spacecrafts=new ArrayList<>();
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot,spacecrafts);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot,spacecrafts);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
private void fetchData(DataSnapshot snapshot,ArrayList<String> spacecrafts)
{
spacecrafts.clear();
for (DataSnapshot ds:snapshot.getChildren())
{
String name=ds.getValue(Spacecraft.class).getName();
spacecrafts.add(name);
}
}
}
package com.example.lenovo.baicactivitytest;
import android.app.Dialog;
import android.support.v7.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.Spinner;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
DatabaseReference db;
FirebaseHelper helper;
private Button madd_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sp =(Spinner) findViewById(R.id.sp);
//SETUP FB
db= FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(db);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,helper.retrieve());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
madd_btn = (Button) findViewById(R.id.add_btn);
madd_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayInputDialog();
}
});
}
//DISPLAY INPUT DILAOG
private void displayInputDialog()
{
Dialog d=new Dialog(this);
d.setTitle("Firebase database");
d.setContentView(R.layout.inputdialog);
final EditText nameTxt= (EditText) d.findViewById(R.id.nameEditText);
Button saveBtn = (Button) d.findViewById(R.id.saveBtn);
//SAVE
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//GET DATA
String name=nameTxt.getText().toString();
//set data
Spacecraft s = new Spacecraft();
s.setName(name);
//SAVE
if(name != null && name.length()>0)
{
if(helper.save(s))
{
nameTxt.setText("");
}
}else
{
Toast.makeText(MainActivity.this, "Name Cannot Be Empty", Toast.LENGTH_SHORT).show();
}
}
});
d.show();
}
}
Upvotes: 0
Views: 2844
Reputation: 80914
Can do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Category");
ref.orderByChild("categoryname").equalTo(name).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exist()) {
Toast.makeText(MainActivity.this, "Name already exists", Toast.LENGTH_SHORT).show();
}
}
Can do the above, it will search in the Category
node in the db, and if it exists then it will give you the Toast message.
Upvotes: 1