Tom
Tom

Reputation: 151

Java activity calling a fragment

First le me start by stating I am fairly new to Java and Android Studio, so please bear with me. my background has been in Visual Basic going way back.

I have an activity that tries to verify that what was entered is a valid Item Number by UPC entered. When only one match all is good. My problem starts when the UPC entered brings back more than one matching item. What I am attempting to do (First time using fragments) is Replace part of the screen with a recycler view showing the matching items and descriptions.

When comes backs with multiple items I call an event called setupMulitpleItems with the following code:

public void setupMultiItems(String mScan){
Timber.d("Multiple matching Items");

Bundle bundle = new Bundle();
bundle.putString("valuesArray",mScan);
SelectItemFragment fragobj = new SelectItemFragment();
fragobj.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction  = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.include2,  fragobj);


fragmentTransaction.commit();

Not sure how many parts of this code will actually work but I am getting underline on the replace line.

     "'replace(int, android.app.Fragment)' in 'android.app.FragmentTransaction' cannot be applied to '(int, com.procatdt.stockright.activities.SelectItemFragment)'" 
meesage.

I am using the following imports for Fragments.

 import android.app.FragmentTransaction;
 import android.app.Fragment;
 import android.app.FragmentManager;

Figured I would also include the XML for the activity that is currently running when I want to use Fragment.

         <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundGray"
    tools:layout="@layout/fragment_select_item"
    tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

       >

        <include
            android:id="@+id/include2"
            layout="@layout/frm1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="left" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingLeft="5dp"
        android:orientation="vertical">

        <include
            android:id="@+id/include"
            layout="@layout/layout_numpad5"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="220dp"
            android:layout_weight="1"
            android:layout_gravity="right"
           />

    </LinearLayout>

</RelativeLayout>

I am trying to replace include2 with Fragment. Hope this enough info but if anyone needs to see more let me know and I ill attach code.

Upvotes: 1

Views: 53

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

You just need to pass the object of your fragment to replace method, there is no need to pass the class declaration and type of class

so just remove

// this is enough
fragmentTransaction.replace(R.id.include2,  fragobj);

// remove this
//fragmentTransaction.replace(R.id.include2,android.app.Fragment  SelectItemFragment);

Upvotes: 3

Related Questions