2014-02-15 2 views
0

У меня есть фрагмент, содержащий расширяемый ListView код, для которого:не в состоянии передать строку из одного фрагмента к другому, используя Bundle

package x; 

import x.expandablelistadapter.ExpListAdapter; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 
import android.widget.ExpandableListView.OnGroupExpandListener; 

//import android.provider.DocumentsContract.Document; 

public class EventFragment extends Fragment { 
    private String[] categories; 
    private String[][] categoryItems; 
    private ExpandableListView expandableListView; 
    private ExpandableListAdapter expandableListAdapter; 
    private int lastExpandedGroup; 

    public EventFragment() { 
     this.lastExpandedGroup = -1; 
    } 

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

     // get the event categories 
     categories = getResources().getStringArray(R.array.eventCategory); 
     // get the computer science games 
     String[] category1 = getResources().getStringArray(R.array.csEvents); 
     // get the ec games 
     String[] category2 = getResources().getStringArray(R.array.ecEvents); 
     // get the online games 
     String[] category3 = getResources().getStringArray(R.array.onEvents); 
     // get the robotics games 
     String[] category4 = getResources().getStringArray(R.array.rbEvents); 
     // get all the category items 
     categoryItems = new String[][] { category3, category1, category2, 
       category4 }; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.event_layout, container, 
       false); 
     return rootView; 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     // get the expandable list view 
     expandableListView = (ExpandableListView) view 
       .findViewById(R.id.eventListExp); 
     expandableListAdapter = new ExpListAdapter(categories, categoryItems, 
       getActivity()); 
     expandableListView.setAdapter(expandableListAdapter); 
     expandableListView.setGroupIndicator(null); 
     expandableListView 
       .setOnGroupExpandListener(new OnGroupExpandListener() { 

        @Override 
        public void onGroupExpand(int groupPosition) { 
         if (groupPosition != lastExpandedGroup 
           && lastExpandedGroup != -1) { 
          expandableListView.collapseGroup(lastExpandedGroup); 
         } 
         lastExpandedGroup = groupPosition; 
        } 

       }); 

     expandableListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       // TODO Auto-generated method stub 

       String event = (String) expandableListAdapter.getChild(
         groupPosition, childPosition); 
       EventFragment fragment = new EventFragment(); 
       Bundle bundle = new Bundle(); 
       bundle.putString("Event", event); 
       Log.d("Bundle", bundle.toString()); 
       fragment.setArguments(bundle); 

       EventDetailsFragment eventDetailsFragment = new EventDetailsFragment(); 

       android.support.v4.app.FragmentTransaction fragTransaction = getFragmentManager() 
         .beginTransaction(); 
       fragTransaction.replace(R.id.content_frame, 
         eventDetailsFragment); 
       // fragTransaction.addToBackStack(null); 
       fragTransaction.commit(); 

       return false; 
      } 
     }); 

    } 

} 

Теперь по событию ребенок клик Я хочу передать значение ребенка к другому фрагменту.

Мой второй фрагмент:

package x; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class EventDetailsFragment extends Fragment { 
    String eventString; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle args = getArguments(); 
     //Log.d("Bundleargs",""+ args.toString()); 
     if (args != null && args.containsKey("Event")) 
      eventString = args.getString("Event"); 
     else 
      eventString="Nothing"; 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.eventdetails_layout, container, 
       false); 


     TextView eventTextView = (TextView)rootView.findViewById(R.id.eventTextView); 
     eventTextView.setText(eventString); 

     return rootView; 
    } 
} 

Здесь я не могу получить значение, хранящееся в пачке, даже если значение в пучке в первом фрагменте становится хранится правильно.

В чем проблема?

ответ

1

Изменить

  fragment.setArguments(bundle); 

в

  eventDetailsFragment.setArguments(bundle); 

потому, что вы устанавливаете пакет для fragment но проходя eventDetailsFragment экземпляр в fragTransaction.replace

Смежные вопросы