Get edittext value from fragment

I use fragments, I have edittext in the fragment, and I want to get the value in the main activity.

This is my fragment layout.

<?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"
    android:orientation="vertical"
    android:background="#878787" >

        <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="dfgdfgdf"
        android:textSize="20dp"
        android:layout_centerInParent="true"
        android:id="@+id/user_name"/>

    <EditText
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   />

    <Button 
        android:text="Gönder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getFromUser"
        android:layout_marginTop="40dp"
        />

</RelativeLayout>

I load a snippet using this function:

public void startChat(JsonObject user) {
    FrameLayout layout = (FrameLayout)findViewById(R.id.container);
    layout.setVisibility(View.VISIBLE); 
    Bundle bundle = new Bundle();
    bundle.putString("name", user.get("name").getAsString());
    sendTo=user.get("username").getAsString();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ConversationFragment conv = new ConversationFragment();
    conv.setArguments(bundle);
    fragmentTransaction.add(R.id.container, conv);
    fragmentTransaction.commit();
    viewPager.setVisibility(View.GONE);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayHomeAsUpEnabled(true);

}

And this is my fragment class

public class ConversationFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String name = getArguments().getString("name");
        View rootView = inflater.inflate(R.layout.fragment_conversation, container, false);
        TextView username=(TextView)rootView.findViewById(R.id.user_name);
        username.setText(name);
        return rootView;
    }
}

As you can see, when you click the button, the main activity launches the getFromUser function. I want to get the value of edittext in this function. How can i do this?

+4
source share
3 answers

It is always the same procedure for these things. You cannot access fragment views that way. You need a callback method.

Add this code to the conversation :

private OnGetFromUserClickListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnGetFromUserClickListener ) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnGetFromUserClickListener");
    }
}

public void getFromUser(View v) {
    if (mListener != null) {
        EditText edit = (EditText)findViewById(R.id.message);
        mListener.getFromUser(edit.getText().toString());
    }
}

public interface OnGetFromUserClickListener {
    void getFromUser(String message);
}

MainActivity . getFromUser() MainActivity :

public void getFromUser(String message) {
    sendMessage(message);
}

.

: , XML-onClick (. onClick , Activity): . , , - . XML (, get_from_user) onCreateView :

v.findViewById(R.id.get_from_user).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.get_from_user) {
            getFromUser(v);
        }
    }
});

.

+4

  • getActivity()

getActivity(). , . , , , .

class ParentActivity extends Activity
{
   public void callFromFragment()
   {
       // Your business logic
   }
}

class ChildFragment extends Fragment
{
    public void callingMethod()
    {
       // This is how you call method of Activity from Fragment.
       ((ParentActivity)getActivity()).callFromFragment();
    }
}

. . . .

public interface Mediator {
        public void callFromFragment();
    }

    class ParentActivity extends Activity implements Mediator
    {

        private void someMethod()
        {
            ChildFragment fragment=new ChildFragment();
            fragment.setMediator(this);
            // Fragmet Transaction lofi
        }
        @Override
        public void callFromFragment()
        {

        }
    }

    class ChildFragment extends Fragment
    {
        Mediator mediator;

        public Mediator getMediator() {
            return mediator;
        }

        public void setMediator(Mediator mediator) {
            this.mediator = mediator;
        }

        public void callingMethod()
        {
           // This is how you call method of Activity from Fragment.
            mediator.callFromFragment();
        }
    }
+2

I solved this problem.

public void getFromUser(View view) {        
    ConversationFragment fragment1 = (ConversationFragment)getSupportFragmentManager().findFragmentById(R.id.container);
    View frag=fragment1.getView();
    EditText editText1 =(EditText) frag.findViewById(R.id.message);
    String message=editText1.getText().toString();
    sendMessage(message);
}

Now I can get the value of edittext from the fragment.

0
source

All Articles