Espresso match selected spinner text - testing

Following the answer here, I try to check whether a certain spinner text is selected. The spinner appears in a dialog, so I tried:
onView(withId(R.id.package_spinner)).inRoot(isDialog()).check(matches(withSpinnerText(containsString("sachet"))));
However this does not work and I get the following error message:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:
'with text: a string containing "sachet"' doesn't match the selected
view.
Expected: with text: a string containing "sachet"
Got: "AppCompatSpinner{id=2131624039, res-name=package_spinner, visibility=VISIBLE, width=620, height=75, has-focus=false,
has-focusable=true, has-window-focus=true, is-clickable=true,
is-enabled=true, is-focused=false, is-focusable=true,
is-layout-requested=false, is-selected=false,
root-is-layout-requested=false, has-input-connection=false, x=0.0,
y=75.0, child-count=1}"
Does "is-selected=false" mean, that the spinner it found is not selected? This is running on a real device (not emulator) with API 18. During the Espresso run and also when testing manually, the spinner is correctly set to "sachet". Why does Espresso have a problem with it?
Not sure whether this is relevant, but the spinner is for objects of type:
public class PackageType {
private int id;
private String name;
private final Context ctx;
public PackageType(Context context) { this.ctx=context; }
public PackageType(String name, Context context) {
super();
this.ctx = context;
setName(name);
}
// setters
public void setId(int i) { this.id = i; }
public void setName(String u) {
this.name = u;
}
// getters
public int getId() { return id; }
public String getName() { return name; }
}
and the spinner adapter looks like:
class SpinnerPackageTypeAdapter extends ArrayAdapter<PackageType> {
private final List<PackageType> packageTypes;
private final Context mContext;
public SpinnerPackageTypeAdapter(Context context, int resource, List<PackageType> packageTypes) {
super(context, resource, packageTypes);
this.mContext = context;
this.packageTypes = packageTypes;
}
public PackageType getItem(int position) { return packageTypes.get(position); }
public long getItemId(int position) { return position; }
// this is for the passive state of the spinner
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// use dynamically created TextView, but could reference custom layout
TextView label = new TextView(mContext);
label.setTextColor(Color.BLACK);
label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
label.setGravity(Gravity.CENTER);
label.setText(getItem(position).getName());
return label;
}
// this is for the chooser dropped down spinner
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView label = (TextView) View.inflate(mContext,R.layout.row_spinner,null);
label.setText(getItem(position).getName());
return label;
}
}

I found the problem. In class PackageType I had to override the toString() method as (of course other versions are possible as long as "name" is in there):
#Override
public String toString() {
return "PackageType [id=" + id + ", name=" + name + "]";
}

Related

Get data from list of EditTexts in a recycler view to a list?

I am having an EditText in a TTextInputLayout as a Row_item for my recycler view, On the click of a button I am incrementing the count value in that recycler view and adding the edit text views in the app. After having as many views as I want, I am putting some data in those EditTexts and After all this I press save button and all the data from EditTexts should save in a list. I tried the textwatcher Property in xamarin.android but not able to perform that.
In my activity I also tried
var view = addChildRecyclerView.FindViewHolderForAdapterPosition(i);
var holder = view as fragmentRecyclerViewAdapterViewHolder;
if (holder != null)
{
holder.childname.FindViewById<TextInputLayout>(Resource.Id.enterChildNameTextView);
childlisttosend.Add(holder.childname.EditText.Text);
But sometimes I get holder as a null and some valus are missed to be put in the list.
I tried textchange property but it puts every single alphabet in the list, like
for index 1 it is "m"
for index 2 it is "ma"
for index 3 it is "man"
for index 4 it is "mani"
for index 5 it is "manis"
for index 6 it is "manish"
It is of no use. Please sugggest a solution.
adapter is
namespace assessment1_part2_v2.Fragment
{
public class addFamilyFragment : DialogFragment
{
public TextInputLayout fathername, mothername, address;
public MaterialButton addchild, savebutton;
public ArrayAdapter adapter;
public ListView listview;
public familyData familydata = new familyData();
public List<childData> childrenList = new List<childData>();
public editTextAdapter EditTextAdapter;
public event EventHandler<DataSenderClass> famaDataSender;
public RecyclerView enterChildRecyclerView;
public List<string> addChildDummyData = new List<string>();
public TextInputLayout enterChildEditTExt;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.addfam_layout, container, false);
connectingViews(view);
buttonClicks();
return view;
}
public class DataSenderClass : EventArgs
{
public familyData data { get; set; }
}
private void buttonClicks()
{
addchild.Click += Addchild_Click;
savebutton.Click += Savebutton_Click;
}
private void Savebutton_Click(object sender, EventArgs e)
{
//father, mother and address
familydata.fathername = fathername.EditText.Text;
familydata.mothername = mothername.EditText.Text;
familydata.location = address.EditText.Text;
familydata.children = new List<childData>();
List<childData> list = new List<childData>();
List<string> dummyCheck = new List<string>();
for (int i = 0; i <= addChildDummyData.Count; i++)
{
var viewholder = enterChildRecyclerView.FindViewHolderForAdapterPosition(i);
var holder = viewholder as editTextAdapterViewHolder;
// holder.addChildEditTExt;
if(holder != null)
{
holder.addChildEditTExt.FindViewById<TextInputLayout>(Resource.Id.enterChildEditTExt);
dummyCheck.Add(holder.addChildEditTExt.EditText.Text);
}
}
var x = dummyCheck;
addChildDummyData.Clear();
famaDataSender?.Invoke(this, new DataSenderClass { data = familydata });
this.Dismiss();
}
private void Addchild_Click(object sender, EventArgs e)
{
if (addChildDummyData.Count == 0)
{
addChildDummyData.Add("");
EditTextAdapter = new editTextAdapter(addChildDummyData);
enterChildRecyclerView.SetLayoutManager(new LinearLayoutManager(Activity));
enterChildRecyclerView.SetAdapter(EditTextAdapter);
int x = enterChildRecyclerView.ChildCount;
}
else
{
addChildDummyData.Add("");
EditTextAdapter.NotifyItemInserted(addChildDummyData.Count - 1);
int x = enterChildRecyclerView.ChildCount;
}
}
private void connectingViews(View view)
{
fathername = view.FindViewById<TextInputLayout>(Resource.Id.enterFatherNameEditText);
mothername = view.FindViewById<TextInputLayout>(Resource.Id.enterMotherNameEditText);
address = view.FindViewById<TextInputLayout>(Resource.Id.enterAddressEditText);
addchild = view.FindViewById<MaterialButton>(Resource.Id.addChildButton);
savebutton = view.FindViewById<MaterialButton>(Resource.Id.saveButton);
enterChildRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.enterChildRecyclerView);
//listview = (ListView)view.FindViewById(Resource.Id.enterChildRecyclerView);
}
}
} `
We couldn't see the code of editTextAdapter.
But you can refer to my code ,my RecyclerView has an Edittext in each item and we can get data from the input list.
I put TextWatcher in my RecyclerView.ViewHolder(PhotoViewHolder.cs).
PhotoViewHolder.cs
public class PhotoViewHolder: RecyclerView.ViewHolder
{
public ImageView Image { get; private set; }
public TextView Caption { get; private set; }
public CheckBox MyCheckBox { get; set; }
public bool IsChecked { get; set; }
public EditText MyEditText { get; set; }
public MyTextWatcher myTextWatcher;
public Button imAddBefore { get; private set; }
public Button imAddAfter { get; private set; }
public PhotoViewHolder(View itemView, Action<int> listener, MyTextWatcher myWatcher ) : base(itemView)
{
//Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
Caption = itemView.FindViewById<TextView>(Resource.Id.textView);
MyCheckBox = itemView.FindViewById<CheckBox>(Resource.Id.myCheckBox);
DeleteButton = itemView.FindViewById<Button>(Resource.Id.deleteBtn);
MyEditText = itemView.FindViewById<EditText>(Resource.Id.mEditText);
this.myTextWatcher = myWatcher;
// add TextWatcher here
MyEditText.AddTextChangedListener(myTextWatcher);
// other code
}
public class MyTextWatcher : Java.Lang.Object, ITextWatcher
{
int position;
public void AfterTextChanged(IEditable s)
{
}
public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
{
}
public void OnTextChanged(ICharSequence s, int start, int before, int count)
{ //mInput is a field in my item model
PhotoAlbumAdapter.mPhotoAlbum[position].mInput = s.ToString();
}
public void updatePosition(int position)
{
this.position = position;
}
}
}
My RecyclerView.Adapter is PhotoAlbumAdapter.cs
PhotoAlbumAdapter.cs
public class PhotoAlbumAdapter: RecyclerView.Adapter
{
public event EventHandler<int> ItemClick;
public static List<Photo> mPhotoAlbum = new List<Photo>();
public static RecyclerView.Adapter adapter;
public PhotoAlbumAdapter(List<Photo> branchesList)
{
adapter = this;
mPhotoAlbum = branchesList;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.PhotoCardView, parent, false);
PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick, new PhotoViewHolder.MyTextWatcher());
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
PhotoViewHolder vh = holder as PhotoViewHolder;
Photo item= mPhotoAlbum[position];
vh.Caption.Text = item.Caption;
// here we call function `updatePosition` in our TextWatcher class
vh.myTextWatcher.updatePosition(vh.AdapterPosition);
vh.MyEditText.Text = item.mInput;
// other code
}
public override int ItemCount
{
get { return mPhotoAlbum.Count; }
}
// Raise an event when the item-click takes place:
void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
}
In my activity, we can get the data from the recycleview like this:
List<Photo> items= new List<Photo>();
mAdapter = new PhotoAlbumAdapter(items);
mRecyclerView.SetAdapter(mAdapter);
The result is:
for (int i=0;i< items.Count;i++) {
Photo temp = items[i];
System.Diagnostics.Debug.WriteLine( "The "+i +" result is: "+" Caption= " + temp.Caption + "<---> input = " + temp.mInput);
}

highlight items in recyclerview android

I am trying to select items in recylerview. When I click an item, a checkbox appears on that item and it is highlighted. But the problem is that when I scroll, the item which is highlighted goes to its original color but the checkbox image remains as it should. Why the highlighted color is gone but the image remains after scrolling, I want the items to keep their state after scrolling.
public static List<Model> item = new ArrayList<Model>()
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int
position) {
holder.bind(item.get(position));
//
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.check);
public void bind(Model model) {
if (model.isChecked()){
imageView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(Color.GRAY);
model.setChecked(true);
} else {
imageView.setVisibility(View.GONE);
itemView.setBackgroundColor(Color.TRANSPARENT);
model.setChecked(false);
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
notifyItemChanged(getAdapterPosition());
}
});
public class Model {
private boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
Don't manually change the design in the onclick listener, just update your model (as you are already doing: model.setchecked(...) depending on state)
Then call Adapter.notifyItemChanged(getAdapterPosition())
This will force the adapter to reload the item, calling onBindViewHolder, where the design should be updated properly
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
adapter.notifyItemChanged(getAdapterPosition())
}
In the above example, replace "adapter" with a reference to your recylerview adapter

Unable to Pass Data from RecyclerView Adapter to Viewpager Adapter

I am creating simple image gallery app in which images are shown on MainActivity inside RecyclerView and on click of Any Image it will take us to SlideActivity in which I want to show images on ViewPager and then we swipe all images should be shown on ViewPager. For retrieving images i am using Glide. Images Load on RecyclerView Perfectly Fine but when i click on any Image i get this Exception "java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1".
I know there are many same questions like this I saw all codes but did not understood so finally decided to ask question I am pasting code whatever i tried till now. Any Suggestion or advice will be helpful bcz I work alone I have no Mentor and I am Learning. Thank you!
Error :
java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at com.starmoonsolutions.imagegallery.RecycleViewAdapter$ViewHolder$1.onClick(RecycleViewAdapter.java:63)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
ImageUrl.java
public class ImageUrl {
String imageUrl;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
RecyclerView recyclerView;
GridLayoutManager gridLayoutManager;
ArrayList<ImageUrl> imageUrlList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
recyclerView = findViewById(R.id.recyclerView);
gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(gridLayoutManager);
imageUrlList = prepareData();
RecycleViewAdapter galleryAdapter = new RecycleViewAdapter(getApplicationContext(), imageUrlList);
recyclerView.setAdapter(galleryAdapter);
}
private ArrayList prepareData(){
// here you should give your image URLs and that can be a link from the Internet
String imageUrls[] = {
"https://images.pexels.com/photos/358457/pexels-photo-358457.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
"https://images.pexels.com/photos/934964/pexels-photo-934964.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1202887/pexels-photo-1202887.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
"https://images.pexels.com/photos/337909/pexels-photo-337909.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/894443/pexels-photo-894443.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/951325/pexels-photo-951325.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1534411/pexels-photo-1534411.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/266436/pexels-photo-266436.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1894350/pexels-photo-1894350.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/87009/earth-soil-creep-moon-lunar-surface-87009.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1820563/pexels-photo-1820563.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1274260/pexels-photo-1274260.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
};
imageUrlList = new ArrayList<>();
for (int i = 0; i < imageUrls.length; i++) {
ImageUrl imageUrl = new ImageUrl();
imageUrl.setImageUrl(imageUrls[i]);
imageUrlList.add(imageUrl);
}
Log.d("MainActivity", "List count: " + imageUrlList.size());
return imageUrlList;
}
}
RecyclerViewAdapter:
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private ArrayList<ImageUrl> imageUrls;
private Context context;
public RecycleViewAdapter(Context context, ArrayList<ImageUrl> imageUrls) {
this.imageUrls = imageUrls;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_layout, viewGroup, false);
return new ViewHolder(view);
}
/**
* gets the image url from adapter and passes to Glide API to load the image
*
* #param viewHolder
* #param i
*/
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Glide.with(context).load(imageUrls.get(i).getImageUrl()).into(viewHolder.img);
}
#Override
public int getItemCount() {
return imageUrls.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RecyclerView recyclerView;
ImageView img;
public ViewHolder(#NonNull final View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imageView);
recyclerView = itemView.findViewById(R.id.recyclerView);
final int pos = getAdapterPosition();
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
intent.putExtra("list", imageUrls.get(pos).imageUrl);
context.startActivity(intent);
}
});
}
}
}
SlideActivity(ViewPager):
public class SlideActivity extends AppCompatActivity {
private ViewPager viewPager;
private Context context = SlideActivity.this;
private ViewPagerAdapter adapter;
private ArrayList<String> list;
String image;
private int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_activity);
viewPager = findViewById(R.id.viewPager);
getIntentInfo();
adapter = new ViewPagerAdapter(list,context);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem((position));
}
private void getIntentInfo() {
image = getIntent().getStringExtra("image");
list = getIntent().getStringArrayListExtra("list");
position = getIntent().getIntExtra("position",0);
}
}
ViewPagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
private ArrayList<String> imageUrls;
private android.content.Context context;
private LayoutInflater inflater;
private ImageView wallpaper;
public ViewPagerAdapter() {
}
public ViewPagerAdapter(ArrayList<String> imageUrls, android.content.Context context) {
this.imageUrls = imageUrls;
this.context = context;
}
#Override
public int getCount() {
return imageUrls.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view.equals(o);
}
#NonNull
#Override
public Object instantiateItem(#NonNull final ViewGroup container, final int position) {
inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.pager_item, container, false);
wallpaper = view.findViewById(R.id.walpaperImage);
Glide.with(context).asBitmap().load(imageUrls.get(position)).into(wallpaper);
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
The problem is actually in getting the adapter position.
public ViewHolder(#NonNull final View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imageView);
recyclerView = itemView.findViewById(R.id.recyclerView);
// remove the below line
//final int pos = getAdapterPosition();
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
// you need to freshly get the adapter position here.
int pos = getAdapterPosition();
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
intent.putExtra("list", imageUrls.get(pos).imageUrl);
context.startActivity(intent);
}
});
}
Since ViewHolder gets recycled, adapter position can change therefore you need to get the latest adapter position inside your click listener.
And regarding the Bundle data, pass the list of URLs of 'String' type instead of the custom object,
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
// imageUrls is of type ArrayList<String>
intent.putStringArrayListExtra("list", imageUrls);
context.startActivity(intent); }
or else use the Parcelable library to serialize the custom object.

Spinner in RecyclerView to update item values

How can I update the values in my Dataset from a Spinner selection in a RecyclerView? Should I be using LiveData and Room with notifyItemChange(), data binding, or something simpler? I have a Horseclass and a HorseData class. The number of items is static and the options for the variables are static. I do not need to add values or items, I just need to be able to select the variable WinPercentHorse for each item and have that value saved back to the dataset.
public class HorseAdapter extends RecyclerView.Adapter<HorseViewHolder> {
private AdapterView.OnItemSelectedListener onItemSelectedListener;
//we are storing all the horses in a list
private List<Horse> horseList;
//this context we will use to inflate the layout
private Context mCtx;
//getting the context and product list with constructor
public HorseAdapter(Context mCtx, List<Horse> horseList) {
this.mCtx = mCtx;
this.horseList = horseList;
}
#Override
public HorseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.item_layout, null);
return new HorseViewHolder(view);
}
#Override
public void onBindViewHolder(final HorseViewHolder holder, final int position) {
//getting the product of the specified position
Horse horse = horseList.get(position);
//binding the data with the viewholder views
holder.ViewWinPercent.setText(String.valueOf(horse.getWinPercentHorse()));
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(horse.getImgHorse()));
holder.ViewValueLine.setText(String.valueOf(horse.getValueOdds()));
holder.itemView.setTag(horse.getId());
holder.spinnerView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Update winPercent of item On selecting a spinner item
Toast.makeText(mCtx, "Spinner Selection =" + l + "item selected =" + position, Toast.LENGTH_SHORT).show();
//How to update the WinPercentHorse variable for this item in the horseList
//Then refresh the item notifyItemChange(position, object payload)?
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
#Override
public int getItemCount() {
return horseList.size();
}
public void setHorseList(List<Horse> horseList) {
this.horseList = horseList;
notifyDataSetChanged();
}
}
class HorseViewHolder extends RecyclerView.ViewHolder {
TextView ViewWinPercent, ViewValueLine;
ImageView imageView;
Spinner spinnerView;
public HorseViewHolder(View itemView) {
super(itemView);
ViewWinPercent = itemView.findViewById(R.id.winPercentHorse);
ViewValueLine = itemView.findViewById(R.id.valueLine);
imageView = itemView.findViewById(R.id.imgHorse);
spinnerView=itemView.findViewById(R.id.spinner);
}
}

RecycleView , some items' subView doesn't show correctly

in fragment
mAdapter = new MessageAdapter(this);
mRV.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
mRV.setItemAnimator(new DefaultItemAnimator());
DividerItemDecoration itemDecoration = new DividerItemDecoration.Builder()
.setOffsetLeft(ScreenUtil.dip2px(getActivity(), 60 + 10) + this.getResources().getDimensionPixelOffset(R.dimen.horizontal_margin))
.build(getActivity());
mRV.addItemDecoration(itemDecoration);
mRV.setItemViewCacheSize(15);
mRV.setAdapter(mAdapter);
in adapter
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MessageItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.w_message_item,parent,false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((RVItemInterface)holder).setAdapter(this);
((RVItemInterface)holder).update(dataList.get(position),position);
}
in holder
public class MessageItemHolder extends RecyclerView.ViewHolder implements RVItemInterface{
private RoundAvatar mAvatar;
private TextView mTitle;
private TextView mContent;
private TextView mTime;
private BaseRVAdapter mAdapter;
private MsgDecorVo mMsgDecorVo;
...
#Override
public void update(Object obj, final int position) {
reset();
mMsgDecorVo = (MsgDecorVo) obj;
if(mMsgDecorVo.type == MsgDecorVo.TYPE_CATEGORY){
updateCategory();
MsgVo msgVo = mMsgDecorVo.msgVo;
if(msgVo.getMsg() == null || msgVo.getMsg().equals("")){
mContent.setVisibility(View.GONE);
}else {
mContent.setVisibility(View.VISIBLE);
mContent.setText(msgVo.getMsg());
}
if(msgVo.getTime() == 0){
mTime.setVisibility(View.GONE);
}else {
mTime.setVisibility(View.VISIBLE);
mTime.setText(TimeUtil.transformLong2DateString(msgVo.getTime()));
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dealCategoryClick();
}
});
}else if(mMsgDecorVo.type == MsgDecorVo.TYPE_CONVERSATION){
...
mContent.setText(ImUtil.getMsgContent(message));
mTime.setText(TimeUtil.transformLong2DateString(message.getMsgTime()));
if(chatUserVo != null){
..
}else {
..
}
}
}
//
private void updateCategory(){
...
}
//
private void dealCategoryClick(){
...
}
#Override
public void setAdapter(RecyclerView.Adapter adapter) {
mAdapter = (BaseRVAdapter) adapter;
}
#Override
public void reset() {
mAvatar.setOnClickListener(null);
mAvatar.setAvatar(R.drawable.avatar_default_circle);
mAvatar.hideTagBottom();
mAvatar.hideTagTop();
}
}
these code in holder
mContent.setText(ImUtil.getMsgContent(message));
mTime.setText(TimeUtil.transformLong2DateString(message.getMsgTime()));
has run
but when I scrolled,some of items doesn't show correctly ,the time and content was gone!
if I notify the recyclerview ,it goes right, and if i scroll again,it will still be wrong
just like the image,you can see some items' (time & content) was gone!
http://g.picphotos.baidu.com/album/s%3D900%3Bq%3D90/sign=297cc7510946f21fcd345253c61f1a5d/a686c9177f3e6709378bcc5538c79f3df9dc5595.jpg "tooltip"
Well, you don't have the full code but I assume it is happening because you are not resetting your view states properly.
For Instance, in your updateCode, if type = TYPE_CATEGORY, you set the mTime's visibility depending on whether getTime is 0 or not. But as you scroll, that row might be re-used for
TYPE_CONVERSATION in which case, mTime's visibility will NOT be updated.