RatingBar and RecyclerView - android

I am trying to make an application that rates 8 different types of foods with simple rater and recyclerview. Pressing the FAB button just adds a random food with 0 rating. I have gotten most of it to work except the rating of the rating bar. When I scroll up or down, I lose the ratings I had. How can I store the ratings of food when I scroll up or down.
RecyclerView:
package com.mycompany.alawamhm.foodrater;
import android.content.Context;
import android.graphics.Movie;
import android.icu.text.AlphabeticIndex;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.xml.transform.sax.SAXSource;
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder>{
private Random mRandom = new Random();
private RecyclerView mRecyclerView;
final HashMap<String,Integer> defaultName;
final ArrayList<String> mNames=new ArrayList<>();
public FoodAdapter(RecyclerView rv){
defaultName = new HashMap<>();
defaultName.put("banana", R.drawable.banana);
defaultName.put("broccoli", R.drawable.broccoli);
defaultName.put("homemade bread", R.drawable.bread);
defaultName.put("chicken", R.drawable.chicken);
defaultName.put("chocolate", R.drawable.chocolate);
defaultName.put("ice cream", R.drawable.icecream);
defaultName.put("lima beans", R.drawable.limabeans);
defaultName.put("steak", R.drawable.steak);
for(String name : defaultName.keySet()){
mNames.add(name);
}
mRecyclerView = rv;
}
public String getRandomName(){
String[] names = new String[]{
"banana","broccoli","homemade bread","chicken",
"chocolate","ice cream","lima beans","steak"
};
return names[mRandom.nextInt(names.length)];
}
public void addName(){
mNames.add(0,getRandomName());
notifyItemInserted(0);
mRecyclerView.getLayoutManager().scrollToPosition(0);
}
#Override
public FoodAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.name_view,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = mNames.get(position);
holder.mNameTextView.setText(name);
holder.mImage.setImageResource(defaultName.get(name));
holder.rBar.setRating(0);
}
#Override
public int getItemCount() {return mNames.size(); }
public void removeName(int position){
mNames.remove(position);
notifyItemRemoved(position);
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView mNameTextView;
private ImageView mImage;
SimpleRatingBar rBar;
public ViewHolder(View itemView) {
super(itemView);
mNameTextView = (TextView)itemView.findViewById(R.id.name_view);
mImage = (ImageView)itemView.findViewById(R.id.imageView);
rBar = (SimpleRatingBar)itemView.findViewById(R.id.ratingBar);
}
}
}

Currently the ratings bar is being set to 0 every time a view is bound to a view holder. Set a listener on the rating bar that runs a method to store the new rating value in a hashSet. Then in the adapter, instead of setting the rating bar to 0, add a helper method that iterates through the hashMap to check if a value has been stored for the food being inflated. This method should return the stored value or 0 if the food hasn't stored a value yet.

Related

Recycle view is showing empty field

I this image I getting error recycler view is showing the empty rows did not understand why
Below is my adapter code Please help me I am new to andorid
package com.business.kraftpaper.adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.business.kraftpaper.R;
import com.business.kraftpaper.pojo.PartyStatementPOJO;
import com.business.kraftpaper.pojo.leftDashboardPOJO;
import java.util.List;
public class Party_Statement_Adapter extends RecyclerView.Adapter<Party_Statement_Adapter.ViewHolder> {
private List<PartyStatementPOJO> list_data;
private Context context;
public Party_Statement_Adapter(List<PartyStatementPOJO> list_data, Context context) {
this.list_data = list_data;
this.context = context;
}
#Override
public Party_Statement_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.allreportsadapterlayout_new,parent,false);
return new Party_Statement_Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(Party_Statement_Adapter.ViewHolder holder, int position) {
final PartyStatementPOJO reportsPOJO = list_data.get(position);
if (reportsPOJO.getBalance().equals("null")){
holder.parentViewDetails.setVisibility(View.GONE);
}else{
holder.order_number.setText(reportsPOJO.getBalance());
holder.order_date.setText(reportsPOJO.getDate());
holder.order_for.setText(reportsPOJO.getParticular());
holder.mill_name.setText(reportsPOJO.getDebit_credit());
}
}
#Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView order_date, order_for, mill_name, order_number, billto, shipto;
private ImageView lenaAmountImageView, denaAmountImageView;
private Button layoutStartAction;
private LinearLayout parentViewDetails, layout_receipt_name;
public ViewHolder(View itemView) {
super(itemView);
order_date = itemView.findViewById(R.id.date_text);
order_for = itemView.findViewById(R.id.credit_text);
mill_name = itemView.findViewById(R.id.debit_text);
order_number = itemView.findViewById(R.id.balance_text);
parentViewDetails = itemView.findViewById(R.id.parentViewDetails);
parentViewDetails.setVisibility(View.VISIBLE);
layout_receipt_name = itemView.findViewById(R.id.layout_receipt_name);
layout_receipt_name.setVisibility(View.GONE);
}
}
}
This is adapter code did not understand why this error is coming
I did not understand why this error is coming I have tried to clear the list but didnot work
also not getting null data from the server checked in logcat
Please Help me
Thanks in Advance
Inside onBindViewHolder you have used "null" as a string it should be null
if (reportsPOJO.getBalance().equals("null")) -- incorrect
better to use
if (TextUtils.isEmpty(reportsPOJO.getBalance())
and set the visibility to gone.

How to set OnclickListener for the dynamically added items in recyclerview?

In my application I'm using a recycler view to show some data and for each item in recyclerview I'm adding a list of checked text view. These checked textviews are added according to the size of list provided and it works fine. Now I want to add the click listener for those items to know if they are checked or not?
When items are checked those checked items should be taken to another activity. But now the click listener is added directly in recycler view which won't access me to do out of box code. So I'm looking for how to add a click listener and access it in another activity.
My Recyclerview code:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.allio.customer.R;
import com.allio.customer.models.Types_Item;
import com.bumptech.glide.Glide;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.github.florent37.expansionpanel.ExpansionLayout;
import com.github.florent37.expansionpanel.viewgroup.ExpansionLayoutCollection;
public class Types_adapter extends FirestoreRecyclerAdapter<Types_Item, Types_adapter.RecyclerHolder> {
private final ExpansionLayoutCollection expansionsCollection = new ExpansionLayoutCollection();
Context context;
public Types_adapter(#NonNull FirestoreRecyclerOptions<Types_Item> options, Context context) {
super(options);
expansionsCollection.openOnlyOne(true);
this.context = context;
}
#Override
protected void onBindViewHolder(#NonNull RecyclerHolder holder, int position, #NonNull Types_Item model) {
expansionsCollection.add(holder.getExpansionLayout());
holder.textView.setText(model.getName());
Glide.with(holder.imageView.getContext())
.load(model.getImageURL())
.into(holder.imageView);
CheckedTextView[] textView = new CheckedTextView[model.getProblems().size()];
holder.problems.removeAllViews();
for (int i = 0; i < model.getProblems().size(); i++){
textView[i] = new CheckedTextView(context);
textView[i].setText(model.getProblems().get(i));
textView[i].setPadding(10,10,10,10);
textView[i].setTextSize(15);
int finalI = i;
textView[i].setOnClickListener(v -> {
if (textView[finalI].isChecked()){
textView[finalI].setChecked(false);
textView[finalI].setCheckMarkDrawable(0);
}else {
textView[finalI].setChecked(true);
textView[finalI].setCheckMarkDrawable(R.drawable.checked);
}
});
holder.problems.addView(textView[i]);
}
}
#NonNull
#Override
public RecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return RecyclerHolder.buildFor(parent);
}
public final static class RecyclerHolder extends RecyclerView.ViewHolder {
private static final int LAYOUT = R.layout.item_types;
ExpansionLayout expansionLayout;
TextView textView;
ImageView imageView;
LinearLayout problems;
public static RecyclerHolder buildFor(ViewGroup viewGroup){
return new RecyclerHolder(LayoutInflater.from(viewGroup.getContext()).inflate(LAYOUT, viewGroup, false));
}
public RecyclerHolder(View itemView) {
super(itemView);
expansionLayout = itemView.findViewById(R.id.expansionLayout);
textView = itemView.findViewById(R.id.type_name);
imageView = itemView.findViewById(R.id.type_image);
problems = itemView.findViewById(R.id.problems_list);
}
public ExpansionLayout getExpansionLayout() {
return expansionLayout;
}
}
}
make an interface class and bind this interface into constructor of adapter class.
public interface CustomDialogListener {
void onItemClicked(CheckedTextView checkedTextView);
}
this is for adapter class
private CustomDialogListener mViewClickListener;
public ProductsAdapter(Context context,boolean status) {
this.context = context;
this.status = status;
this.mViewClickListener = (CustomDialogListener) context;
}
and assign interface class method on your required action like onClick on any view.
textView[i].setOnClickListener(v -> {
if (textView[finalI].isChecked()){
textView[finalI].setChecked(false);
textView[finalI].setCheckMarkDrawable(0);
}else {
textView[finalI].setChecked(true);
textView[finalI].setCheckMarkDrawable(R.drawable.checked);
}
listener.onItemClicked(textView);
});
add implementation of interface in parent activity class and override the interface method.
public class ParentActivity extends AppCompatActivity implements
YourAdapter.CustomDialogListener {
#Override
public void onItemClicked(CheckedTextView checkedTextView) {
//Log.d("Item clicked", checkedTextView.isChecked()+"");
}
}
thats the way to track event of recyclerView.
hope i made myself clear.

How to get dataset data from EditText in String format?

I want to get link data in OnClickListener so I have created array Model with an object and store all data which is dynamic.
now I want to extract data so I have use array object to get data. like
I want to link data with string format and I use String urls=object.link; and when I generate toast it shows me Null.
So please Tell me How can I get link data into String Format so I can pass it on Uri.parse(urls));
File Code:
package com.ejobbox.ejobbox;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter{
private ArrayList<Model> dataset;
private Context mContext;
public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) {
this.dataset=mlist;
this.mContext=context;
}
public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
TextView title,subtitle,link,date;
ImageView imageView;
public ImageTypeViewHolder(View itemView){
super(itemView);
this.title=(TextView)itemView.findViewById(R.id.title);
//this.link=(TextView)itemView.findViewById(R.id.link);
this.subtitle=(TextView) itemView.findViewById(R.id.subtitle);
this.imageView=(ImageView) itemView.findViewById(R.id.icon);
this.date=(TextView)itemView.findViewById(R.id.date);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.postdetails,parent,false);
return new ImageTypeViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final Model object=dataset.get(position);
((ImageTypeViewHolder) holder).title.setText(object.title);
((ImageTypeViewHolder) holder).subtitle.setText(object.subtitle);
//((ImageTypeViewHolder) holder).link.setText(object.link);
((ImageTypeViewHolder) holder).date.setText((CharSequence) object.date);
((ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String urls=object.link;
Toast.makeText(mContext,urls, Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(urls));
mContext.startActivity(browserIntent);
}
});
}
#Override
public int getItemCount() { return dataset.size();}
}
Use the getText() method of the link object of EditText.
Do this:
String urls = ((ImageTypeViewHolder) holder).link.getText().toString();
But before this, remove the lines you have commented out.
((ImageTypeViewHolder) holder).title.setText(object.title);
I don't know why u casting here use holder directly so instead of the above line use this
holder.title.setText(object.title)
this will work perfectly and if it didn't` work plz let me know

The activity is blank something wrong with the custom gird Adapter code?

I'm testing a simple grid adapter with a custom object, the app runs on the device without a problem but stays blank instead of inflating the activity with the specified grid items. This is my code.
Main Activity
package com.example.nobodyme.errorrepository;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<GridItems> gridItems = new ArrayList<>();
gridItems.add(new GridItems("Android", 2));
gridItems.add(new GridItems("Java", 3));
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new GridViewAdapter(this, gridItems));
}
}
GridViewAdapter
package com.example.nobodyme.errorrepository;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class GridViewAdapter extends ArrayAdapter<GridItems> {
private Context context;
int b;
public GridViewAdapter(Context context, List<GridItems> gridItems) {
super(context, 0, gridItems);
b=gridItems.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if(gridView == null)
{
gridView = LayoutInflater.from(getContext()).inflate(
R.layout.grid_view, parent, false);
}
GridItems currentgriditem = getItem(position);
TextView mMaintextView = (TextView) gridView.findViewById(R.id.grid_item_main);
mMaintextView.setText(currentgriditem.getTitle());
TextView mUnansweredtextView = (TextView) gridView.findViewById(R.id.grid_item_unanswered);
mUnansweredtextView.setText(currentgriditem.getUnansweredNo());
return gridView;
}
#Override
public int getCount() {
return b;
}
#Override
public long getItemId(int position) {
return 0;
}
}
GridItems
package com.example.nobodyme.errorrepository;
/**
* Created by nobodyme on 15/1/17.
*/
public class GridItems {
/** Grid title*/
private String mTitle;
/** NO of unanswered items in the gird **/
private Integer mUnansweredNo;
public GridItems(String Title, Integer UnansweredNo) {
mTitle = Title;
mUnansweredNo = UnansweredNo;
}
public String getTitle() {
return mTitle;
}
public Integer getUnansweredNo() {
return mUnansweredNo;
}
}
I have edited the code as per the comments and the app still crashes.
You're overriding getCount and returning zero in the adapter. Don't override this, or return the correct number of items.
Please check below code :
#Override
public int getCount() {
return gridItems.size();
}
Instead of 0 returning, You should return your list size.
You need define gridItems List on your arrayAdapter and set it on the constructor
and override getCount to return the gridItems (adapterGridItems) size
private List<GridItems> adapterGridItems;
public GridViewAdapter(Context context, List<GridItems> gridItems) {
super(context, 0, gridItems);
//set adapterGridItems
this.adapterGridItems=gridItems;
}
#Override
public int getCount() {
return adapterGridItems.size();
}
please, check
mMaintextView.setText(currentgriditem.getUnansweredNo());
you are passing a integer so mMainTextView.setText will find a resource with currentgriditem.getUnansweredNo() id, i think you are trying to set currentgriditem.getUnansweredNo() as string, so this will work
mMaintextView.setText(currentgriditem.getUnansweredNo()+"");
are you sure that is mMaintextView.setText(currentgriditem.getUnansweredNo()+"");
and not
mUnansweredtextView.setText(currentgriditem.getUnansweredNo()+"");
?
In order to tell the adapter how many GridItems to show we need to override the getCount method as shown above by #samsad.
The reason the compiler complains that it can't recognize symbol griditems is because you haven't created a field to store a reference to the GridItems in your adapter.
Try creating a field for the ArrayList of GridItems in your adapter to fix the issue.

custom list view with check Box does not work

I have a problem my custom List view.I have data in hash map and add into array list which is shown in below code.what is my problem is in my hash map contain 20 values and i try set into the list view but first data only display other or not.Thanks advance
This is my CustomAdapter
package com.example.node10.databasetesting;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.example.node10.databasetesting.DataBase.*;
public class ListViewEmployee extends AppCompatActivity implements View.OnClickListener {
private SimpleCursorAdapter dataAdapter;
private Button btn_view;
private Button btn_submit;
ArrayList<HashMap<Integer,String>> emp;
HashMap<Integer,String> map;
Set<Integer> keyValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_employee);
DataBase dataBase=new DataBase(this);
map=new HashMap<>();
map=dataBase.getValueEmpTable();
emp=new ArrayList<HashMap<Integer, String>>();
keyValue=map.keySet();
emp.add(map);
btn_view= (Button) findViewById(R.id.id_btn_emp_view);
btn_view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.id_btn_emp_view){
displayList();
// selectItem();
}
}
private void displayList() {
//create the Arrayt adapter
ListView listview= (ListView) findViewById(R.id.id_listview);
final MyAdapter adapter=new MyAdapter(this,R.layout.custom_listview,emp);
listview.setAdapter(adapter);
}
public class MyAdapter extends ArrayAdapter<HashMap<Integer,String>>{
boolean[] checkBoxState;
ViewHolder viewholder;
public MyAdapter(Context context, int resource,ArrayList<HashMap<Integer,String>> map) {
super(context, resource, map);
// create the boolean array for check box selection
checkBoxState= new boolean[map.size()];
}
// create the class for caching the view
class ViewHolder{
TextView txtView;
CheckBox checkBox;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater objlayout= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=objlayout.inflate(R.layout.custom_listview,null);
viewholder=new ViewHolder();
viewholder.txtView= (TextView) convertView.findViewById(R.id.id_checkbox_textview);
viewholder.checkBox= (CheckBox) convertView.findViewById(R.id.id_checkbox);
convertView.setTag(viewholder);
}else{
viewholder = (ViewHolder) convertView.getTag();}
// viewholder.txtView.setText(emp.get(position).toString());
viewholder.txtView.setText(emp.get(position).get(position+1).toString());
viewholder.checkBox.setChecked(checkBoxState[position]);
viewholder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checkBoxState[position] = true;
} else {
checkBoxState[position] = false;
}
}
});
return convertView;
}
}
}
Now you put to your adapter ArrayList of HashMap. I don't know why are you doing that, but now your ArrayList emp contains ony one element - that was added in line
emp.add(map);
So, ArrayAdapter for array with size = 1 will show 1 elemet.
If you want to show in list all elements from your map. I suggest to use
ArrayList emp and comvert map to list by using map.values()
ArrayList<String> emp = (ArrayList<String>)map.values();
Then change your adapter to
public class MyAdapter extends ArrayAdapter<String>

Categories

Resources