我已经完成了一个简单的应用程序,可以通过json列出mysql数据库中的所有产品。当我点击特定产品时,它会将我带到显示其详细信息的活动。 现在我更改了我的应用程序,以便当我从列表中单击特定产品时,它将转到3个选项卡式片段。我创建了所有相关的片段,一切正常。当我尝试将数据从列表中的所有产品活动传递到详细信息片段活动时,它才起作用。如何将主屏幕中列出所有产品的数据传递给显示详细信息的3个片段?
进一步说明:
没有框架的工作示例: 主要活动显示所有产品,并在选择单个产品时将其传递给详细信息活动 - >详细活动从主要活动获取数据并显示详细信息
现在我有一个显示所有产品的主要活动。我是否将数据传递给 - > "公共类EmpresaDetailsActivity extends FragmentActivity实现ActionBar.TabListener {etc"或者我必须将它传递给我创建的所有其他3个单个片段吗?
(旧方式)列出所有产品:
@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();
}
(旧方式)单品的详细信息:
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);
}
}
包含3个片段的New Details类:
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类:
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;
}
}
3个片段中的1个的例子:
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;
}
}
答案 0 :(得分:5)
在你的活动onItemClick()上,在启动片段之前:
Bundle args = new Bundle();
args.putString(TAG_EID, eid);
args.putString(TAG_MARCA, marca);
args.putString(TAG_INVESTIMENTO, invetstimento);
args.putString(TAG_MARCATOTLA, marcatotal);
yourFragment.setArguments(args);
在片段OnCreateView()上:
Bundle args = getArguments();
String marca = args.getString(TAG_MARCA);
// and so on...
更清楚的是,您应该如何在Activities / Fragments之间传递数据。
检查新代码后:
将TabsPagerAdapter更改为:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private Bundle args;
public TabsPagerAdapter(FragmentManager fm, Bundle args) {
super(fm);
this.args = args;
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
ApresentacaoFragment apresentacaoFragment = new ApresentacaoFragment();
apresentacaoFragment.setArguments(args);
return apresentacaoFragment;
case 1:
// Games fragment activity
// do the same as above with your Games fragmnet class
case 2:
// Movies fragment activity
// ...
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
答案 1 :(得分:0)
由于EmpresaDetailsActivity
是包含您的三个Activity
的{{1}},我认为将数据从Fragments
传递到MainActivity
是一种很好的做法,然后从那里传递给EmpresaDetailsActivity
。那样你就尊重等级制度。
要将值从Fragments
传递到Fragment
,您可以执行以下操作:
1)在每个Activity
类中定义一个名为newInstance的静态方法,您将获得所有值
Fragment
2)在创建public static YourFragmentClass newInstance(int myValue, int MyValue2) {
// You can add as many values as you need to initialize your fragment
YourFragmentClass fragment = new YourFragmentClass();
Bundle args = new Bundle();
args.putInt("value_key", myValue);
args.putInt("value_key2", myValue2);
fragment.setArguments(args);
return fragment;
}
时,在TabAdapter中,只需将您的Fragments
声明替换为
Fragment
3)在每个YourFragmentClass.newInstance(value1, value2);
的{{1}}方法中检索您传递的值:
onCreate
通过这种方式,您可以更清晰地初始化Fragment