Displaying CardViews inside a RecyclerView using FirebaseUI and Firebase database - java

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.

Related

How to save items added in RecyclerView?

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.

Converting RecyclerView to ViewPager / PagerAdapter

Background - New to Android, but pretty nifty with moving layouts around, understanding Java, Kotlin and XML. However this task seems to be way above my head.
Problem - I'm looking to convert the following Java file (RecyclerView) into a Kotlin file (ViewPager) - since I already have a ViewPager hooked-up with the same scrolling behaviour as desired. I get the impression it's a 10min job for a seasoned developer. If that's the case I wonder if I could call upon some assistance from the community? At least to work out where to start. I can't seem to find a guide on how to convert a RecyclerView into a PagerAdapter or ViewPager.
Essentially the existing ViewPager I'm using has static data (5 items) and this one could have tens of items, so needs to be dynamic with a separate datasource (items of CardItem).
RecyclerViewAdapter - Current (Java)
package com.APPNAME.fragments.cards;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import com.APPNAME.R;
import com.APPNAME.databinding.FragmentCardsRecentlyViewedBinding;
import com.APPNAME.model.cardItem.CardItem;
public class RecentlyViewedAdapter extends RecyclerView.Adapter<RecentlyViewedAdapter.RecentlyViewedViewHolder> {
public OnCardClicked listener;
private ArrayList<CardItem> items = new ArrayList<>();
public void addItems(ArrayList<CardItem> list) {
items = list;
notifyDataSetChanged();
}
#Override
public RecentlyViewedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
FragmentCardsRecentlyViewedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_cards_recently_viewed, parent, false);
return new RecentlyViewedViewHolder(binding);
}
#Override
public void onBindViewHolder(RecentlyViewedViewHolder holder, int position) {
holder.binding.setViewModel(new RecentlyViewedViewModel(items.get(position)));
}
#Override
public int getItemCount() {
return items.size();
}
interface OnCardClicked {
void onCardClicked(View view, CardItem cardItem);
}
class RecentlyViewedViewHolder extends RecyclerView.ViewHolder {
FragmentCardsRecentlyViewedBinding binding;
public RecentlyViewedViewHolder(FragmentCardsRecentlyViewedBinding itemView) {
super(itemView.getRoot());
binding = itemView;
binding.cardView.setOnClickListener(v -> {
if (listener != null) {
listener.onCardClicked(v, items.get(getAdapterPosition()));
}
});
}
}
}
ViewPagerAdapter - Future (Kotlin)
package com.APPNAME.fragments.cards
import android.support.annotation.DrawableRes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
import kotlinx.android.synthetic.main.fragment_cards_recently_viewed.view.*
import com.APPNAME.R
import com.APPNAME.activities.BaseActivity
import com.APPNAME.activities.cards.NewCardActivity
import com.APPNAME.model.cardItem.CardItem
import com.APPNAME.views.wrapContentViewPager.ObjectAtPositionPagerAdapter
class RecentlyViewedItemAdapter constructor(private val activity: BaseActivity) : ObjectAtPositionPagerAdapter() {
private var items = ArrayList<CardItem>()
override fun instantiateItemObject(container: ViewGroup, position: Int) : Any {
return getImageView(container, R.drawable.placeholder_card_image) { NewCardActivity.start(activity, it) }
}
private fun getImageView(container: ViewGroup, #DrawableRes imageResourceId: Int, onClick: (imageResourceId: Int) -> Unit = {}): View {
val layoutInflater = LayoutInflater.from(container.context)
val layout = layoutInflater.inflate(R.layout.fragment_cards_recently_viewed, container, false)
val image = layout.recentlyViewedImage
image.setImageResource(imageResourceId)
image.setOnClickListener { onClick(imageResourceId) }
container.addView(layout)
return layout
}
override fun isViewFromObject(view: View, anObject: Any) = (view == anObject)
override fun getCount() = 5 //Placeholder
override fun destroyItemObject(container: ViewGroup, position: Int, view: Any) {
container.removeView(view as View)
}
}
If you have relatively large number of items and need views to be recycled then the RecyclerView is the right option for you. It will be definitely easier than creating a custom view pager adapter with a fixed item count and a view holder pattern.
The default/generic PageAdapter is not recycling anything - it's added to make sure you have all your views initiated and ready to swipe through. However, you can use https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html that will destroy/recreate the fragments when no longer used or reused.
Job done. I actually surprised myself by being able to modify the recycler view to mimmick the PageView's behaviour precisely (including padding, snap scrolling etc). Thanks to the simple subclass SnapHelper. Saved me a tonne of work, so no need to refactor the codebase.
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
PagerSnapHelper solved my problem.
binding.recyclerViewHomeAnnouncement.apply {
layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
binding.recyclerViewHomeAnnouncement.layoutManager = layoutManager
setHasFixedSize(true)
itemAnimator = DefaultItemAnimator()
adapter = homeAnnouncementAdapter
}
val snapHelper: SnapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(binding.recyclerViewHomeAnnouncement)

Exchanging icons in listview items

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();

How to create a ListView with multiple views

my problem is - how to create custom list view with not just repeating one custom view, but like in Instagram or other app where list include other views in it, which it looks like scroll view with list view android other views in it, but Roman Guy says "List View in a Scroll view is a very poor way", and I'm agreed with it, don't believe which is Google use this way...
What are the best way to achieve this thing with ListView or Recycler View
To achieve that UI, you must define multiple view types for your Listview or Recyclerview; a very similar question has been answered here.
In your example, you will have two view types:
<Horizontal Scroll> which is an embedded horizontal Recyclerview/Listview.
<View> which is a view type that you have defined.
There are many tutorials on this concept. I would recommend you to use Recyclerview in your implementation due to its advantages over Listview.
Hey this is how your main fragment will look like :
package com.leoneo.stackoverflow;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.soulpatch.stackoverflow.R;
import java.util.ArrayList;
public class RecyclerViewExample extends Fragment {
private ArrayList<Object> mValues = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = inflater.inflate(R.layout.recycler_view_example, container, false);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final MultpleItemAdapter adapter = new MultpleItemAdapter(mValues);
recyclerView.setAdapter(adapter);
return recyclerView;
}
}
And here's your adapter code.
package com.leoneo.stackoverflow;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.soulpatch.stackoverflow.R;
import java.util.ArrayList;
public class MultpleItemAdapter extends RecyclerView.Adapter<MultpleItemAdapter.ViewHolder> {
private ArrayList<Object> mValues = new ArrayList<>();
public MultpleItemAdapter(ArrayList<Object> values) {
mValues = values;
}
enum ItemType {
TYPE_A,
TYPE_B;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
//Inflate Type A layout
final LinearLayout linearLayoutA = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_a, parent, false);
//And pass the view to the ViewHolder
return new ViewHolder(linearLayoutA);
break;
case 2:
//Inflate Type B layout
final LinearLayout linearLayoutB = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_b, parent, false)
//And pass the view to the ViewHolder
return new ViewHolder(linearLayoutB);
break;
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 1:
final TypeA typeA = (TypeA) mValues.get(position);
//Deal with the views that you defined for LinearLayout A
break;
case 2:
final TypeB typeB = (TypeB) mValues.get(position);
//Deal with the views that you defined for LinearLayout B
break;
}
}
#Override
public int getItemCount() {
return mValues.size();
}
#Override
public int getItemViewType(int position) {
final Object obj = mValues.get(position);
if (obj instanceof TypeA) {
return ItemType.TYPE_A.ordinal();
} else if (obj instanceof TypeB) {
return ItemType.TYPE_B.ordinal();
}
return super.getItemViewType(position);
}
static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
//Class that I want to be displayed in a CardView
private class TypeA {
}
//Class that I want to be displayed in a a LinearLayout
private class TypeB {
}
}
You can have as many classes as you want like TypeA and TypeB and add types to ItemType class as well accordingly.
Rest should be pretty self explanatory assuming you've worked with RecyclerViews before.
You are looking for different View types. It's possible with using these
GetViewTypeCount() this is an overridable method which returns how many view type you have in your listview-recycleview.
getItemViewType(int pos) get which item view type should return at the given position
For example if you want to add a different view in every 10 item, and there is only 2 type of views, you should use a code like this:
#Override
public int getItemViewType(int position){
if (position % 10 == 0)
return SECOND_ITEM;
return FIRST_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
And at the getView() function you can handle it with a switch-case or if-else structure like this:
switch (getItemViewType(cursor.getPosition())) {
case SECOND_ITEM:
//something to do
break;
default:
//something to do
}
You might want to use 2 different layout file to inflate in the switch-case statement above. However, if the layouts of both items are not different that much, I recommend to create just 1 layout, and make views inside it visible and gone according to item you want to get.
Oh and in case you don't know where to use them, you use them at your adapter class. The functions may vary as which kind of adapter you use however, they all work with the same logic.
And finally, I recommend you to use recyclerview. It is just a bit harder to implement than listview but it is a great substitution of listview which is more powerful and flexible. You can do a research for it.
Here is how you can achieve this like instagram, facebook etc. : You inflate a scrollable view at the given positions.
I hope the answer helps.
As always,
Have a nice day.

Fragments: how to change content?

I have a class that manages a fragment .. in a TextView I have to put the phone features, but because by mistake?
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) findViewById(R.id.textView3);
text.setText(scheda);
private TextView findViewById(int textview3) {
// TODO Auto-generated method stub
return null;
}
}
I did not understand a thing. I have three fragment, one of which is called Home, one faq and one about. How do I change the contents of the various "screens"?
Firstly you cannot have operations on objects in the main class body without a helper method in java. I also see when you are creating a reference to you TextView, you need to find the TextView from your inflated view, so the right way to do it would be something like
TextView text = (TextView)view.findViewById(R.id.textview3);
Lastly, when inflating the view, pass the ViewGroup as the parent for that view, and a false for the attachToRoot
You should have something like:
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page,container,false);
String model = Build.MODEL;
String product = Build.PRODUCT;
String androidOS = Build.VERSION.RELEASE;
String scheda = model+"\n"+androidOS+" \n "+product+"";
TextView text = (TextView) view.findViewById(R.id.textView3);
text.setText(scheda);
return view;
}
}
Hope that helps

Categories