2017-05-06 3 views
0

i zwei Fragmente haben und deren Verwendung bei tabs Schöbe i Objekt von Fragmente (1) bis Fragment übergeben werden soll (2), wo dieses Objekt aus (Web-Service) kommtObjekt zwischen gleitenden Laschen vorbei Fragmenten

diese Aktivität verantwortlich für tabs

public class ChartActivity extends AppCompatActivity { 

private static final String TAG="ChartActivity"; 

private ViewPager mPager; 

private SlidingTabLayout tabLayout; 

private Toolbar toolbarChart; 

private TextView txtToolbar; 

private AdapterTabs adapterTabs; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chart); 
    Log.e(TAG,"starting to Create Chart"); 
    setupToolBar(); 
    setupTabs(); 
} 



// setup ToolBar 
public void setupToolBar() { 
    toolbarChart = (Toolbar) findViewById(R.id.app_bar_chart); // get instance from view 
    setSupportActionBar(toolbarChart); // set Toolbar in supporting ActionBar 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 
    txtToolbar=(TextView)toolbarChart.findViewById(R.id.toolbar_title); 
    txtToolbar.setText("Dashboard"); 
} 

public void setupTabs(){ 
    tabLayout=(SlidingTabLayout)findViewById(R.id.tabs); 
    mPager=(ViewPager)findViewById(R.id.viewPager); 
    adapterTabs= new AdapterTabs(this,getSupportFragmentManager()); 
    mPager.setAdapter(adapterTabs); 
    tabLayout.setDistributeEvenly(true); 
    tabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.colorAccent); 
     } 
    }); 
    tabLayout.setViewPager(mPager); 
} 

public AdapterTabs getAdapterTabs() { 
    return adapterTabs; 
} 

} 

dieses Fragment (1)

public class InpatientFragment extends Fragment { 


private final static String INPATIENT_STATE="inpatient"; 

public final static String OutPatient_STATE="outpatient"; 

private RequestQueue request; 

private PieChart chart; 

ArrayList<Patient> inpatients; 

ArrayList<Patient> outPatient; //outPatient=====> show it in fragment(2) 

private ProgressDialog progressDialog; 

private VolleySingleton singleton; 

private final static String TAG = "InpatientFragment"; 

public InpatientFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    progressDialog = new ProgressDialog(getActivity()); 
    progressDialog.setIndeterminate(true); 
    progressDialog.setMessage("Load Data..."); 
    requsetDatachart(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View layout = inflater.inflate(R.layout.fragment_inpatient, container, false); 
    chart = (PieChart) layout.findViewById(R.id.pieChart); 
    chart.setCenterTextSize(10); 
    chart.setCenterText("Inpatient"); 
    chart.setDrawEntryLabels(true); 
    chart.setHoleRadius(2.5f); 
    return layout; 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
} 

private void requsetDatachart() { 
    singleton = VolleySingleton.getInstance(); 
    request = singleton.getRequestQueue(); 
    progressDialog.show(); 
    JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, MyApplication.APP_URL + "api/getOverview", null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        inpatients = PatientParser.parserJsonPatient(response, "INPATIENT", 1); 
        Log.e(TAG, inpatients.size()+""); 
        outPatient=PatientParser.parserJsonPatient(response,"OUTPATIENT",0); 
        addDataToChart(inpatients); 
        progressDialog.dismiss(); 

       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, error.getMessage()); 
      progressDialog.dismiss(); 

     } 
    }); 
    request.add(objectRequest); 
} 

public void addDataToChart(List<Patient> patients){ 
    ArrayList<PieEntry>pieEntries=new ArrayList<>(); 
    for (int i=0;i<patients.size();i++){ 
     pieEntries.add(new PieEntry(Float.parseFloat(patients.get(i).getResult_val()),patients.get(i).getName_en())); 
    } 

    // add color to data set 
    ArrayList<Integer> colors=new ArrayList<>(); 
    colors.add(Color.BLUE); 
    colors.add(Color.GREEN); 
    colors.add(Color.YELLOW); 
    // form data set slicing between containing and size of text 
    PieDataSet dataSet=new PieDataSet(pieEntries,"InPatient"); 
    dataSet.setSliceSpace(3); 
    dataSet.setValueTextSize(13); 
    dataSet.setColors(colors); 
    /// form of chart through legend object 
    Legend legend=chart.getLegend(); 
    legend.setForm(Legend.LegendForm.CIRCLE); 
    // set data set to pieData 
    PieData data=new PieData(dataSet); 
    chart.setData(data); 
    chart.invalidate(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putParcelableArrayList(INPATIENT_STATE,inpatients); 
} 


} 

und dieses Fragment 2 i-Objekt in diesem Fragment Ambulanter ==> Liste in der Splitter zeigen möchten (1)

public class OutpatientFragment extends Fragment { 


private final static String TAG = "OutpatientFragment"; 

private static final String ARG_PARAM1 = "param1"; 

ArrayList<Patient> outPatient; 

private RequestQueue request; 

private PieChart chart; 

private ProgressDialog progressDialog; 

private VolleySingleton singleton; 



public OutpatientFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View layout=inflater.inflate(R.layout.fragment_outpatient, container, false); 
    //getArguments().getBundle(InpatientFragment.OutPatient_STATE).getParcelableArrayList(); 
    chart = (PieChart) layout.findViewById(R.id.pieChart); 
    chart.setCenterTextSize(10); 
    chart.setCenterText("Outpatient"); 
    chart.setDrawEntryLabels(true); 
    chart.setHoleRadius(2.5f); 
    return layout; 
} 

@Override 
public void onResume() { 
    super.onResume(); 

} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    progressDialog = new ProgressDialog(getActivity()); 
    progressDialog.setIndeterminate(true); 
    progressDialog.setMessage("Load Data..."); 
    } 


    } 

Antwort

0

Ich denke, Sie können EventBus dafür verwenden. Zum Beispiel

In InpatientFragment Anruf EventBus.getDefault().post(new MessageEvent(<Your object>)) wenn Daten empfangen.

In OutpatientFragment erstellen Verfahren zur Datenbeispiels Empfangen

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(MessageEvent event) {/* Do something */}; 
Verwandte Themen