Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had populate my list view with text views and button control using custom adapter.But when i am scrolling list view the data inside the list view is shuffling.

Java
public class CustomAdapter extends BaseAdapter
{
	LayoutInflater mInlfater;
	ArrayList<hashmap><string,string>> list;
	String rno="",name="";
	public CustomAdapter(Context context,ArrayList<hashmap><string,string>> list) 
	{
		mInlfater = LayoutInflater.from(context);
		this.list =list;
	}
	@Override
	public int getCount() 
	{
		return list.size();

	}

	@Override
	public Object getItem(int position) 
	{	
		return position;
	}

	@Override
	public long getItemId(int position)
	{	
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		final ViewHolder holder;
		if(convertView ==null)
		{
			convertView = mInlfater.inflate(R.layout.attendancelistview,null);
			holder = new ViewHolder();
			holder.b1 = (Button)convertView.findViewById(R.id.row3);
			holder.tv1 = (TextView)convertView.findViewById(R.id.row1);
			holder.tv2 = (TextView)convertView.findViewById(R.id.row2);
		   convertView.setTag(holder);
		}
		else
		{
			holder =(ViewHolder) convertView.getTag();
		}
		HashMap<string,string> map = list.get(position);
		holder.tv1.setText(map.get("Rno"));
		holder.tv2.setText(map.get("StudentName"));
		holder.b1.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if(holder.b1.getText().equals("Present"))
				{
					holder.b1.setText("Absent");
					list.get(position).put("Attendance", "Absent");
					holder.b1.setTextColor(Color.RED);

				}
				else if(holder.b1.getText().equals("Absent"))
				{
					holder.b1.setText("Present");
					list.get(position).put("Attendance", "Present");
					holder.b1.setTextColor(Color.GREEN);

				}
				else
				{
					holder.b1.setText("Present");
					list.get(position).put("Attendance", "Present");
					holder.b1.setTextColor(Color.GREEN);
				}
				String rno1=  String.valueOf(holder.tv1.getText());
				name=String.valueOf(holder.tv2.getText());
			}

		});
		return convertView;
	}
	static class ViewHolder
	{
		Button b1;
		TextView tv1,tv2,tv3;
	}

}
Posted
Updated 3-Sep-18 9:52am
v2

1 solution

I would suggest you to use recycler view instead of ListView
Recycler view comes with more decoration and methods which will definitely help you.
Have a look to offical docs page
RecyclerView  |  Android Developers[^]

And there is a know issue with recycler view when scrolling
It shuffles the item when scroll. When i was beginner it took me several hours to find its issue.
There are multiple solutions to it You can do if else conditions but it is not recommended you need to override Recyclerview two methods
getItemId() and getITemViewType And this will solve your problem
Solution reference: On Scrolling, Recyclerview shuffles or change values on list item | Solution Spirit[^]
 
Share this answer
 
v2
Comments
Member 13971576 3-Sep-18 16:00pm    
Thanks alot, you saved my day
Perfectly working solution
taniyas 25-Dec-18 22:28pm    
Thanks you for such great solution. Which works perfect on ListView I waste my complete one week of work. While doing google I found this solution which save my time.
How to implement in Xamarin Android taken guide
https://www.c-sharpcorner.com/article/xamarin-android-recyclerv/
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900