我无法让我的应用程序成功地将数据从一个片段发送到另一个片段。
MainActivity.java设置我的片段标签
package myPackage;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_HOME));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_PERSON_INFO));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_3));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_4));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_5));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_6));
tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_7));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@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_main, 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);
}
}
PageAdapter.java
package myPackage;
public class PagerAdapter extends FragmentStatePagerAdapter {
int numberOfTabs;
public PagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.numberOfTabs = numberOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Home home = new Home();
return home;
case 1:
PersonInformation personInformation = new PersonInformation();
return personInformation;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
Tab4 tab4 = new Tab4();
return tab4;
case 4:
Tab5 tab5 = new Tab5();
return tab5;
case 5:
Tab6 tab6 = new Tab6();
return tab6;
case 6:
Tab7 tab7 = new Tab7();
return tab7;
default:
return null;
}
}
@Override
public int getCount() {
return numberOfTabs;
}
}
发送片段
打包myPackage;
public class Home extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor personCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private Activity myActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
myDBHelper = new DatabaseHelper(getActivity());
personCursor = myDBHelper.getPersonCursor();
String[] fromColumns = {"personID"};
int[] toViews = {R.id.person_id_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.person_layout, personCursor, fromColumns, toViews, 0);
ListView myListView = (ListView) rootView.findViewById(R.id.person_row);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
String personIDNumber = subCursor.getString(subCursor.getColumnIndex("personID"));
PersonInformation personInformation = new PersonInformation();
Bundle myBundle = new Bundle();
myBundle.putString("personID", personIDNumber);
personInformation.setArguments(myBundle);
getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();
}
});
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
return rootView;
}
}
接收片段
打包myPackage;
public class PersonInformation extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor informationCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private TextView personIDDisplay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.person_information, container, false);
personID = getArguments().getString("personID");
myDBHelper = new DatabaseHelper(getActivity());
informationCursor = myDBHelper.getInformationCursor(personID);
String[] fromColumns = {"firstname", "lastname", "homephone", "homeaddress"};
int[] toViews = {R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.information_layout, informationCursor, fromColumns, toViews, 0);
personIDDisplay = (TextView) rootView.findViewById(R.id.person_id_display);
personIDDisplay.setText("Person ID: " + personID);
ListView myListView = (ListView) rootView.findViewById(R.id.row_of_information);
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
return rootView;
}
}
person_information.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/person_information_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/top_linear_layout">
<TextView android:id="@+id/person_id_display" style="@style/personIDDisplay" />
<include layout="@layout/person_table_header" />
<ListView
android:id="@+id/row_of_information"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
我要么
addView(View) not supported in AdapterView
当我使用
时getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();
// notice the R.id.row_of_information
如果我使用ListView父视图ID,哪个是我的ListView的人员信息,或者我在接收类中得到NullPointerException。
我已经尝试将接收ListView封装在另一个布局中并发送到该ID而不是ListView的ID,但这也没有奏效。所有关于如何将数据从一个片段发送到另一个片段的示例看起来非常相似,所以我无法弄清楚为什么我的工作不起作用。
答案 0 :(得分:0)
警告: 不是最好的方法,但现在可以做。更好的解决方案是使用Interface并将其用作回调。请参阅Passing data between two Fragments in a VIewPager (Android) (NullPointerException)和Passing data beetwen fragments in viewpager
像这样更改PersonInformation
public class PersonInformation extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor informationCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private TextView personIDDisplay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.person_information, container, false);
return rootView;
}
public void init(string personId)
{
myDBHelper = new DatabaseHelper(getActivity());
informationCursor = myDBHelper.getInformationCursor(personId);
String[] fromColumns = {"firstname", "lastname", "homephone", "homeaddress"};
int[] toViews = {R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.information_layout, informationCursor, fromColumns, toViews, 0);
personIDDisplay = (TextView) rootView.findViewById(R.id.person_id_display);
personIDDisplay.setText("Person ID: " + personId);
ListView myListView = (ListView) rootView.findViewById(R.id.row_of_information);
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
}
替换
PersonInformation personInformation = new PersonInformation();
Bundle myBundle = new Bundle();
myBundle.putString("personID", personIDNumber);
personInformation.setArguments(myBundle);
getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();
带
String tag = "android:switcher:" + R.id.pager + ":" + "1";
PersonInformation f = (PersonInformation) getSupportFragmentManager().findFragmentByTag(tag);
f.init(personIDNumber);
<{1>} onItemClick
片段中的