2016-03-22 18 views
0

以下のクラスで概要を説明したタブ付きフラグメントがあります。タブ付きフラグメントのみ最初のフラグメントを表示する

友だちとグループの情報のリストが2つあります。私が抱えている問題は、グループのリストが最初に「フレンド」タブ内でレンダリングされ、次に「フレンド」リストが「フレンド」タブ内でレンダリングできるように消えることです。ただし、[グループ]タブにスワイプすると、グループリストはレンダリングされません。

どうすれば解決できますか?

タブ付きの活動:

public class InvitingFriendsGroupsToEvents extends AppCompatActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_inviting_friends_groups_to_events); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_inviting_friends_groups_to_events, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public PlaceholderFragment() { 
     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_inviting_friends_groups_to_events, container, false); 
      TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
      textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 

     @Override 
     public Fragment getItem(int position) { 

      // Need to build 
      // Order/first appearing will be specific to time of day, ie. if in morning, then breakfast/brunch, if midday then lunch, late then dinner 
      switch (position) { 
       case 0: 
        return new InviteFriendsToEventsFragment(); 
       case 1: 
        return new InviteGroupsToEventsFragment(); 
      } 

      return null; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "Friends"; 
       case 1: 
        return "Groups"; 
      } 
      return null; 
     } 
    } 
} 

フレンドタブ:

public class InviteFriendsToEventsFragment extends Fragment { 
    // ie. once a fiend is invited, it will show their group on the second tab, the name, who invite them 

    MyCustomAdapter dataAdapter = null; 
    SaveSharedPreference preference = new SaveSharedPreference(); 

    final Firebase myFeastFirebase = new Firebase(Constants.getFirebaseUrl()); 
    Firebase presentEventMembers; 
    Firebase myFriends; 
    Firebase sentFriendEventInvites; // this will be the grayed out standing invitation 

    Firebase eventMemberInviteNeedSync; 

    ValueEventListener getFriendsListener; 
    ValueEventListener getEventMembersListener; 
    ValueEventListener getAlreadyInvitedFriendsListener; 

    String eventID; 
    String eventName; 

    ArrayList<Member> mEventMembers = new ArrayList<>(); 
    ArrayList<Member> mFriends = new ArrayList<>(); 
    ArrayList<Member> mAlreadyInvited = new ArrayList<>(); 
    ArrayList<FriendInvite> mFriendsFinalInviteStatuses = new ArrayList<>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     View view = inflater.inflate(R.layout.invite_friends_to_events, container, false); 

     Bundle extras = getActivity().getIntent().getExtras(); 
     eventID = extras.getString("eventID"); 
     eventName = extras.getString("eventName"); 
     Log.v("InvitingFriendsToEvents", "Event ID: " + eventID); 
     Log.v("InvitingFriendsToEvents", "Event Name" + eventName); 

     //Generate list View from ArrayList 
     // Get present group members 
     presentEventMembers = myFeastFirebase.child("eventMembers").child(eventID); 
     myFriends = myFeastFirebase.child("friends").child(preference.getUserSyncID(getContext())); 
     sentFriendEventInvites = myFeastFirebase.child("userEventMemberInviteSent").child(preference.getUserSyncID(getContext())).child(eventID); 

     eventMemberInviteNeedSync = myFeastFirebase.child("eventMemberInviteNeedSync"); 

     getAlreadyInvitedFriendsListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot inviteSnapshot : dataSnapshot.getChildren()) { 

        Member invitedFriend = new Member(); 
        invitedFriend.setUserUID(inviteSnapshot.getKey()); 
        invitedFriend.setUserName(inviteSnapshot.getValue().toString()); 

        mAlreadyInvited.add(invitedFriend); 

        Log.v("InvitingFriendsToEvents", "Invite Sent userName is: " + inviteSnapshot.getValue().toString()); 
        Log.v("InvitingFriendsToEvents", "Invite Sent ID is: " + inviteSnapshot.getKey().toString()); 

       } 

       // Here will be the operation to get the proper setting for 
       // First: remove from friends, those already in the group: 
       mFriends.removeAll(mEventMembers); 

       // Then add to the invite group 
       for (Member friend : mFriends) { 
        FriendInvite groupMemberInvite = new FriendInvite(); 
        groupMemberInvite.setUserName(friend.getUserName()); 
        groupMemberInvite.setUserUID(friend.getUserUID()); 

        if (mAlreadyInvited.contains(friend)) { 
         groupMemberInvite.setInvitedAlready(true); 
        } 
        mFriendsFinalInviteStatuses.add(groupMemberInvite); 

        Log.v("InvitingFriendsToEvents", "Final List UserName is: " + groupMemberInvite.getUserName()); 
        Log.v("InvitingFriendsToEvents", "Final List UserID is: " + groupMemberInvite.getUserUID()); 
        Log.v("InvitingFriendsToEvents", "Final List InviteStatus is: " + groupMemberInvite.isInvitedAlready()); 
       } 
       //dataAdapter.notifyDataSetChanged(); 

       //create an ArrayAdaptar from the String Array 
       dataAdapter = new MyCustomAdapter(getActivity(), 
         R.layout.country_info, mFriendsFinalInviteStatuses); 
       ListView listView = (ListView) getActivity().findViewById(R.id.listView1); 
       // Assign adapter to ListView 
       listView.setAdapter(dataAdapter); 

       dataAdapter.notifyDataSetChanged(); 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 

      } 
     }; 

     getFriendsListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot friendSnapshot : dataSnapshot.getChildren()) { 

        if (!friendSnapshot.getKey().equals(preference.getUserSyncID(getContext()))) { 
         Member friend = new Member(); 
         friend.setUserUID(friendSnapshot.getKey()); 
         friend.setUserName(friendSnapshot.child("userName").getValue().toString()); 

         mFriends.add(friend); 

         Log.v("InvitingFriendsToEvents", "Friend userName is: " + friendSnapshot.child("userName").getValue().toString()); 
         Log.v("InvitingFriendsToEvents", "Friend user ID is: " + friendSnapshot.getKey().toString()); 
        } 
       } 

       // after getting friends 
       sentFriendEventInvites.addListenerForSingleValueEvent(getAlreadyInvitedFriendsListener); 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
       Log.v("InvitingFriendsToEvents", "Firebase Error: " + firebaseError.toString()); 

      } 
     }; 

     getEventMembersListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot groupMemberSnapshot : dataSnapshot.getChildren()) { 

        if (!groupMemberSnapshot.getKey().equals(preference.getUserSyncID(getContext()))) { 
         Member groupMember = new Member(); 
         groupMember.setUserUID(groupMemberSnapshot.getKey()); 
         groupMember.setUserName(groupMemberSnapshot.getValue().toString()); 

         mEventMembers.add(groupMember); 

         Log.v("InvitingFriendsToEvents", "Group member userName is: " + groupMemberSnapshot.getValue().toString()); 
         Log.v("InvitingFriendsToEvents", "Group member user ID is: " + groupMemberSnapshot.getKey().toString()); 
        } 
       } 

//     upon completion of getting groups 
       myFriends.addListenerForSingleValueEvent(getFriendsListener); 

      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
       Log.v("InvitingFriendsToEvents", "Firebase Error: " + firebaseError.toString()); 

      } 
     }; 
     presentEventMembers.addListenerForSingleValueEvent(getEventMembersListener); 

     //confirmFriendInvites(); 

     return view; 
    } 

    private class MyCustomAdapter extends ArrayAdapter<FriendInvite> { 

     private ArrayList<FriendInvite> groupMemberInviteList; 

     public MyCustomAdapter(Context context, int textViewResourceId, 
           ArrayList<FriendInvite> groupMemberInviteList) { 
      super(context, textViewResourceId, groupMemberInviteList); 
      this.groupMemberInviteList = new ArrayList<>(); 
      this.groupMemberInviteList.addAll(groupMemberInviteList); 
     } 

     private class ViewHolder { 
      TextView inviteSent; 
      CheckBox checkBox; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      ViewHolder holder = null; 
      Log.v("ConvertView", String.valueOf(position)); 

      if (convertView == null) { 
       LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
         Context.LAYOUT_INFLATER_SERVICE); 
       convertView = vi.inflate(R.layout.country_info, null); 

       holder = new ViewHolder(); 
       holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); // here it is 
       holder.inviteSent = (TextView) convertView.findViewById(R.id.invite_already_sent); 

       convertView.setTag(holder); 

       holder.checkBox.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         CheckBox cb = (CheckBox) v; 
         FriendInvite groupMemberInivited = (FriendInvite) cb.getTag(); 
         Toast.makeText(getActivity(), 
           "Clicked on Checkbox: " + cb.getText() + 
             " is " + cb.isChecked(), 
           Toast.LENGTH_LONG).show(); 
         groupMemberInivited.setSelected(cb.isChecked()); 
        } 
       }); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      FriendInvite groupMemberInvite = groupMemberInviteList.get(position); 

      holder.checkBox.setText(groupMemberInvite.getUserName()); 
      holder.checkBox.setChecked(groupMemberInvite.isSelected()); 
      holder.checkBox.setTag(groupMemberInvite); 

      // Check if already invited, if so, should remove 
      if (groupMemberInvite.isInvitedAlready() == true) { 

       holder.checkBox.setTextColor(Color.GRAY); 
       holder.checkBox.setClickable(false); 

       holder.inviteSent.setText("Invite Already Sent"); 
      } else { 
       holder.inviteSent.setText(""); 
      } 
      return convertView; 
     } 

    } 

} 

グループタブ:

パブリッククラスInviteGroupsToEventsFragmentが断片{// すなわちを拡張します。悪魔が招待されると、それはそれらを招待する2つ目のタブ、名前、上の自分のグループを表示します

GroupInviteCustomAdapter dataAdapter = null; 
SaveSharedPreference preference = new SaveSharedPreference(); 

final Firebase myFeastFirebase = new Firebase(Constants.getFirebaseUrl()); 
Firebase presentEventMembers; 
Firebase sentFriendEventInvites; // this will be the grayed out standing invitation 

Firebase mGroups; 
Firebase mGroupMembers; 

Firebase eventMemberInviteNeedSync; 

ValueEventListener getFriendsListener; 
ValueEventListener getEventMembersListener; 
ValueEventListener getAlreadyInvitedFriendsListener; 
ValueEventListener getUsersGroups; 

String eventID; 
String eventName; 

ArrayList<Member> mEventMembers = new ArrayList<>(); 
ArrayList<Member> mFriends = new ArrayList<>(); 
ArrayList<Member> mAlreadyInvited = new ArrayList<>(); 
ArrayList<FriendInvite> mFriendsFinalInviteStatuses = new ArrayList<>(); 

ArrayList<GroupInvite> mUsersGroups = new ArrayList<>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    View view = inflater.inflate(R.layout.invite_groups_to_events, container, false); 

    Bundle extras = getActivity().getIntent().getExtras(); 
    eventID = extras.getString("eventID"); 
    eventName = extras.getString("eventName"); 
    Log.v("InvitingFriendsToEvents", "Event ID: " + eventID); 
    Log.v("InvitingFriendsToEvents", "Event Name" + eventName); 

    //Generate list View from ArrayList 
    // Get present group members 
    presentEventMembers = myFeastFirebase.child("eventMembers").child(eventID); 
    mGroupMembers = myFeastFirebase.child("groupMembers"); // will append: .child("") 
    mGroups = myFeastFirebase.child("usersGroups").child(preference.getUserSyncID(getContext())); 
    sentFriendEventInvites = myFeastFirebase.child("userEventMemberInviteSent").child(preference.getUserSyncID(getContext())).child(eventID); 

    eventMemberInviteNeedSync = myFeastFirebase.child("eventMemberInviteNeedSync"); 

    getUsersGroups = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot inviteSnapshot : dataSnapshot.getChildren()) { 

       GroupInvite invitedGroup = new GroupInvite(); 
       invitedGroup.setGroupSyncID(inviteSnapshot.getKey()); 
       invitedGroup.setGroupName(inviteSnapshot.getValue().toString()); 

       mUsersGroups.add(invitedGroup); 

       Log.v("InvitingFriendsToEvents", "Invite Sent userName is: " + inviteSnapshot.getValue().toString()); 
       Log.v("InvitingFriendsToEvents", "Invite Sent ID is: " + inviteSnapshot.getKey().toString()); 

      } 

      dataAdapter = new GroupInviteCustomAdapter(getActivity(), 
        R.layout.event_invites_groups, mUsersGroups); 
      ListView listView = (ListView) getActivity().findViewById(R.id.listView1); 
      // Assign adapter to ListView 
      listView.setAdapter(dataAdapter); 

      dataAdapter.notifyDataSetChanged(); 

     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) { 

     } 
    }; 


    mGroups.addListenerForSingleValueEvent(getUsersGroups); 


    return view; 
} 

private class GroupInviteCustomAdapter extends ArrayAdapter<GroupInvite> { 

    private ArrayList<GroupInvite> groupInviteList; 

    public GroupInviteCustomAdapter(Context context, int textViewResourceId, 
            ArrayList<GroupInvite> groupInviteList) { 
     super(context, textViewResourceId, groupInviteList); 
     this.groupInviteList = new ArrayList<>(); 
     this.groupInviteList.addAll(groupInviteList); 
    } 

    private class ViewHolder { 
     TextView inviteSent; 
     CheckBox checkBox; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder = null; 
     Log.v("ConvertView", String.valueOf(position)); 

     if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.event_invites_groups, null); 

      holder = new ViewHolder(); 
      holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); // here it is 
      holder.inviteSent = (TextView) convertView.findViewById(R.id.invite_already_sent); 

      convertView.setTag(holder); 

      holder.checkBox.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        GroupInvite groupInvited = (GroupInvite) cb.getTag(); 
        Toast.makeText(getActivity(), 
          "Clicked on Checkbox: " + cb.getText() + 
            " is " + cb.isChecked(), 
          Toast.LENGTH_LONG).show(); 
        groupInvited.setSelected(cb.isChecked()); 
       } 
      }); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     GroupInvite groupInvite = groupInviteList.get(position); 

     holder.checkBox.setText(groupInvite.getName()); 
     holder.checkBox.setChecked(groupInvite.isSelected()); 
     holder.checkBox.setTag(groupInvite); 

     // Check if already invited, if so, should remove 
     if (groupInvite.isInvitedAlready() == true) { 

      holder.checkBox.setTextColor(Color.GRAY); 
      holder.checkBox.setClickable(false); 

      holder.inviteSent.setText("Invite Already Sent"); 
     } else { 
      holder.inviteSent.setText(""); 
     } 
     return convertView; 
    } 

} 

}

答えて

0

移動コード外getUsersGroups =の下にブロック新しいValueEventListener()

  dataAdapter = new GroupInviteCustomAdapter(getActivity(), 
       R.layout.event_invites_groups, mUsersGroups); 
     ListView listView = (ListView) getActivity().findViewById(R.id.listView1); 
     // Assign adapter to ListView 
     listView.setAdapter(dataAdapter); 

     dataAdapter.notifyDataSetChanged(); 

ValueEventListener()は、オーバーライドメソッドのみを実行します。私の推測では、上記のコードは決して実行されません。それが動作するかどうか試してみてください。それができない場合、さらにデバッグします。

+0

これは非同期操作なので、コールバックの内部にある必要があります。私はあなたのアプローチをテストしてまだ空白ですが、今は友だちの断片でレンダリングされずに消えてしまってもレンダリングされません – Sauron

+0

チャットに入ることはできますか? – Sauron

+0

申し訳ありませんでした。とにかくあなたのListViewのFirebaseUIを使用しないでください。それは同期を処理します。あなたのプロジェクトで使用することは可能でしょうか? –