I have finished a simple app to list all products from a mysql database via json. When I click a specific product, it takes me to an activity that displays it's details. Now I changed my app so that when I click a specific product from the list it goes to a 3 tabbed fragment. I have created all the relevant fragments and everything works fine. It only doesn't work when I try to pass the data from the list all products activity to the details fragment activity. How can I pass the data from the main screen which lists all products to the 3 fragments that display the details?
further explanation:
working example without the framents: Main activity displays all products and passes it to details activity upon selecting a single product-> details activity gets data from main activity and displays details
Now I have the main activity which displays all products. Do I pass the data to -> "public class EmpresaDetailsActivity extends FragmentActivity implements ActionBar.TabListener { etc" or do I have to pass it to all other 3 single fragments that I created?
(old way)List all products:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_empresas);
empresaList = new ArrayList<HashMap<String, String>>();
// Get listview
ListView lv = getListView();
// on selecting single empresa
// launching Empresa Details Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String eid = ((TextView) view.findViewById(R.id.eid)).getText().toString();
String marca = ((TextView) view.findViewById(R.id.marca)).getText().toString();
String investimento = ((TextView) view.findViewById(R.id.investimento)).getText().toString();
String marcatotal = ((TextView) view.findViewById(R.id.marcatotal)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),EmpresaDetailsActivity.class);
// sending data to next activity
in.putExtra(TAG_EID, eid);
in.putExtra(TAG_MARCA, marca);
in.putExtra(TAG_INVESTIMENTO, investimento);
in.putExtra(TAG_MARCATOTAL, marcatotal);
// starting new activity and expecting some response back
startActivity(in);
}
});
// Loading empresa in Background Thread
new LoadAllEmpresa().execute();
}
(old way)Details of a single product:
public class EmpresaDetailsActivity extends Activity {
// JSON node keys
private static final String TAG_EID = "eid";
private static final String TAG_MARCA = "marca";
private static final String TAG_INVESTIMENTO = "investimento";
private static final String TAG_MARCATOTAL = "marcatotal";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_empresa);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String eid = in.getStringExtra(TAG_EID);
String marca = in.getStringExtra(TAG_MARCA);
String investimento = in.getStringExtra(TAG_INVESTIMENTO);
String marcatotal = in.getStringExtra(TAG_MARCATOTAL);
// Displaying all values on the screen
TextView lblEid = (TextView) findViewById(R.id.showEid);
TextView lblName = (TextView) findViewById(R.id.showMarca);
TextView lblInvestimento = (TextView) findViewById(R.id.showInvestimento);
TextView lblMarcatotal = (TextView) findViewById(R.id.showMarcaTotal);
lblEid.setText(eid);
lblName.setText(marca);
lblInvestimento.setText(investimento);
lblMarcatotal.setText(marcatotal);
}
}
New Details class that has the 3 fragments:
public class EmpresaDetailsActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Apresentação", "Ficha Técnica", "Pedir Informação" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_global_frag);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
TabsPagerAdapter class:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new ApresentacaoFragment();
case 1:
// Games fragment activity
return new FichaTecnicaFragment();
case 2:
// Movies fragment activity
return new PedirInformacaoFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
example of 1 of the 3 fragments:
public class ApresentacaoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_apresentacao, container, false);
return rootView;
}
}