Update common post in different ListView with different data in adapter

后端 未结 3 1535
别跟我提以往
别跟我提以往 2021-01-27 17:47

Assume That an app Firstly List all post in a ListView in Fragment ( 1st ) with a custom adapter. and that custom adapter data is

3条回答
  •  长情又很酷
    2021-01-27 18:06

    The first (single post activity) problem can be easily solved by adding the like to the category list set of data (ArrayList.get(i).addLike or whatever you use to add likes) and then call

    adapter.notifyDatasetChanged();
    

    Which will make your second list update it self with the new content (that you've just changed)

    So now the problem is the first list. as far as I understand we have 2 lists that contains an identical item, that (at least I wanna believe since it's post kind of element) has it's own unique ID, perhaps try to loop thru the first list searching for that element?

    for example if the 2nd list (category?) is now showing a post in single post activity and its ID is 5. When changing the likes amount try something like this:

    //First we change the second list item which is the first one on the backstack & we know it's position on the list
    CategoryList.get(currentPost).addLike();
    
    //Then we loop the first list to find the same post from the second list on it
    for(int i = 0; i < MainList.size(); i++)
    {
        if(MainList.get(i).getID == CategoryList.get(currentPost).getID)
        {
            //Add the like on the first list
            MainList.get(i).addLike();
            break;
        }
    }
    
    //Call update on the adapters
    MainListAdapter.notifyDatasetChanged();
    SecondListAdapter.notifyDatasetChanged();
    

提交回复
热议问题