This video is part – 2 of the developing a simple business App using Free Firebase database.
In this video it shows how to implement search functionality in any Android App using SearchView widget.
Part – 1 of the video can be found at below link:
https://youtu.be/52pQU6S66S0
(How to develop a small business App and connect with Firebase Database using Android Studio?)
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
package com.example.mystore;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;import androidx.annotation.NonNull;
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;public class MainActivity extends AppCompatActivity {
private EditText editTextProduct;
private EditText editTextPrice;private DatabaseReference myRef;
private String ValueDatabase;
private String refinedData;
private ListView listView;private SearchView searchView;
private TextView textViewSearch;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);myRef = FirebaseDatabase.getInstance().getReference(“MyStore1”);
editTextPrice = findViewById(R.id.editTextPrice);
editTextProduct = findViewById(R.id.editTextProduct);
listView =findViewById(R.id.listView);
searchView = findViewById(R.id.serachview);
textViewSearch = findViewById(R.id.textViewSearch);myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ValueDatabase = dataSnapshot.getValue().toString();
refinedData = ValueDatabase.substring(1,ValueDatabase.length()-1);
String List[] = refinedData.split(“,”);
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, List));}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
int SearchIndex = refinedData.indexOf(query);
String SearchResult = refinedData.substring(SearchIndex);
String SearchSplit[] = SearchResult.split(“,”);
textViewSearch.setText(SearchSplit[0]);return false;
}@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}public void InsertButton(View view){
try {
myRef.child(editTextProduct.getText().toString()).setValue(editTextPrice.getText().toString());
}
catch (Exception e){
e.printStackTrace();
}
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mystore”><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>
</application></manifest>
<?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=”.MainActivity”><EditText
android:id=”@+id/editTextProduct”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”50dp”
android:autofillHints=””
android:ems=”10″
android:hint=”@string/product_name”
android:inputType=”textPersonName” /><EditText
android:id=”@+id/editTextPrice”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”200dp”
android:layout_marginTop=”50dp”
android:autofillHints=””
android:ems=”10″
android:hint=”@string/price”
android:inputType=”textPersonName” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”20dp”
android:onClick=”InsertButton”
android:text=”@string/insert” /><ListView
android:id=”@+id/listView”
android:layout_width=”300dp”
android:layout_height=”400dp”
android:layout_marginStart=”50dp”
android:layout_marginTop=”200dp” /><SearchView
android:id=”@+id/serachview”
android:layout_width=”350dp”
android:layout_height=”20dp”
android:layout_marginStart=”20dp”
android:layout_marginTop=”100dp” /><TextView
android:id=”@+id/textViewSearch”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”150dp”
android:text=”@string/search_result” />
</RelativeLayout>