Ratingbar in listview - android

Rating bar is resets when we scroll down in android, I need to send the response of rating feedback to server but it will resets or not saved when i scroll down in listview in android.
public class FacultyListAdapter extends BaseAdapter {
private final Context context;
public FacultyListAdapter(Context context) {
this.context = context;
if (!facultylist.isEmpty())
courseEmptyLayout.setVisibility(LinearLayout.GONE);
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder TabviewHolder;
if (convertView == null) {
TabviewHolder = new ViewHolder();
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item_feedback,
parent, false);
TabviewHolder.FacultyName = (TextView) convertView.findViewById(R.id.FacultyName);//facultyname
TabviewHolder.rating = (RatingBar) convertView.findViewById(R.id.rating);//rating starts
TabviewHolder.Submit = (Button) convertView.findViewById(R.id.btnSubmit);
// Save the holder with the view
convertView.setTag(TabviewHolder);
} else {
TabviewHolder = (ViewHolder) convertView.getTag();
}
final Faculty mFac = facultylist.get(position);//*****************************NOTICE
TabviewHolder.FacultyName.setText(mFac.getEmployeename());
TabviewHolder.rating.setTag(position);
TabviewHolder.rating.setRating(mFac.getRatingStar());
// TabviewHolder.ModuleName.setText(mFac.getSubject());
TabviewHolder.rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
feedbackresult =String.valueOf(rating);
TabviewHolder.rating.setRating(Float.parseFloat(feedbackresult));
Log.d("feedback","feedback is: "+ feedbackresult);
}
});
return convertView;
}
/*private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position) {
return new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
FacultyName item = getItem(position);
item.setRatingStar(v);
Log.i("Adapter", "star: " + v);
}
};
}*/
#Override
public int getCount() {
return facultylist.size();
}
#Override
public Object getItem(int position) {return facultylist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}

Listview and recyclerview auto refresh (recreate) view based on your list passed to it when you scroll.
You have to store your rating to your List<> (i think it is facultylist) in which you contains all details.
When user click on rating you need to store rating to particular index. Then your listview show correct rating based on value in list.

refer this link Rating Bar in ListView Android
public class FeedbackAdapter extends BaseAdapter {
static Context context;
List<FeedbackRowDetails> row;
ViewHolder holder;
public FeedbackAdapter(Context context,List<FeedbackRowDetails> row)
{
this.context = context;
this.row =row;
}
private class ViewHolder {
TextView tv_slno, tv_productname, tv_time;
RatingBar rb_productrating;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.custom_feedback_row_format,parent, false);
holder = new ViewHolder();
holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname);
holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.rb_productrating.setOnRatingBarChangeListener(onRatingChangedListener(holder, position));
holder.rb_productrating.setTag(position);
holder.tv_slno.setText(String.valueOf(position + 1));
holder.tv_time.setText(getItem(position).gettime());
holder.tv_productname.setText(getItem(position).getproductsName());
float rating=Float.valueOf(getItem(position).getrating());
holder.rb_productrating.setRating(rating);
return convertView;
}
private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position)
{
return new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean b)
{
FeedbackRowDetails item = row.get(position);
int roundoff_rating = (int)Math.round(rating);
ratingBar.setRating(roundoff_rating);
item.setrating(String.valueOf(roundoff_rating));
}
};
}
#Override
public int getCount() {
return row.size();
}
#Override
public FeedbackRowDetails getItem(int position)
{
return row.get(position);
}
#Override
public long getItemId(int position) {
return row.indexOf(getItem(position));
}
}

Related

Ratingbar's rating within listview resets

Senario: To get rating feedback for multiple multiple products.
Problem: Ratingbar value resets on scrolling listview by calling setOnRatingBarChangeListener
Expected output:
Customized listview adpater code:
public class FeedbackAdapter extends BaseAdapter {
static Context context;
List<FeedbackRowDetails> row;
ViewHolder holder;
public FeedbackAdapter(Context context,List<FeedbackRowDetails> row)
{
this.context = context;
this.row =row;
}
private class ViewHolder {
TextView tv_slno, tv_productname, tv_time;
RatingBar rb_productrating;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.custom_feedback_row_format,null);
holder = new ViewHolder();
holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname);
holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
FeedbackRowDetails row_pos = row.get(position);
holder.tv_slno.setText(String.valueOf(position + 1));
holder.tv_time.setText(row_pos.gettime());
holder.tv_productname.setText(row_pos.getproductsName());
holder.rb_productrating.setRating(Float.valueOf(row_pos.getrating()));
holder.rb_productrating.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser)
{
FeedbackRowDetails row_pos = row.get(position);
int roundoff_rating = (int)Math.round(rating);
ratingBar.setRating(roundoff_rating);
row_pos.setrating(String.valueOf(roundoff_rating));
}
});
return convertView;
}
#Override
public int getCount() {
return row.size();
}
#Override
public Object getItem(int position) {
return row.get(position);
}
#Override
public long getItemId(int position) {
return row.indexOf(getItem(position));
}
}
Just wrap your codes present inside your onRatingChanged method with a simple if as shown below
if(fromUser) {
FeedbackRowDetails row_pos = row.get(position);
int roundoff_rating = (int) Math.round(rating);
ratingBar.setRating(roundoff_rating);
row_pos.setRating(String.valueOf(roundoff_rating));
}
Have tested it with emulator and it was working as expected. Hope this solves your problem
This problem solved with reference to link
public class FeedbackAdapter extends BaseAdapter {
static Context context;
List<FeedbackRowDetails> row;
ViewHolder holder;
public FeedbackAdapter(Context context,List<FeedbackRowDetails> row)
{
this.context = context;
this.row =row;
}
private class ViewHolder {
TextView tv_slno, tv_productname, tv_time;
RatingBar rb_productrating;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.custom_feedback_row_format,parent, false);
holder = new ViewHolder();
holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname);
holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.rb_productrating.setOnRatingBarChangeListener(onRatingChangedListener(holder, position));
holder.rb_productrating.setTag(position);
holder.tv_slno.setText(String.valueOf(position + 1));
holder.tv_time.setText(getItem(position).gettime());
holder.tv_productname.setText(getItem(position).getproductsName());
float rating=Float.valueOf(getItem(position).getrating());
holder.rb_productrating.setRating(rating);
return convertView;
}
private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position)
{
return new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean b)
{
FeedbackRowDetails item = row.get(position);
int roundoff_rating = (int)Math.round(rating);
ratingBar.setRating(roundoff_rating);
item.setrating(String.valueOf(roundoff_rating));
}
};
}
#Override
public int getCount() {
return row.size();
}
#Override
public FeedbackRowDetails getItem(int position)
{
return row.get(position);
}
#Override
public long getItemId(int position) {
return row.indexOf(getItem(position));
}
}

Base Adapter onclick not working

I am trying to use the material dialogs library Material Dialog and i can't get the callback to work for the custom adapter.
The items are displayed perfectly but on click of the items i don't get any callback.
Base Adapter
public class EcardBaseAdapter extends BaseAdapter implements View.OnClickListener {
private LayoutInflater mInflater;
private List<? super Greeting> items;
private Context context;
private final int width;
private final int height;
public EcardBaseAdapter(List<Greeting> items, Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.items = items;
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
this.width = metrics.widthPixels;
this.height = metrics.heightPixels;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = mInflater.inflate(R.layout.custom_item, parent, false);
holder = new ViewHolder();
holder.avatar = (ImageView) view.findViewById(R.id.image_path);
holder.name = (TextView) view.findViewById(R.id.custom_event_name);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
if (items.get(position) instanceof ECard) {
ECard eCard = (ECard) items.get(position);
Log.i("test", eCard.getImage() + " " + eCard.getSubject());
if (eCard.getImage() != null) {
Glide.with(context)
.load(eCard.getImage())
.fitCenter()
.override(width, height / 2)
.into(holder.avatar);
//holder.avatar.setImageURI(Uri.parse(eCard.getImage()));
}
holder.name.setTextColor(ContextCompat.getColor(context, R.color.darkGrey));
holder.name.setText(eCard.getSubject());
}
return view;
}
#Override
public void onClick(View view) {
Log.i("click", "itemclick");
}
private class ViewHolder {
public ImageView avatar;
public TextView name;
}
}
Code in the activity which uses the adapter
new MaterialDialog.Builder(birthday_details.this)
.title("Ecard")
.adapter(adapter,
new MaterialDialog.ListCallback() {
#Override
public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
Toast.makeText(birthday_details.this, "Clicked item " + which, Toast.LENGTH_SHORT).show();
}
})
.show();
I did not understandd all your code, but I seems you never call
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("click", "itemclick");
}
};
or in your case
view.setOnClickListener(this);

Navigation Drawer list view with 2 type

I have a Navigation Drawer, With a ListView and with a header. When I scorll the drawer up down the items are mixed, the type of the items. Why?
My DrawerAdapter.java:
public class DrawerAdapter extends BaseAdapter {
private Context context;
private List<DrawerItem> navDrawerItems;
DrawerItemClickListener drawerItemClickListener;
public DrawerAdapter(Context context, ArrayList<DrawerItem> navDrawerItems,DrawerItemClickListener drawerItemClickListener) {
this.context = context;
this.navDrawerItems = navDrawerItems;
this.drawerItemClickListener = drawerItemClickListener;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean isEnabled(int position) {
return false;
}
static class ViewHolder {
ImageView imvIcon;
TextView textView;
LinearLayout layout;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final DrawerItem mItem = navDrawerItems.get(position);
if(mItem.getType().equals(DrawerItem.TYPE_CLICKABLE))
{
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_list_item, parent, false);
holder.imvIcon = (ImageView) convertView.findViewById(R.id.item_icon);
holder.textView = (TextView) convertView.findViewById(R.id.item_text);
holder.layout = (LinearLayout) convertView.findViewById(R.id.drawer_one_layout);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
if(holder.imvIcon != null)
Picasso.with(parent.getContext()).load(mItem.getIcon()).into(holder.imvIcon);
if(holder.textView != null)
holder.textView.setText(mItem.getTitle());
if(holder.layout != null)
holder.layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerItemClickListener.DrawerItemClicked(mItem);
}
});
}
else
{
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_list_item_two, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.item_text);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mItem.getTitle());
}
return convertView;
}}
And the Drawer Item :
public class DrawerItem {
public final static String TYPE_CLICKABLE = "clikc";
public final static String TYPE_HEADER = "header";
private String title, type;
private int icon;
public DrawerItem(String title, int icon, String type) {
this.title = title;
this.icon = icon;
this.type = type;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
public String getType() {return this.type;}}
Please help me. Thanks.
You are not clearly stating which item type your current view is, so the adapter doesn't know the viewtype and doesn't display correctly the items (e.g. it mixes them).
In your getItemViewType() you should be clear about the viewtype of the current item, and do something like this:
#Override
public int getItemViewType(int position) {
if(navDrawerItems.get(position).getType().equals(DrawerItem.TYPE_CLICKABLE))
return 0;
return 1;
}

Checkbox in Listview showing unexpected behaviour

Hi i am using checkbox in listview, but the checkbox get auto unchecked or checked when i scroll my list view.
public class MyContactsAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Friend> mAllFriends;
public MyContactsAdapter(Context context,List<Friend> allFriends) {
inflater = LayoutInflater.from(context);
this.mAllFriends =allFriends;
}
#Override
public int getCount() {
if(mAllFriends==null)
{
return 0;
}else{
return mAllFriends.size();
}
}
#Override
public Object getItem(int position) {
return mAllFriends.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contact_list_item, null);
mHolder = new ViewHolder();
mHolder.mProFileImage = (ImageView) convertView
.findViewById(R.id.profleImage);
mHolder.mPersonName = (TextView) convertView
.findViewById(R.id.person_Name);
mHolder.mEmailId = (TextView) convertView
.findViewById(R.id.person_emailId);
mHolder.mSelectPerson = (CheckBox)convertView.findViewById(R.id.personSelect);
mHolder.mSelectAllLabel = (TextView)convertView.findViewById(R.id.personSelectAllLabel);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
if(position==0)
{
mHolder.mProFileImage.setVisibility(View.GONE);
mHolder.mPersonName.setVisibility(View.GONE);
mHolder.mEmailId.setVisibility(View.GONE);
mHolder.mSelectAllLabel.setVisibility(View.VISIBLE);
}else{
mHolder.mProFileImage.setVisibility(View.VISIBLE);
mHolder.mPersonName.setVisibility(View.VISIBLE);
mHolder.mEmailId.setVisibility(View.VISIBLE);
mHolder.mSelectAllLabel.setVisibility(View.GONE);
}
mHolder.mProFileImage.setImageResource(R.drawable.ic_launcher);
mHolder.mPersonName.setText(""+mAllFriends.get(position).getName());
mHolder.mEmailId.setText(""+mAllFriends.get(position).getEmail());
mHolder.mSelectPerson.setTag(position);
mHolder.mSelectPerson.setChecked(mAllFriends.get(position).isSelected());
mHolder.mSelectPerson.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mAllFriends.get((Integer) buttonView.getTag()).setSelected(isChecked);
}
});
return convertView;
}
private static class ViewHolder {
ImageView mProFileImage;
TextView mPersonName;
TextView mEmailId;
CheckBox mSelectPerson;
TextView mSelectAllLabel;
}
}

Android - Pull to refresh on custom adapter

I'm developing an Android Application and I have a custom adapter for my ListView. I want to use a Pull to refresh library, such as this one. This works fine with an ArrayAdapter, but not with a custom adapter.
How can I get my pull to refresh working with a custom adapter?
Here my adapter code:
public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;
public AdvertListAdapter(AdvertListActivity activity) {
this.activity = activity;
}
public void setFirstVisibleItem(int currentFirstVisibleItem) {
this.currentFirstVisibleItem=currentFirstVisibleItem;
}
public void setLastVisibleItem(int currentLastVisibleItem) {
this.currentLastVisibleItem=currentLastVisibleItem;
}
#Override
public int getCount() {
return activity.getAdvertList().size();
}
#Override
public Advert getItem(int pos) {
return activity.getAdvertList().get(pos);
}
#Override
public long getItemId(int pos) {
return activity.getAdvertList().get(pos).getId();
}
private class ViewHolder {
public ImageView image;
public TextView title;
public TextView price;
public TextView size;
public TextView city;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listview_advertlist, null);
view.image = (ImageView) convertView.findViewById(R.id.advertImage);
view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
view.city = (TextView) convertView.findViewById(R.id.advertCityText);
convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}
Advert advert = activity.getAdvertList().get(position);
view.title.setText(advert.getTitle());
view.price.setText(String.valueOf(advert.getPrice()+"€"));
view.size.setText("Taille: "+advert.getSize());
view.city.setText(advert.getCity());
if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
view.image.setImageResource(R.drawable.defaultadvertimage);
ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
}
return convertView;
}
}
Here my activity code:
list = (PullToRefreshListView) findViewById(R.id.advertList);
advertList = getAdvertList();
((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
list.setAdapter(adapter);

Categories

Resources