the date class from the real time database is not filled in - firebase

I can't fill in the date class from the database. I don't understand why it doesn't work, everything seems to be written correctly.
In the initUser function, the date of the User Model class is filled in, but if you write the USER variable to println, it will be empty. Below is the code of the MainActivity class, which has the initUser function
class MainActivity : AppCompatActivity() {
private lateinit var btn_settings: ImageButton
private lateinit var btn_add_friend: ImageButton
private lateinit var btn_search_main: ImageView
private lateinit var search_main: EditText
private lateinit var nickname_main: TextView
private lateinit var Id_main: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initFirebase()
initUser()
init()
initFunc()
btn_settings.setOnClickListener {
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
btn_search_main.setOnClickListener {
if (search_main.text.toString() == "Happy") {
val intent = Intent(this, psh::class.java)
startActivity(intent)
}
}
nickname_main.text = USER.username
Id_main.text = CURRENT_UID
println(USER)
}
private fun initFunc() {
if (AUTH.currentUser != null) {
} else {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
}
private fun init(){
btn_settings = findViewById(R.id.btn_settings)
btn_add_friend = findViewById(R.id.btn_add_friend)
btn_search_main = findViewById(R.id.btn_search_main)
search_main = findViewById(R.id.search_main)
nickname_main = findViewById(R.id.nickname_main)
Id_main = findViewById(R.id.Id_main)
}
private fun initUser(){
REF_DATABASE_ROOT.child(NODE_USERS).child(CURRENT_UID)
.addListenerForSingleValueEvent(AppValueEventListener{
USER = it.getValue(UserModel()::class.java) ?:UserModel()
})
}
fun initFirebase(){
AUTH = FirebaseAuth.getInstance()
REF_DATABASE_ROOT = FirebaseDatabase.getInstance("https://ert-d167-default-rtdb.europe-west1.firebasedatabase.app").reference
USER = UserModel()
CURRENT_UID = AUTH.currentUser?.uid.toString()
}
}
class AppValueEventListener (val onSuccess:(DataSnapshot)->Unit) : ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
onSuccess(snapshot)
}
}
data class UserModel(
var id: String = "",
var username: String = ""
)
If you insert the USER variable into print, it will be User Model(id=, username=)
The data in the database is

Related

Put pdf files which I download from Firebase Cloud Storage to recyclerview using Kotlin

I have prepared recyclerview to show pdf files but when I try some functions to put pdfs into recyclerview they didin't show. Is any function to do this?
This is my adapter and here probably all is well
class AdapterKarty(private val fileList: ArrayList<plik>): RecyclerView.Adapter<AdapterKarty.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterKarty.ViewHolder {
val a = LayoutInflater.from(parent.context).inflate(R.layout.karta_pliku,parent, false)
return ViewHolder(a);
}
override fun onBindViewHolder(holder: AdapterKarty.ViewHolder, position: Int) {
var item = fileList[position]
holder.dokument.text = item.Name
}
override fun getItemCount(): Int {
return fileList.size
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
var dokument: TextView = itemView.tv_dokment
}
}
In list I use class plik where i declare only name because here i try put name of pdf
class plik( var Name: String?=null){
}
And here I have my main class where I try put those files to recyclerview
class Dokumenty : AppCompatActivity() {
private var stor: FirebaseStorage?=null
private var storageReference = stor?.reference
lateinit var recyclerView: RecyclerView
private lateinit var listaPlikow : ArrayList<plik>
private lateinit var myAdapter: AdapterKarty
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dokumenty)
stor = FirebaseStorage.getInstance()
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
listaPlikow = arrayListOf()
myAdapter = AdapterKarty(listaPlikow)
getFiles()
}
private fun getFiles() {
var pliki = storageReference?.child("Pliki/")
}
}

SetOnClickListener for Button within RecyclerView to access viewmodel and perform action on room database

I would like to add a "delete" button in a RecyclerView showing a list of "Users" present in a Room Database. The button should permit to delete the single user when clicking on the button. I have tried to insert a function in Myviewholder, but when I call it in OnBindViewHolder the error concerns the initialization of the mUserViewModel. Do you have any suggestion on it?
This is the adapter:
class ListAdapterUser: RecyclerView.Adapter<ListAdapterUser.MyViewHolder>() {
private var UserList = emptyList<User>()
private lateinit var mUserViewModel: UserViewModel
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val button = itemView.findViewById<Button>(R.id.deleteoption)
fun deleteitem(item: User, viewModel: UserViewModel){
button.setOnClickListener{
viewModel.deleteUser(item)
}}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_rowUser, parent, false))
}
override fun getItemCount(): Int {
return UserList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = UserList[position]
holder.itemView.textview_valueUser.text = currentItem.Uservalue.toString()
holder.deleteitem(currentItem, mUserViewModel)
}
fun setUserData(User: List<User>){
this.UserList = User
notifyDataSetChanged()
}
}
Thank you!
Solved obtaining the viewmodel initialized in the fragment passing it with setUserData function. Here the final code:
class ListAdapterUser: RecyclerView.Adapter<ListAdapterUser.MyViewHolder>() {
private var UserList = emptyList<User>()
private lateinit var mUserViewModel: UserViewModel
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val button = itemView.findViewById<Button>(R.id.deleteoption)
fun deleteitem(item: User, viewModel: UserViewModel){
button.setOnClickListener{
viewModel.deleteUser(item)
}}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_rowUser, parent, false))
}
override fun getItemCount(): Int {
return UserList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = UserList[position]
holder.itemView.textview_valueUser.text = currentItem.Uservalue.toString()
holder.deleteitem(currentItem, mUserViewModel)
}
fun setUserData(User: List<User>, viewModel: UserViewModel)){
this.UserList = User
this.mUserViewModel = viewModel
notifyDataSetChanged()
}}

Firebase notification click Intent show null on Background state - Xamarin.forms Android

In my xamarin.forms app I have used Firebase Push notifications. In the android portion I can receive notification on both Foreground ,background and killed state. The problem I am facing is when I tap the notification in background state or killed state, I cant get the values from Intent; It shows null. This was working perfectly earlier, I can't figure out what I have done wrong. I can get the values of notification when app is in foreground mode.
My FirebaseMessagingService
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
// private string TAG = "MyFirebaseMsgService";
public static string EmployeeID = "";
public static string StartDate = "";
public static string NotificationType = "";
public static string TotalHours = "";
public static string EmployeeName = "";
public static string EmployeeNumber = "";
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
try
{
SendNotification(message.GetNotification().Body, message.GetNotification().Title, message.Data);
EmployeeID = message.Data["EmpID"].ToString();
StartDate = message.Data["SDate"].ToString();
NotificationType = message.Data["NotificationType"].ToString();
TotalHours = message.Data["TotalHours"].ToString();
EmployeeName = message.Data["EmployeeName"].ToString();
EmployeeNumber = message.Data["EmpNo"].ToString();
}
catch (Exception ex)
{
}
}
private void SendNotification(string messageBody, string messageTitle, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("user_notification_id", EmployeeID);
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this, new Random().Next(), intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID).SetSmallIcon(Resource.Drawable.icon_logo).SetContentTitle(messageTitle).SetContentText(messageBody).SetAutoCancel(true).SetContentIntent(pendingIntent).SetVibrate(new long[] { 1000, 1000 }).SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)).SetStyle((new NotificationCompat.BigTextStyle().BigText(messageBody)));
var notificationManager = NotificationManagerCompat.From(this); notificationManager.Notify(new Random().Next(), notificationBuilder.Build());
}
}
Main Activity
namespace App.Droid
{
[Activity(Label = "App", Icon = "#mipmap/ic_launcher", Theme = "#style/MainTheme", MainLauncher = false ]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
static readonly string TAG = "MainActivity";
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
private bool isNotification = false;
protected override void OnCreate(Bundle savedInstanceState)
{
IsPlayServicesAvailable(); //You can use this method to check if play services are available.
CreateNotificationChannel();// Notification channel is required for Android 8.0 + to receive notifications.
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
// Here Iam getting Intent values as null on background or killed state
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
if (key != null)
{
var value = Intent.Extras.GetString(key);
string EmployeeID = Intent.Extras.GetString("EmpID");
string startDate = Intent.Extras.GetString("SDate");
string NotificationType = Intent.Extras.GetString("NotificationType");
string EmployeeName = Intent.Extras.GetString("EmployeeName");
string TotalHours = Intent.Extras.GetString("TotalHours");
string EmpNo = Intent.Extras.GetString("EmpNo");
LoadApplication(new App(true, EmployeeID, startDate, NotificationType, EmployeeName, TotalHours, EmpNo));
}
}
}
else
{ LoadApplication(new App(isNotification));
}
}
// <-------------- Notification click management in foregorund mode--->
protected override void OnNewIntent(Intent intent)
{
if (intent != null)
{
var message = intent.GetStringExtra("EmpID");
if (!string.IsNullOrEmpty(message))
{
string EmployeeID = intent.GetStringExtra("EmpID");
string startDate = intent.GetStringExtra("SDate");
string NotificationType = intent.GetStringExtra("NotificationType");
string EmployeeName = intent.GetStringExtra("EmployeeName");
string TotalHours = intent.GetStringExtra("TotalHours");
string EmpNo = intent.GetStringExtra("EmpNo");
LoadApplication(new App(true, EmployeeID, startDate, NotificationType, EmployeeName, TotalHours, EmpNo));
}
}
base.OnNewIntent(intent);
}
//<-------------- Checking whether google play service is availabe for fcm-------------------->
public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.Success)
{
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
{
}
// msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode);
else
{
//This device is not supported
Finish(); // Kill the activity if you want.
}
return false;
}
else
{
//Google Play Services is available.
return true;
}
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
{
Description = "Firebase Cloud Messages appear in this channel",
};
channel.EnableVibration(true);
channel.EnableLights(true);
channel.LockscreenVisibility = NotificationVisibility.Public;
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
}
I am using Xamarin.forms version 4.6.0.800 and Xamarin.Firebase.Messaging version 71.1740.4. Any help is appreciated
Edit
The issue was related to the SplashScreen I have. When I removed the SplashActivty, and set MainLauncher is True for MainActivity, the issue solved. I can get the Intent value even if App closed. So how can I solve the issue with Splashscreen ? Should I pass the Intent to MainActivty from SplashActivity?
My SplashActivity
[Activity(Label = "App", MainLauncher = true,
LaunchMode = LaunchMode.SingleTop,
ScreenOrientation = ScreenOrientation.Portrait,
Theme = "#style/splashscreen", NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.DecorView.SystemUiVisibility = StatusBarVisibility.Visible;
Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
}
InvokeMainActivity();
}
private void InvokeMainActivity()
{
var mainActivityIntent = new Intent(this, typeof(MainActivity));
mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
StartActivity(mainActivityIntent);
}
}
First you need to set "click_action":"OPEN_ACTIVITY_1" in your firebase payload.
And then flag your default activity with IntentFilterAttribute
[IntentFilter(new[] { "OPEN_ACTIVITY_1" }, Categories = new[] { "android.intent.category.DEFAULT" })]
with out click_action your notification don't know which activity to be started.
Note: OPEN_ACTIVITY_1 you can change this value but what you should pay attention to is that this value must be the same between Firebase and IntentFilter.
Sorry guys for this silly question. The issue was whenever I click notification the Intent will pass to SplashActivity. I didn't passed the Intent from Splash To Main Activity. Thanks #Cahyo for your help.
I added this in SplashActivity
private void InvokeMainActivity()
{
var mainActivityIntent = new Intent(this, typeof(MainActivity));
if (Intent.Extras != null)
{
mainActivityIntent.PutExtras(Intent.Extras);
}
mainActivityIntent.AddFlags(ActivityFlags.NoAnimation); //Add this line
StartActivity(mainActivityIntent);
}

Retrofit service works only once

I'm using retrofit to login to my API, but if, for example, I insert a wrong password and try to login again, the service is not called again.
It seems that the second call gets "stuck" in the repository, never reaches the service.
This is my first android app and i'm struggling with this situation.
Tks in advance for any help you can provide on this.
This is the code for my app.
DI
class Fiscalizacao : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidModule(this#Fiscalizacao))
bind() from singleton {
Autenticacao(
instance()
)
}
bind<IAutenticacaoDataSource>() with singleton {
AutenticacaoDataSourceImpl(
instance()
)
}
bind<IAuthenticationRepository>() with singleton {
AuthenticationRepositoryImpl(
instance()
)
}
bind() from provider { AutenticacaoViewModelFactory(instance(), instance()) }
}
override fun onCreate() {
super.onCreate()
AndroidThreeTen.init(this)
}
}
Service
interface Autenticacao {
#POST("auth")
fun authAsync(#Body user: RequestBody): Deferred<AutenticacaoResponseResource>
companion object {
operator fun invoke(connectivityInterceptor: IConnectivityInterceptor): Autenticacao {
val okHttpClient = OkHttpClient.Builder()
.build()
return Retrofit
.Builder()
.client(okHttpClient)
.baseUrl("http://10.110.100.216/identity/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(Autenticacao::class.java)
}
}
}
DataSource
class AutenticacaoDataSourceImpl(
private val autenticacao: Autenticacao
) :
IAutenticacaoDataSource {
private val _authResponse = MutableLiveData<AutenticacaoResponseResource>()
override val authResponse: LiveData<AutenticacaoResponseResource>
get() = _authResponse
override suspend fun auth(user: CredentialsResource?): LiveData<AutenticacaoResponseResource> {
try {
val userJsonObject = JsonObject()
userJsonObject.addProperty(Parameters.USERNAME.value, user?.utilizador)
userJsonObject.addProperty(Parameters.PASSWORD.value, user?.password)
val result = autenticacao
.authAsync(
userJsonObject.toString()
.toRequestBody(contentType = "application/json; charset=utf8".toMediaTypeOrNull())
)
.await()
_authResponse.postValue(result)
} catch (e: Exception) {
Log.e(e.cause.toString(), e.message, e)
}
return authResponse
}
}
Repository
class AuthenticationRepositoryImpl(
private val autenticacaoDataSource: IAutenticacaoDataSource
) : IAuthenticationRepository {
override suspend fun auth(user: CredentialsResource?): LiveData<AutenticacaoResponseResource> {
return withContext(Dispatchers.IO) {
return#withContext autenticacaoDataSource.auth(user)
}
}
}
ViewModel
class AutenticacaoViewModel(
private val authenticationRepository: IAuthenticationRepository,
) : ViewModel() {
lateinit var user:CredentialsResource
val login by lazyDeferred {
authenticationRepository.auth(user)
}
}
View Model Factory
class AutenticacaoViewModelFactory(private val authenticationRepository: IAuthenticationRepository, ) :
ViewModelProvider.NewInstanceFactory() {
#Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AutenticacaoViewModel(authenticationRepository) as T
}
}
Coroutines
fun <T> lazyDeferred(block: suspend CoroutineScope.() -> T): Lazy<Deferred<T>>{
return lazy {
GlobalScope.async(start = CoroutineStart.LAZY) {
block.invoke(this)
}
}
}
class AutenticacaoFragment : ScopedFragment(), KodeinAware {
override val kodein by closestKodein()
private val authViewModelFactory: AutenticacaoViewModelFactory by instance()
private lateinit var viewModel: AutenticacaoViewModel
val gson = Gson()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: AutenticacaoFragmentBinding =
DataBindingUtil.inflate(inflater, R.layout.autenticacao_fragment, container, false)
binding.model =
AppGlobalObject
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this, authViewModelFactory)
.get(AutenticacaoViewModel::class.java)
mAutenticacao.setOnClickListener(listenerService)
}
private val listenerService =
View.OnClickListener {
when (it.id) {
R.id.mAutenticacao -> {
login(this.requireContext())
}
}
}
private fun login(context: Context) = launch {
viewModel.user = gson.fromJson(
gson.toJson(AppGlobalObject.autenticacao.user),
CredentialsResource::class.java
)
val result = viewModel.login.await()
result.observe(viewLifecycleOwner, Observer { response ->
if (response == null) return#Observer
val login = Utilities().validateStatusCodeOK(response.error)
when {
login -> {
Utilities().setLoginStatus(login, context)
}
else -> {
mPassword.error = "Erro no Login"
}
}
})
}

I am unable to use searchView with multiple fragments

In my application I have two fragments Fragment Card, Fragment Note. I am using ViewPager and TabLayout to sliding between two fragments. I want to implement a search function in my application.
This is my MainActivity class in Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var viewPager:ViewPager
private lateinit var tabLayout:TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
//toolbar.title = "Cards"
initComponent()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val sv: SearchView = menu!!.findItem(R.id.action_bar_search).actionView as SearchView
val sm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
sv.setSearchableInfo(sm.getSearchableInfo(componentName))
sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item != null) {
when (item.itemId) {
R.id.sort -> {
Toast.makeText(context, "sort note", Toast.LENGTH_SHORT).show()
}
R.id.settings -> {
Toast.makeText(context, "settings note", Toast.LENGTH_SHORT).show()
}
}
}
return super.onOptionsItemSelected(item)
}
private fun initComponent() {
viewPager = findViewById(R.id.view_pager)
setupViewPager(viewPager)
tabLayout = findViewById(R.id.tab_layout)
tabLayout.setupWithViewPager(viewPager)
viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
changeFabIcon(position)
toolbar.title = tabLayout.getTabAt(position)!!.text.toString()
super.onPageSelected(position)
}
})
fabT.setOnClickListener {
//var text = ""
when (viewPager.currentItem) {
0 -> {
// open add new card page
startActivity(Intent(this, AddCardActivity::class.java))
//text = "Add Card"
}
1 -> {
// open add new note page
startActivity(Intent(this, AddNoteActivity::class.java))
//text = "Add note"
}
}
//Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
}
}
private fun changeFabIcon(index: Int) {
fabT.hide()
Handler().postDelayed({
when(index) {
0 -> {
fabT.setImageResource(R.drawable.ic_card)
}
1 -> {
fabT.setImageResource(R.drawable.ic_note)
}
}
fabT.show()
}, 400)
}
private fun setupViewPager(view_pager: ViewPager) {
val mAdapter = ViewPagerAdapter(supportFragmentManager)
mAdapter.addFragment(FragmentCard(), "Cards")
mAdapter.addFragment(FragmentNote(), "Notes")
view_pager.adapter = mAdapter
}
internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
//fragment.arguments = args
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
}
}
This is my FragmentNote class in Kotlin
class FragmentNote: Fragment() {
private var listNotes = ArrayList<Note> ()
private lateinit var mRecyclerViewNote: RecyclerView
private lateinit var mAdapterNote: RecyclerViewAdapterNote
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.note_fragment, container, false)
mAdapterNote = RecyclerViewAdapterNote(context!!, listNotes, this)
mRecyclerViewNote = v.findViewById(R.id.noteRecView) as RecyclerView
mRecyclerViewNote.layoutManager = LinearLayoutManager(context)
mRecyclerViewNote.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
mRecyclerViewNote.adapter = mAdapterNote
setHasOptionsMenu(true)
NoteLoadQuery("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun NoteLoadQuery(title: String) {
var dbManager = DbManagerNote(context!!)
val projections = arrayOf("ID", "Title", "Description")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "ID like ?", selectionArgs, "ID")
listNotes.clear()
// ascending
if (cursor.moveToLast()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val Title = cursor.getString(cursor.getColumnIndex("Title"))
val Description = cursor.getString(cursor.getColumnIndex("Description"))
listNotes.add(Note(ID, Title, Description))
} while (cursor.moveToPrevious())
}
mAdapterNote.notifyDataSetChanged()
}
}
and finally this is my FragmentCard class
class FragmentCard: Fragment() {
private var listCards = ArrayList<Card>()
private lateinit var myRecyclerViewCard: RecyclerView
private lateinit var mAdapter: RecyclerViewAdapterCard
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.card_fragment, container, false)
mAdapter = RecyclerViewAdapterCard(context!!,listCards, this)
myRecyclerViewCard = v.findViewById(R.id.cardRecView) as RecyclerView
myRecyclerViewCard.layoutManager = LinearLayoutManager(context)
myRecyclerViewCard.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
myRecyclerViewCard.adapter = mAdapter
setHasOptionsMenu(true)
CardLoadQueryAscending("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun CardLoadQueryAscending(title: String) {
var dbManager = DbManagerCard(context!!)
val projections = arrayOf("ID", "CardName", "CardNum")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "CardName like ?", selectionArgs, "CardName")
listCards.clear()
// ascending
if (cursor.moveToFirst()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val CardName = cursor.getString(cursor.getColumnIndex("CardName"))
val CardNum = cursor.getString(cursor.getColumnIndex("CardNum"))
listCards.add(Card(ID, CardName, CardNum))
} while (cursor.moveToNext())
}
mAdapter.notifyDataSetChanged()
}
}
I am trying to implement my search view in MainActivity, but I am unable to do. can anyone please give me your valuable suggestions to get my problem solve. thanks in advance.
I have done this, it is quite simple.
Just make your MainActivity own the SearchView. As you already did.
Then make a simple interface
ISearchChangedListener
onTextChanged(newString: String?)
onSearchSubmitted()
Setup your BaseFragment to implement this interface.
Override the implementation in your child fragments of the BaseFragment
Then keep track of mSelectedFragment on PageChanged.
Then you simply call
mSelectedFragment?.onTextChanged(newString)
whenever the parent gets a text changed and handle appropriately.

Resources