0

In fragment A, it has listView. When the list is clicked, it should pass the data to fragment 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>

This is how my Fragment A looked like.

enter image description here

When the list is clicked.

enter image description here

What's wrong here :?

John
  • 684
  • 11
  • 35

2 Answers2

0

I think may be your problem in instantiating the EditText fields, you'd better declare your items after the view is created, so you have to override the method onViewCreated and make your declarations in it, then put your database work in a try-catch block to make sure it isn't the reason of any problems:

@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();
    }

}

About fragments overlapping, you must declare each fragment as a separated fragment, and the main FragmentActivity is the activity that holds the fragments which in your case must be an empty activity with only the ViewPager or FrameLayout that contain fragments:

<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>

So, you must have MainActivity that contains two fragments FragmentA and FragmentB

for reference look at this answer.

Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0

in your a.xml use the following :

<?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>
Metwalli
  • 1,861
  • 1
  • 18
  • 27