1

When I chose one item in ListView in Xamarin Android is choosen twins . In Xamarin.Forms exist setting the Caching Strategy of ListView : RetainElement and RecycleElement .

С#

var listView = new ListView(ListViewCachingStrategy.RecycleElement);

XAML

<ListView CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
          ...
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>
</ListView>

How can I config cache in the Xamarin Android ? Cause I have are problem , when I choose some Items in the ListView it's choosen twice symmetric in another part of ListView . What I must to do to fix that ? Say me please somebody ! Than You !

  • Please elaborate a bit more or post an image to describe your problem. – York Shen Jan 10 '18 at 08:53
  • **York Shen - MSFT** Imagine ListView in the screen . I touch first item and he is change background . I scroll down and I have one more selected and when I choose more items I get symmetric choosen items in another part of ListView. Problem is in RecycleView . I think indexes is repeat . For example if I choose in the programm in the adapter an item he is work correct . But when I choose in List item ,which is in the adapter , I get wrong item often . How can I work with RecycleView , or maybe to off him ? – Andrei Damaschin Jan 10 '18 at 09:43
  • Your code in the question is Xamarin.Forms, but RecycleView is belong to native android, what is your project type? Xamarin.Forms or Xamarin.Android? – York Shen Jan 10 '18 at 09:45
  • Xamarin Android but I thought it same structure. So if not I dont know in what is problem. – Andrei Damaschin Jan 10 '18 at 09:46
  • Please post your Xamarin.Android ListView code. – York Shen Jan 10 '18 at 09:50
  • I'm sorry but my code is not wrong . Problem is in the caching ListView and get List<> inside his adapter are index . It's impossible that nobody meet this . – Andrei Damaschin Jan 10 '18 at 09:58
  • If we can't see your code, we can't find the problem. – York Shen Jan 10 '18 at 11:29
  • @YorkShen-MSFT Can explain me please what ViewHolder System is in this post [link](https://stackoverflow.com/questions/42806583/xamarin-mutli-selection-listview?rq=1) ? And please say me , you said that RecycleView is only in Xamarin.Forms , but in this answer is code with RecyclerView and in the tags is Xamarin Android ,is are mistake ? – Andrei Damaschin Jan 10 '18 at 12:06
  • [ListView](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/) in Xamarin.Forms, [RecyclerView](https://developer.xamarin.com/guides/android/user_interface/layouts/recycler-view/) in native Android. – York Shen Jan 10 '18 at 12:10
  • Delete the `ListAdapter.Remove(contactList[e.Position])` will solve your problem. – York Shen Jan 10 '18 at 12:47
  • Im sorry @YorkShen-MSFT will not resolve . I made `ListAdapter.Remove(contact List[in.Position])` to see are differences what we delete and what is in Console . – Andrei Damaschin Jan 10 '18 at 13:35

2 Answers2

0
        List<string> contactList;
    ListView list;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
        list = FindViewById<ListView>(Resource.Id.listView1);
        var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;
        string[] projection = { ContactsContract.CommonDataKinds.Phone.Number, ContactsContract.Contacts.InterfaceConsts.DisplayName };
        var cursor = ContentResolver.Query(uri, projection, null, null, null);
        contactList = new List<string>();
        if (cursor.MoveToFirst())
        {
            while (cursor.MoveToNext())
                contactList.Add(cursor.GetString(cursor.GetColumnIndex(projection[0])) + cursor.GetString(cursor.GetColumnIndex(projection[1])));
        }
        var ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.ContactItemView, contactList);
        list.Adapter = ListAdapter;

        list.ItemClick += (sender, e) => { ListAdapter.Remove(contactList[e.Position]); Console.WriteLine(ListAdapter.GetItem(e.Position)); };}
0

recycling works like this: when you display your list, let's say there are 6 items displayed, then you will have a pool of at least 6 cell instances. When you scroll, the instances of the cells that disappeared "return" to the "unused" instances pool. This pool is used when the listview has to display a new cell, so that the existing instances get used again for other lines, with other content.

Please look at this answer for great explanation

Your issue seems to be that the cell (view instance) state changes when you click on a cell, and is not reset when reusing that instance.

You should keep a trace of the state of each cell's content and assign it when getting any cell. The ViewHolder Pattern is a good way to manage it

Hope it helps

AdricoM
  • 579
  • 4
  • 11