0

I have this cardlibs listview in a fragment.

I have some dynamic data coming in through the main activity.

When I first call the fragment with the listview, to populate a card, everything is cool. It' shows. I have the function set to check if the fragment is activate and if it is just call a method inside the fragment which is supposed to add another card to the list.

Host Activity

    public void pushToFeedFromActivity(JSONObject msg){
    FragmentManager fragmentManager = getFragmentManager();
    if (!myFeedActive) {
        myFeedActive = true;
        Bundle argsScene = new Bundle();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.feedInnerParent, FragFeed.newInstance(msg), "myfeed");
        fragmentTransaction.commit();

    }else {
        FragFeed fragFeed = (FragFeed) getFragmentManager().findFragmentByTag("myfeed");
        fragFeed.pushToFeed(msg);
    }
};

   @OnClick(R.id.PushToFeedButton)
    public void presstoPushToFeed(View v) {
    pushToFeedFromActivity(json)
    ......

In onActivity, I'm getting the arg from the bundle/parsing the json and passing it to the method that adds cards to the list, Like I said these instructions work the first time the fragment is added but not the subsequent times, when just the method is called directly from the activity.

Fragment

    public void onActivityCreated(Bundle savedInstanceState) {
    ....
    pushToFeed(jsonFromString);
    .....
    }

    public void pushToFeed(JSONObject json) {
    Log.v(TAG + "pushtofeed", json.toString());
    if (json != null) {
        try {
            body = jsonFromString.get("stdin").toString();
            lo = jsonFromString.get("lo").toString();
            channel = jsonFromString.get("channel").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    {
    ArrayList<Card> cards = new ArrayList<Card>();
    {
        SceneCard card = new SceneCard(getActivity());
        //CardHeader header = new CardHeader(getActivity());
        card.setClickable(true);
        //card.addCardHeader(header);
        card.setSceneCardloqoo(loqoo);
        card.setSceneCardmsgbody(body);
        card.setSceneCardchannel(channel);
        card.getSceneCardtime();
        cards.add(card);

    }


    CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
    CardListView listView = (CardListView) getActivity().findViewById(R.id.fragFeedList);
    if (listView != null) {
        listView.setVerticalScrollBarEnabled(false);
        listView.setAdapter(mCardArrayAdapter);
        Log.v(TAG + "pushtofeed - listview", json.toString());

    }
    mCardArrayAdapter.notifyDataSetChanged();

}

I've added Log statements in the fragment method, so I know for sure the calls are going through, there's just something in the list implementation, that's mixed up. What gives?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
sirvon
  • 2,547
  • 1
  • 31
  • 55

1 Answers1

1

From the creator of the lib himself, by way of github issues comes the solution....

https://github.com/gabrielemariotti/cardslib/issues/313

 @gabrielemariotti commented:

  "In your code, you are using listView.setAdapter(mCardArrayAdapter); each time.
   This code will remove all views from the list view.

   In your case if you want to add new items to an existing adapter,
   you have to retrieve your adapter, add the new items,
   calls the notifyDataSetChanged, without using listView.setAdapter again."
sirvon
  • 2,547
  • 1
  • 31
  • 55