每次我滚动Recyclerview中的顺序时,都会更改。我不知道为什么会这样。
//-----VIEWHOLDER-------------------------
public class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Crime mCrime;
String Tag = "Crime_list";
//-------function for displaying title name---------------
public void bind(Crime crime) {
mCrime = crime;
System.out.println(crime.getmTitle());
Log.d(Tag,"crime number is :"+ crime.getmTitle());
mTitle_TextView.setText(mCrime.getmTitle());
mDate_TextView.setText(mCrime.getmDate().toString());
}
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime,parent,false));
itemView.setOnClickListener(this);
mTitle_TextView = (TextView) itemView.findViewById(R.id.crime_title);
mDate_TextView = (TextView)itemView.findViewById(R.id.crime_date);
}
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),mCrime.getmTitle() +" clicked!",Toast.LENGTH_SHORT).show(); }
}
//----ADAPTER------------------------
public class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes) {
mCrimes = crimes;
}
@NonNull
@Override
public CrimeHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new CrimeHolder(layoutInflater,viewGroup);
}
@Override
public void onBindViewHolder(@NonNull CrimeHolder crimeHolder, int i) {
Crime crime = mCrimes.get(i);
crimeHolder.bind(crime);
}
@Override
public int getItemCount() {
return mCrimes.size();
}
我尝试在Logcat中获得输出(在函数bind中),那里也显示了正确的输出。 getTitle在Viewholder中的函数bind中。 Toast在单击标题时也显示正确的输出,当我滚动标题时,标题名称将不断更改。 顺序就像犯罪#0,犯罪#1,...,犯罪#13,它将以正确的顺序显示,但是一旦我滚动几次,这些标题将不断重复,犯罪#14将不断变化(更像是波动)不同的数字)。
谢谢