вызвать другую java активность из класса фрагмента - PullRequest
0 голосов
/ 24 января 2020

Эй, у меня простой вопрос. у меня есть mobile_navigation, содержащая код для вызова класса фрагмента, и у меня есть класс фрагмента, но я хочу вызвать / передать другой класс java. например, у меня есть Inputdata. xml, что когда нажатие будет go, чтобы фрагментировать класс, нет, я хочу, когда я коснусь ввода xml, он мгновенно go будет переадресован в другой java класс, не застрявший в классе фрагмента

вот пример кода для mobile_navigation

<fragment
        android:id="@+id/nav_pantuan"
        android:name="com.joshua.r0th.crud2.ui.gallery.GalleryFragment"
        android:label="@string/pantauan"
        tools:layout="@layout/fragment_pantauan" />

, а вот код GalleryFragment

public class GalleryFragment extends Fragment {

    private GalleryViewModel GalleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        GalleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_pantauan, container, false);
        final TextView textView = root.findViewById(R.id.text_gallery);
        GalleryViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }
}

, и это еще один класс java, который я хочу достичь

public class pantauan extends AppCompatActivity {

    database1 myDb;

    EditText editNomorRumah,editJentikDalam,editJentikLuar;

    Button btnAddData;

    Button btnViewAll;





    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_pantauan);

        myDb = new database1(this);

        editNomorRumah = (EditText)findViewById(R.id.nomorrmh);

        editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);

        editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);


        btnAddData = (Button)findViewById(R.id.tambahdata);

        btnViewAll = (Button)findViewById(R.id.lihatdata);



        AddData();

        viewAll();



    }



    //fungsi tambah

    public void AddData() {

        btnAddData.setOnClickListener(

                new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {

                        boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),

                                editJentikDalam.getText().toString(),

                                editJentikLuar.getText().toString() );

                        if(isInserted == true)

                            Toast.makeText(pantauan.this,"Data Iserted",Toast.LENGTH_LONG).show();

                        else

                            Toast.makeText(pantauan.this,"Data Not Iserted",Toast.LENGTH_LONG).show();

                    }

                }

        );

    }



    //fungsi menampilkan data

    public void viewAll() {

        btnViewAll.setOnClickListener(

                new View.OnClickListener(){

                    @Override

                    public void onClick(View v) {

                        Cursor res = myDb.getAllData();

                        if(res.getCount() == 0) {

                            // show message

                            showMessage("Error","Noting Found");

                            return;

                        }



                        StringBuffer buffer = new StringBuffer();

                        while (res.moveToNext() ) {

                            buffer.append("NomorRumah :"+ res.getString(0)+"\n");

                            buffer.append("JentikDalam :"+ res.getString(1)+"\n");

                            buffer.append("JentikLuar :"+ res.getString(2)+"\n");



                        }



                        // show all data

                        showMessage("Data",buffer.toString());

                    }

                }

        );

    }



    //membuat alert dialog

    public void showMessage(String title, String Message){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setCancelable(true);

        builder.setTitle(title);

        builder.setMessage(Message);

        builder.show();

    }

}

и вот входные данные. xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/gradient"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="70dp"
        android:background="@drawable/gradient"
        android:elevation="4dp"
        android:orientation="vertical"
        android:padding="20dp">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="30dp">

            <TextView

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Nomor Rumah" />

            <EditText
                android:id="@+id/nomorrmh"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/roundtext" />
            <TextView

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Jentik Di Dalam Rumah" />
            <EditText
                android:id="@+id/jentikdirumah"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/roundtext" />
            <TextView

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Jentik Di Luar Rumah" />

            <EditText
                android:id="@+id/jentikdiluarrumah"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/roundtext"
                android:layout_marginBottom="10dp"/>
           <LinearLayout
               android:layout_width="wrap_content"
               android:orientation="horizontal"
               android:layout_height="match_parent"
               android:layout_gravity="center">

            <Button

                android:id="@+id/tambahdata"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:background="#d67601"
                android:text="Tambah Data"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />

            <Button
                android:id="@+id/lihatdata"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:background="#d67601"
                android:text="contoh lihat data"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
           </LinearLayout>
        </LinearLayout>

    </RelativeLayout>


    <TextView
        android:id="@+id/textviewadd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:gravity="center_horizontal"
        android:text="Input Data"
        android:textColor="#fff"
        android:textSize="26sp"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:id="@+id/text_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

Моя цель - передать фрагмент в класс java, который я хочу, потому что я не знаю, как его поместить входной класс для фрагмента всегда получает , не может быть приведен к androidx.fragment.app.Fragment , когда я заменяю GalleryFragment на pantauan. java code. спасибо

...