在片段A 中,它有listView
。单击列表时,它应将数据传递给片段B 。
A.java
B fragment2 = new B();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
B.java
public class B extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.b, container, false);
Log.d("TAG","fragment2");
return view;
}
}
A.XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/some_text"
android:textSize="20sp" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</FrameLayout>
B.XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="false">
<AbsoluteLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="218dp"
android:layout_height="47dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Update Page"
android:id="@+id/textView114"
android:layout_x="12dp"
android:layout_y="20dp" />
</AbsoluteLayout>
</ScrollView>
这就是我的片段A的样子。
点击列表时。
这里有什么问题:?
答案 0 :(得分:0)
我认为在实例化EditText
字段时可能是您的问题,您最好在创建视图后声明您的项目,因此您必须覆盖方法onViewCreated
并在其中声明您的声明,然后将您的数据库工作在try-catch块中,以确保它不是任何问题的原因:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
name3 = (EditText) view.findViewById(R.id.editText9);
date3 = (EditText) view.findViewById(R.id.editText12);
Bundle bundle = this.getArguments();
date = bundle.getString("date1");
ID = bundle.getString("ID");
RetrievePage(date, ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.b, container, false);
return view;
}
public void RetrievePage(String date, String id) {
final String date2 = date;
final String id2 = id;
Log.d("m", id2);
name3.setText("Lim ");
date3.setText(date2);
try {
database = dbHelper.getWritableDatabase();
c = database
.rawQuery(
"SELECT i.Weather FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i._id WHERE i.Name = ? AND i._id= ? ",
new String[] { String.valueOf("Lim"),
String.valueOf(id2) }, null);
if (c != null) {
while (c.moveToNext()) {
Info I = new Info();
Force WF = new Force();
String Weather = c.getString(c
.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
}
}
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
关于片段重叠,您必须将每个片段声明为分离的片段,而主FragmentActivity
是保存片段的活动,在您的情况下,片段必须是仅包含ViewPager
的空活动或FrameLayout
包含片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ac_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9" />
</RelativeLayout>
因此,您必须MainActivity
包含两个片段FragmentA
和FragmentB
参考this回答。
答案 1 :(得分:0)
使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/app_name"
android:textSize="20sp" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</FrameLayout>