I have a RecyclerView but I face some problems. Whenever I add something to the recycler view and for example switch fragments or close the app all the items in the RecyclerView disappear. Would there be a way to save them? Any help would be nice!
Here is some code to see if anyone needs it:
Adapter
package com.example.freetrialtracker;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class SubTrialAdapter extends RecyclerView.Adapter<SubTrialAdapter.MyViewHolder>{
private ArrayList<SubTrial> listData;
private Context context;
private OnEditListener onEditListener;
public SubTrialAdapter(Context context, ArrayList<SubTrial> list,OnEditListener onEditListener){
this.listData=list;
this.context=context;
this.onEditListener=onEditListener;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View viewItem= LayoutInflater.from(parent.getContext()).inflate(R.layout.subscription_card_view,parent,false);
return new MyViewHolder(viewItem);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
SubTrial dataObj=listData.get(position);
holder.nameTxt.setText(dataObj.getNamee());
holder.startDate.setText(dataObj.getStartDate());
holder.endDate.setText(dataObj.getEndDate());
holder.description.setText(dataObj.getDescription());
holder.link.setText(dataObj.getLink());
holder.imgDelete.setOnClickListener(v->{
listData.remove(position);
notifyDataSetChanged();
});
holder.imgEdit.setOnClickListener(v->{
onEditListener.onEditClick(listData.get(position),position);
});
}
#Override
public int getItemCount() {
return listData.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView nameTxt,startDate,endDate,description,link;
ImageView imgEdit,imgDelete;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
nameTxt=itemView.findViewById(R.id.nameTxtId);
startDate=itemView.findViewById(R.id.startDateTxtId);
endDate = itemView.findViewById(R.id.endDateTxtId);
description = itemView.findViewById(R.id.descriptionId);
link = itemView.findViewById(R.id.linkId);
imgEdit=itemView.findViewById(R.id.imgEdit);
imgDelete=itemView.findViewById(R.id.imgDelete);
}
}
public void editData(SubTrial listDataObj,int currentPosition){
listData.get(currentPosition).setLink(listDataObj.getLink());
listData.get(currentPosition).setDescription(listDataObj.getDescription());
listData.get(currentPosition).setEndDate(listDataObj.getEndDate());
listData.get(currentPosition).setStartDate(listDataObj.getStartDate());
listData.get(currentPosition).setNamee(listDataObj.getNamee());
notifyDataSetChanged();
}
public interface OnEditListener{
void onEditClick(SubTrial listCurrentData, int CurrentPosition);
}
}
Fragment
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class SubscriptionFragment extends Fragment implements SubscriptionDialogFragment.OnInputSelected {
AlertDialog alertDialog;
TextView textView1;
RecyclerView subscriptionList;
private FloatingActionButton mOpenDialog;
SubTrialAdapter subscriptionAdapterList;
ArrayList<SubTrial> subTrialArrayList;
#Override
public void sendInput(String name, String startDate, String endDate, String description, String link) {
addSubscription(name, startDate, endDate, description, link);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subscription, container, false);
mOpenDialog = view.findViewById(R.id.fabSub);
subTrialArrayList = new ArrayList<>();
subscriptionList = view.findViewById(R.id.activityListSub);
subscriptionList.setHasFixedSize(true);
subscriptionList.setLayoutManager(new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false));
textView1 = view.findViewById(R.id.textView1);
mOpenDialog.setOnClickListener(v -> {
SubscriptionDialogFragment dialog = new SubscriptionDialogFragment();
dialog.setTargetFragment(SubscriptionFragment.this, 1);
dialog.show(getFragmentManager(), "Dialog");
});
return view;
}
public void addSubscription(String strName, String strStartDate, String strEndDate, String strDescription, String strLink) {
textView1.setText(strStartDate);
SubTrial obj = new SubTrial();
obj.setNamee(strName);
obj.setStartDate(strStartDate);
obj.setEndDate(strEndDate);
obj.setDescription(strDescription);
obj.setLink(strLink);
subTrialArrayList.add(obj);
subscriptionAdapterList = new SubTrialAdapter(this.getContext(), subTrialArrayList, this::onEditClick);
subscriptionList.setAdapter(subscriptionAdapterList);
}
private void onEditClick(SubTrial listCurrentData, int currentPosition) {
View view=LayoutInflater.from(this.getContext()).inflate(R.layout.edit_subscription,null);
AlertDialog.Builder builderObj=new AlertDialog.Builder(view.getContext());
EditText mSubscriptionName = view.findViewById(R.id.subscriptionName);
EditText mStartDate = view.findViewById(R.id.startDate);
EditText mEndDate = view.findViewById(R.id.endDate);
EditText mDescription = view.findViewById(R.id.description);
EditText mLink = view.findViewById(R.id.link);
MaterialButton btnEdit=view.findViewById(R.id.btnEdit);
mSubscriptionName.setText(listCurrentData.getNamee());
mStartDate.setText(listCurrentData.getStartDate());
mEndDate.setText(listCurrentData.getEndDate());
mDescription.setText(listCurrentData.getDescription());
mLink.setText(listCurrentData.getLink());
ImageView closeAlert = view.findViewById(R.id.closeAlert);
builderObj.setView(view);
builderObj.setCancelable(false);
closeAlert.setOnClickListener(v -> {
alertDialog.cancel();
});
btnEdit.setOnClickListener(v->{
String strName = "", strStartDate = "", strEndDate = "", strDescription = "", strLink = "";
if (mSubscriptionName.getText() != null) {
strName = mSubscriptionName.getText().toString();
}
if (strName.equals("")) {
Toast.makeText(this.getContext(), "Please enter Subscription Name", Toast.LENGTH_LONG).show();
return;
}
if (mStartDate.getText() != null) {
strStartDate = mStartDate.getText().toString();
}
if (strStartDate.equals("")) {
Toast.makeText(this.getContext(), "Please enter Start Date", Toast.LENGTH_LONG).show();
return;
}
if (mEndDate.getText() != null) {
strEndDate = mEndDate.getText().toString();
}
if (strEndDate.equals("")) {
Toast.makeText(this.getContext(), "Please enter End Date", Toast.LENGTH_LONG).show();
return;
}
if (mDescription.getText() != null) {
strDescription= mDescription.getText().toString();
}
if (strDescription.equals("")) {
Toast.makeText(this.getContext(), "Please enter Description", Toast.LENGTH_LONG).show();
return;
}
if (mLink.getText() != null) {
strLink = mLink.getText().toString();
}
if (strLink.equals("")) {
Toast.makeText(this.getContext(), "Please enter Link", Toast.LENGTH_LONG).show();
return;
}
editContact(strName, strStartDate, strEndDate, strDescription, strLink, currentPosition);
});
alertDialog=builderObj.create();
alertDialog.show();
}
public void editContact(String strUserName, String strStartDate, String strEndDate, String strDescription, String strLink, int currentPosition){
SubTrial obj = new SubTrial();
obj.setNamee(strUserName);
obj.setStartDate(strStartDate);
obj.setEndDate(strEndDate);
obj.setDescription(strDescription);
obj.setLink(strLink);
subscriptionAdapterList.editData(obj,currentPosition);
alertDialog.cancel();
}
}
Yes. Because once you exit the Application, the app memory is killed in the background process and opening up the app creates a new instance. Let's say for example, you created an editText and button which displays a Toast + text entered by the user to the user when clicked. The app memory will stop once you close and it will shutdown once you remove it from background memory. This method is called onDestroy().
So to prevent this, you can make use of android default local storages e.g
SQlite Database, Shared Preferences, Room Database
1. Sqlite database is android's offline local database which requires no internet access to store data. And data to be stored in SQlite Database should be in strings format like uri path. Storing bigger files or contents like images, audios, videos inside the SQLite database is not advisible to prevent exceptions such as;
FATAL EXCEPTION: main
11-06 15:16:17.199: E/AndroidRuntime(1789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demodbimage/com.example.demodbimage.ImagesList}: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
2. Shared Preferences is good for storing very small data values in form of keys such as strings, boolean, integers. Take for instance, you want to prevent user from logging in after first time login is successful or you want to display dark theme next time once the user opt-in the first time.
3. Room Database is same as Sqlite database but Google recommends us to use it because it's easy to use, also provides databases syntax and very sensitive to errors.
Or you can make use of Online databases eg mySQL, MongoDB, Firebase Database, mariaDb etc.
Basically, RecyclerView is used in conjunction with the database like Room(Local DB) or API(Remote DB such as MySQL).
However, if you are creating a very lightweight project for your portfolio, I don't think it's a bad idea to use a datastore or sharedPrefereces. As with any program, List and Array commonly used in Kotlin are, of course, volatile.
Related
I'm pretty new to Android Studio and Java overall so this might be simple question but I couldn't find any solutions or either couldn't use them to fix my issue.
So I have a RecyclerView which I can insert items from a list of pre-defined items with the function "addItems()" when I press to a button and display them.
Also those items have ImageButtons -which is just a transparent rectangle- to get individual clicks on them.
The purpose of these ImageButtons are to switch to a new activity which I defined in the "addItems()" method.
But the problem is, I can't catch -but they respond to the clicks- click on those items, and also I can't pass the activity class or the layout file.
To be exact, I want to use these buttons to switch to a new activity and display the info of that item there.
It's my first question here, so if I need to show any code, please tell me to.
NewsAdapter.java
package com.example.yiyecek2.Activities;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.yiyecek2.R;
import java.util.List;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder>
{
Context mContext;
List<NewsItem> mData;
public NewsAdapter(Context mContext, List<NewsItem> mData) {
this.mContext = mContext;
this.mData = mData;
}
#NonNull
#Override
public NewsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View layout;
layout = LayoutInflater.from(mContext).inflate(R.layout.lyt_restoran,viewGroup,false);
return new NewsViewHolder(layout);
}
// Changed below ----------
#Override
public void onBindViewHolder(#NonNull NewsViewHolder newsViewHolder, int position) {
// Bind data here
newsViewHolder.tvSellerName.setText(mData.get(position).getSellerName());
newsViewHolder.tvSellerAddress.setText(mData.get(position).getSellerAddress());
newsViewHolder.ivSellerImage.setImageResource(mData.get(position).getSellerImage());
newsViewHolder.tvMinCost.setText(mData.get(position).getMinCost());
newsViewHolder.tvMinTime.setText(mData.get(position).getMinTime());
newsViewHolder.tvDeliveryCost.setText(mData.get(position).getDeliveryCost());
newsViewHolder.tvClassToGo.setText(mData.get(position).getClassToGo());
// Line below is the buttons attached to the items which I aim to use on switching Activities
newsViewHolder.ibGoSellerPage.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(SiparisActivity.this, classDominos.class);
startActivity(intent); // startActivity is marked red
}
});
}
// Changed above ----------
#Override
public int getItemCount() {
return mData.size();
}
public class NewsViewHolder extends RecyclerView.ViewHolder{
TextView tvSellerName, tvSellerAddress, tvMinCost, tvMinTime, tvDeliveryCost, tvClassToGo;
ImageView ivSellerImage;
ImageButton ibGoSellerPage;
public NewsViewHolder(#NonNull View itemView) {
super(itemView);
tvSellerName = itemView.findViewById(R.id.tvFoodName);
tvSellerAddress = itemView.findViewById(R.id.tvFoodDescription);
ivSellerImage = itemView.findViewById(R.id.ivFoodImage);
tvMinCost = itemView.findViewById(R.id.tvFoodCost);
tvMinTime = itemView.findViewById(R.id.tvMinTime);
tvDeliveryCost = itemView.findViewById(R.id.tvDeliveryCost);
tvClassToGo = itemView.findViewById(R.id.tvClassToGo);
ibGoSellerPage = itemView.findViewById(R.id.ibGoSellerPage);
//int position = getAdapterPosition();
//Toast.makeText(mContext.getApplicationContext(), "Position is: "+position, Toast.LENGTH_SHORT).show();
}
}
}
SiparisActivity.java //this is where I list restaurant items with ImageButtons said above.
package com.example.yiyecek2.Activities;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.yiyecek2.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import recyclerview.CustomItemAnimator;
public class SiparisActivity extends AppCompatActivity {
Button btnListRestaurants;
RecyclerView NewsRecyclerView;
NewsAdapter newsAdapter;
List<NewsItem> mData;
ImageButton ibGoSellerPage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_siparis);
// Hide the action bar
getSupportActionBar().hide();
NewsRecyclerView = findViewById(R.id.news_rv);
btnListRestaurants = (Button) findViewById(R.id.buttonListRestaurants);
NewsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
NewsRecyclerView.setHasFixedSize(true);
NewsRecyclerView.setItemAnimator(new CustomItemAnimator());
// Get clicks on the "List Restaurants"
btnListRestaurants.setOnClickListener(new View.OnClickListener() {
int restaurantCount = 0;
#Override
public void onClick(View view) {
Toast toast = Toast.makeText(getApplicationContext(), "Listing Restaurants", Toast.LENGTH_SHORT);
toast.show();
Timer timerToTransition;
timerToTransition = new Timer();
TimerTask task = new TimerTask() {
public void run() {
addItems();
restaurantCount++;
if (restaurantCount > 9)
{
System.out.println(restaurantCount);
timerToTransition.cancel(); // Stops the timer when theres 10 Restaurants listed
}
}
};
timerToTransition.scheduleAtFixedRate(task,0,300); // waits 300ms before creating a new Restaurant
}
});
if (ibGoSellerPage != null) // Check if Restaurant button exists
{
System.out.println("ON CLICK HERE");
ibGoSellerPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("ON CLICK HERE");
}
});
}
mData = new ArrayList<>();
// fill list news with pre defined data
// Adapter ini and setup
newsAdapter = new NewsAdapter(this,mData);
NewsRecyclerView.setAdapter(newsAdapter);
NewsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void addItems()
{
RecyclerView.State state = null;
int switchInt = 0; // To use in the switch
// Random integer list to assign switchInt a random value
int[] intList;
intList = new int[]{0,1,2,3,4,5,6,7};
// Pick a random int
Random rand = new Random();
int rndInt = rand.nextInt(5);
// Check if the array's index isn't empty(-1)
while (intList[rndInt] != -1)
{
switchInt = (int) intList[rndInt];
intList[rndInt] = -1;
}
//System.out.println(rndInt);
switch(switchInt) {
case 0:
mData.add(0,new NewsItem("Domino's Pizza","Sarıgöl, Ordu Cd. No:128, 34240 Gaziosmanpaşa/İstanbul",
R.drawable.dominos,"32 TL","30 dk.","0 TL","classDominos")); // I'm trying to catch that "classDominos" to use to switch Activity
break;
case 1:
mData.add(0,new NewsItem("Migros","Bağlarbaşı, Küçükköy Yolu Cd., 34245 Gaziosmanpaşa/İstanbul",
R.drawable.migroslogo,"32 TL","25 dk.","0 TL", "classDominos"));
break;
case 2:
mData.add(0,new NewsItem("KFC","Yeşilpınar Mah. Şehit Metinkaya Sok Vialand AVM No:11 Mağaza No:237, 34065 Eyüpsultan",
R.drawable.kfclogo,"32 TL","35 dk.","3 TL", "classDominos"));
break;
case 3:
mData.add(0,new NewsItem("Popeyes","Yeşilpınar Mah. Şehit Metin Kaya Sok. No:11 K:3 Vialand AVM, 34065",
R.drawable.popeyeslogo,"32 TL","35 dk.","3 TL", "classDominos"));
break;
case 4:
mData.add(0,new NewsItem("Mado","İslambey, Hz. Halid Blv. No:43 D:B, 34050 Eyüpsultan/İstanbul",
R.drawable.madologo,"32 TL","35 dk.","3 TL", "classDominos"));
break;
default:
break;
}
System.out.println("Added item");
newsAdapter.notifyItemInserted(0);
NewsRecyclerView.getLayoutManager().smoothScrollToPosition(NewsRecyclerView, state, 0);
// Un-commenting the lines below crash the app when "Listing Restaurants" (Creating items)
//ibGoSellerPage = (ImageButton) findViewById(R.id.ibGoSellerPage);
/*ibGoSellerPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("ON CLICK HERE");
}
});
*/
}
}
I can see the individual buttons in the profiler but how to catch clicks on them? (You can see I'm holding the button on the right)
Profiler Screenshot
I think the problem with you is that the click event happen on the screen in another view
By making a transparent rectangle i think the click go to the parent view
Try to test that for ex:
<LinearLayout
android:id="#+id/layout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
handle the click on layout view if it work so you need to change the transparent rectangle button to a bold one or another approach
better to add some code from your side to help us understand the problem well
I am trying to change icon image according to the item data . when pressing the icon, it should switch to other icon and change the item data. everything is going right but when i scroll up or down the new icon image change its place to some other items because i am using list view with adapter. how can i keep the new icons in the pressed items without mixing with other items.
package com.example.sairamkrishna.handymade;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HandAdapter extends ArrayAdapter<HandClass> {
private int myColor;
public HandAdapter(Context context, ArrayList<HandClass> objects, int my_Color) {
super(context, 0, objects);
myColor = my_Color;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View myView = convertView;
final HandClass myData = getItem(position);
if (myView == null) {
myView = LayoutInflater.from(getContext()).inflate(
R.layout.label_item, parent, false);
}
ImageView aImage = (ImageView) myView.findViewById(R.id.itemImage);
aImage.setImageResource(myData.getClsImage());
TextView aName = (TextView) myView.findViewById(R.id.itemName);
aName.setText(myData.getClsName());
FrameLayout aColor = (FrameLayout) myView.findViewById(R.id.itemColor);
aColor.setBackgroundColor(myColor);
final ImageView aAddToBasket = (ImageView) myView.findViewById(R.id.itemAddToBasket);
aAddToBasket.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Boolean aClsToBasket = (Boolean) myData.getClsToBasket();
if (aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
myData.setClsToBasket(false);
Toast.makeText(getContext(), "Remove from basket"+ position, Toast.LENGTH_SHORT).show();
} else {
// if (!aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
myData.setClsToBasket(true);
Toast.makeText(getContext(), "Add to basket"+ position, Toast.LENGTH_SHORT).show();
}
}
});
ImageView aAddToFavorite = (ImageView) myView.findViewById(R.id.itemAddToFavorite);
aAddToFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Favorite - List item was clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return myView;
}
}
A fundamental concept of ListView and RecyclerView is that you need to "update" the value (in this case, the image) for each portion of the row every time getView() or onBindViewHolder() is called.
For example, every time getView() is called, you're always updating the (text) value of TextView aName. That is, there is always a call to aName.setText().
Right now, the only time you call aAddToBasket.setImageResource() is inside an OnClickListener. Of course, it makes sense to do it here, but you must also update the image outside of the listener.
Add this code right after your ImageView aAddToBasket line:
if ((Boolean) myData.getClsToBasket()) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
} else {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
}
Add this line after icon change.
adapter.notifyDataSetChanged();
I am trying to display CardViews inside a RecyclerView, each card will represent a cheese object.
This cheese object has 6 instance variables.
This is my Cheese.java :
public class Cheese {
private String CheeseName;
private String CheeseCountryOfOrigin;
private String CheeseDayMade;
private String CheeseDayExpire;
private String CheeseDescription ;
private String CheesePrice;
public Cheese(){} //Required for firebase
public Cheese(String CheeseName, String CheeseCountryOfOrigin, String CheeseDayMade, String CheeseDayExpire, String CheeseDescription, String CheesePrice) {
this.CheeseName = CheeseName;
this.CheeseCountryOfOrigin = CheeseCountryOfOrigin;
this.CheeseDayMade = CheeseDayMade;
this.CheeseDayExpire = CheeseDayExpire;
this.CheeseDescription = CheeseDescription;
this.CheesePrice = CheesePrice;
}
public String getCheeseName() {
return CheeseName;
}
public String getCheeseCountryOfOrigin() {
return CheeseCountryOfOrigin;
}
public String getCheeseDayMade() {
return CheeseDayMade;
}
public String getCheeseDayExpire() {
return CheeseDayExpire;
}
public String getCheeseDescription() {
return CheeseDescription;
}
public String getCheesePrice() {
return CheesePrice;
}
}
and this is my cheese_card.xml (I hardcoded some android:text for better understanding): cheese_card.xml
my RecyclerView is in a fragment.
This is my fragment:
fragment_cheeses_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cheeses_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
all my cheese items are already in my Firebase Real-Time Database. To make my life simpler I am trying to use FirebaseUI to populate my RecyclerView with data from my Firebase database.
This is my CheesesListFragment.java, which is displayed in my MainActivity:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class CheeseListFragment extends Fragment {
private static final String TAG = "CheesesListFragment";
private FirebaseDatabase aFirebaseDatabase;
private DatabaseReference aCheesesDatabaseReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView Started Successfully");
//Create the recycler view object
RecyclerView cheesesRecycler = (RecyclerView) inflater.inflate(R.layout.fragment_cheeses_list, container, false);
//Add a grid layout manager to the recycler view
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
cheesesRecycler.setLayoutManager(layoutManager);
cheesesRecycler.setHasFixedSize(true);
aFirebaseDatabase = FirebaseDatabase.getInstance();
aCheesesDatabaseReference = aFirebaseDatabase.getReference().child("cheeses");
//Query the cheeses in firebase db using firebaseUI instead of addChildEventListener
Query query = aCheesesDatabaseReference;
//configuration for the FirebaseRecyclerAdapter
FirebaseRecyclerOptions<Cheese> options =
new FirebaseRecyclerOptions.Builder<Cheese>()
.setQuery(query, Cheese.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Cheese, CheeseViewHolder>(options) {
#Override
public CheeseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.cheese_card for each item
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheese_card, parent, false);
return new CheeseViewHolder(cv);
}
#Override
protected void onBindViewHolder(CheeseViewHolder holder, int position, Cheese model) {
CheeseViewHolder myHolder = (CheeseViewHolder)holder;
myHolder.cheeseName.setText(model.getCheeseName());
myHolder.cheeseCountryOfOrigin.setText(model.getCheeseCountryOfOrigin());
myHolder.cheeseDayMade.setText(model.getCheeseDayMade());
myHolder.cheeseDayExpire.setText(model.getCheeseDayExpire());
myHolder.cheeseDescription.setText(model.getCheeseDescription());
myHolder.cheesePrice.setText(model.getCheesePrice());
}
};
//Set the adapter to the recycle View
cheesesRecycler.setAdapter(adapter);
return cheesesRecycler;
}
public static class CheeseViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView CheeseName;
TextView CheeseCountryOfOrigin;
TextView CheeseDayMade;
TextView CheeseDayExpire;
TextView CheeseDescription;
TextView CheesePrice;
public CheeseViewHolder (CardView v){
super(v);
cardView = v;
CheeseName = (TextView)cardView.findViewById(R.id.cheese_name);
CheeseCountryOfOrigin= (TextView)cardView.findViewById(R.id.cheese_origin);
CheeseDayMade= (TextView)cardView.findViewById(R.id.cheese_day_made);
CheeseDayExpire= (TextView)cardView.findViewById(R.id.cheese_day_expire);
CheeseDescription= (TextView)cardView.findViewById(R.id.cheese_description);
CheesePrice= (TextView)cardView.findViewById(R.id.cheese_price);
}
}
}
So my questions are: (answering any of them is welcomed and very helpful)
If i get it right, onCreateViewHolder is supposed to make ViewHolders for my Cheese object using my cheese_card.xml . if so, assuming I delete onBindingViewHolder am I suppose to see lots of view holders that look like my cheese_card.xml?
in onBindingViewHolder in setText : how can I get my TextViews to get a value from my firebase?
I am new to programming and not sure about onCreateViewHolder, onBindingHolder and cheesesViewHolder.I am not sure what every code I writed there means as some of them are copy-pasted.If I got it all wrong, can you please explain how can I reach my desired outcome, and what I did wrong?
Thank you, in advance :)
Modify onBindingViewHolder and cheesesViewHolder. Because in onBindingViewHolder you will bind data with Views not Views with they ids. Bind Views with they ids inside cheesesViewHolder. For example:
CardView cardView;
TextView cheese_name;
TextView cheese_origin;
public CheeseViewHolder(CardView v) {
super(v);
cardView = v;
cheese_name = (TextView) cardView.findViewById(R.id.cheese_name);
cheese_origin = (TextView) cardView.findViewById(R.id.cheese_origin);
// and so on...
}
Then inside onBindingViewHolder you will do something like this:
#Override
protected void onBindViewHolder(CheeseViewHolder holder, int position, Cheese model) {
cheesesViewHolder myHolder = (cheesesViewHolder)holder;
myHolder.cheese_name.setText(model.getCheeseName());
myHolder. cheese_origin.setText(model.getCheeseOrigin());
//and so on...
}
I was able to eventually fix my problem and get onCreateViewHolder and onBindViewHolder to start simply by adding
adapter.startListening();
to my onStart method. like this:
#Override
public void onStart() {
super.onStart();
Log.e(TAG,"onStart Started Successfully");
adapter.startListening();
}
And I edited the code using #Yupi suggestion.
I am making android app that takes user input from EditText on press of button from button.setOnClick() and use the string.getText() to search the database. But the string becomes null and no way i can send it
DbBackend.class where the search function is.
I checked and database works fine on static query which you can set to = "xyz" but no way i am able to use the user input to query the database.
Here is my code: MainActivity.java
package com.vadhod.dictcopy;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.sql.Array;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import static android.app.PendingIntent.getActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText searchBar;
Button searchButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DbBackend dbBackend = new DbBackend(MainActivity.this);
searchBar = (EditText) findViewById(R.id.searchBar);
searchButton = (Button) findViewById(R.id.searchButton);
textView = (TextView)findViewById(R.id.textView);
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
String input = searchBar.getText().toString();
ArrayList<String> terms = dbBackend.getList(input);
StringBuilder builder = new StringBuilder();
for (String details : terms) {
builder.append(details + "\n");
}
textView.setText(builder.toString());
}catch (Exception e){
e.printStackTrace();
}
}
});
}
}
DbBackend.java
package com.vadhod.dictcopy;
import ...
public class DbBackend extends DatabaseObject {
public DbBackend(Context context){
super(context);
}
public ArrayList<String> getList(String search){
ArrayList<String> kWords = new ArrayList<String>();
String searchQuery = "SELECT kName FROM dictionary WHERE eName LIKE '%" + search + "%'";
try {
Cursor kCursor = this.getDbConnection().rawQuery(searchQuery, null);
if (kCursor != null){
kCursor.moveToFirst();
for (int i = 0; i < kCursor.getCount(); i++ ){
String sword = kCursor.getString(kCursor.getColumnIndexOrThrow("kName"));
kWords.add(sword);
kCursor.moveToNext();
}
kCursor.close();
return kWords;
}
return kWords;
}catch (Exception e){
e.printStackTrace();
}
return kWords;
}
}
Any help please on how to use the string when user press the search button and search that through the database and return the query?
Updated the moved code
Assuming that inside the searchButton.setOnClickListener(new View.OnClickListener() {...}); the search is working, and outside - not. The problem is your
//Option 1 not working
ArrayList<String> terms2 = dbBackend2.getList(input);
is called during the Activity setup before any view is shown to the user. So, the input is not filled at that time. You have to move this code into an onclick listener, like you did in the code above these lines.
Or provide some other source for the input.
Upd:
For the NullPointer: in your layout file activity_main you need to have a TextView with id textView. Because you do the following:
// find the textview in the layout
textView = (TextView)findViewById(R.id.textView);
// do the search, get results
...
// set textview's text, at this moment textView must not be null
textView.setText(kbuilder.toString());
I'm working on an Android app so I can learn mobile dev and I'm stuck with this problem.
Basically in my SampleFragment class I have an adapter called mAdapter and when my TestService gets my data objects and updates my dataObjects arrayList and notifies the adapter that the data has changed the adapter isn't initialized and is null at the time. Is it a thread issue or is it associated with the fragment lifecycle?
SampleFragment.java
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class SampleFragment extends Fragment {
private static final int SEND_DELAY = 1500;
private String userName, sEventId;
private EditText etMessage;
private ImageButton btSend;
private Context applicationContext;
private View view;
private Handler handler = new Handler();
private ArrayList<Message> dataObjects = new ArrayList<>();
private MessageListAdapter mAdapter;
private Runnable initMessageAdapter = new Runnable() {
#Override
public void run() {
initMessageAdapter();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String eventName = CurrentActiveEvent.getInstance().getEventName();
Activity activity = getActivity();
activity.setTitle(eventName);
mAdapter = new MessageListAdapter(context, userName, dataObjects);
CurrentActiveUser currentUser = CurrentActiveUser.getInstance();
userName = currentUser.getUsername();
Intent intent = new Intent(activity, TestService.class);
activity.startService(intent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_messaging, container, false);
applicationContext = getActivity();
sEventId = CurrentActiveEvent.getInstance().getEventID();
btSend = (ImageButton) view.findViewById(R.id.btSend);
handler.post(initMessageAdapter);
btSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.post(new Runnable() {
#Override
public void run() {
saveMessage(body);
}
});
}
});
return view;
}
// Setup message field and posting
private void initMessageAdapter() {
etMessage = (EditText) view.findViewById(R.id.etMessage);
ListView lvChat = (ListView) view.findViewById(R.id.lvChat);
lvChat.setAdapter(mAdapter);
}
public void updatedataObjects(List<objs> newdataObjects){
this.dataObjects.clear();
this.dataObjects.addAll(newdataObjects);
mAdapter.notifyDataSetChanged();
}
}
TestService.java
import android.app.IntentService;
import android.content.Intent;
import java.util.Collections;
import java.util.List;
public class TestService extends IntentService {
private static final int MAX_RESULTS = 50;
private static final String CLASS_NAME = TestService.class.getSimpleName();
private final String sEventId = CurrentActiveEvent.getInstance().getEventID();
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public TestService() {
super(CLASS_NAME);
}
#Override
#SuppressWarnings("unchecked")
protected void onHandleIntent(Intent intent) {
if (NetworkState.isConnected(getApplicationContext())) {
Query query = new Query(Data.class);
query.whereEqualTo(Events.ID, sEventId);
query.orderByDESC(Data.CREATED_AT);
query.setLimit(MAX_RESULTS);
List objs = queryDB(query, Data.class.getSimpleName());
if (objs != null) {
Collections.reverse(objs);
new SampleFragment().updateMessages(objs);
}
}
}
}
Your problem comes from this line:
new SampleFragment().updateMessages(objs);
You are creating a new instance of your fragment inside your service. Since you are not attaching the fragment anywhere, it's lifecycle is not started and the onCreate() method is never called, which results in the NullPointerException.
IntentServices are great for executing tasks on a background thread, but they are components, that are meant to be separated from the UI - related components, like Activities and Fragments. You should never have direct communication between an IntentService and a Fragment or Activity. If you need to return a result from your IntentService, you should consider using the LocalBroadcastManager. It will fire an intent, containing the result, and you can register receivers to intercept it.
There are also other options, like Bound Services - they are created to provide an interface for their clients, and you can use this to return your result. But bear in mind, that, unlike IntentService they don't work in a background thread by default.
Since you are trying to work with a database, I recommend you take a look and the ContentProvider and ContentResolver classes. They are the recommended way of working with DBs in Android and come with loads of neat stuff.