Update common post in different ListView with different data in adapter

后端 未结 3 1530
别跟我提以往
别跟我提以往 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:04

    You can create the model of the data bind to your list view and populate the list view with that data.

    To have post execution of something like making like button you can use broadcast receiver(local) or can use tinyBus or otto to have call backs for any kind of event.

    After getting the callback just notify the particular list in fragments.

    Trick lies between model formation and callbacks.

    0 讨论(0)
  • 2021-01-27 18:06

    You can explicitly force the back button state to not be added to the back stack with transaction.addToBackStack(null). You can then override the back button (inside onBackPressed()) and insert whatever behavior you need. I'm not sure I'm following everything above, but I think that this combo will take care of the fundamental issue you address.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题