0

Hello I have a navigation drawer. In the main activity I have a login form

<EditText android:id="@+id/et_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/Login" />
<EditText android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="@string/Password"/>
<Button
            android:id="@+id/btn_valider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/colorbutton"
            android:padding="12dp"
            android:text="@string/Connexion"/>

If the password, login and the type are true, it opens the AdminAccount

MainActivity.java

if(d.equalsIgnoreCase(et_password.getText().toString())&& d3.equals("Admin"))
            {
                Intent intent = new Intent(MainActivity.this,AdminAccount.class);
                startActivity(intent);
            }

I want to display the login of this user in the textview login

fragment_account_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/text"
            android:text="welcome"></TextView>
<TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/text"
            android:text="login"></TextView>
    </FrameLayout>

On click in the first item, it opens the fragment AccountFragment.

AdminAccount.java

public class AdminAccount extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback {
........
if (id == R.id.nav_camera) {
            fm.beginTransaction().replace(R.id.content_frame, new AccountFragment()).commit();
        } else if (id == R.id.nav_produit) {
            fm.beginTransaction().replace(R.id.content_frame, new ProductFragment()).commit();
        }
}

The login should be also displayed in the edittext et_login

AccountFragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="1dp"
            android:paddingLeft="60dp"
            android:paddingRight="58dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Bienvenue"
                android:id="@+id/welcome" />
                <EditText android:id="@+id/et_login"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Login" />
<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/et_nom" />
        </LinearLayout>
    </FrameLayout>

AccountFrafment.java

public class AccountFragment extends Fragment {
    private TextView welcome;
private EditText et_login;
}

thanks in advance

Lara Fab
  • 33
  • 7

1 Answers1

0

First, pass the login text from First Activity to Second Activity. https://stackoverflow.com/a/20170125/4586742

Then, pass the login text from Second Activity to the Fragment. https://stackoverflow.com/a/12739968/4586742

Get the Login text at the Fragment(as described in the link above), and set it to TextView/ EditText.

Edit:

getText() is to get the typed String from EditText.

Intent intent = new Intent(this,AdminAccount.class);

intent.putExtra("login", yourEditText.getText().toString()); 

startActivity(intent); 

Edit 2: (from your comment)

Change this line fm.beginTransaction().replace(R.id.content_frame, new AccountFragment()).commit(); to fm.beginTransaction().replace(R.id.content_frame, fragobj).commit();

Community
  • 1
  • 1
Bob
  • 13,447
  • 7
  • 35
  • 45
  • I used theese lines Intent intent = new Intent(this,AdminAccount.class); intent.putExtra("login", getText()); startActivity(intent); knowing that String login = c.getString("Login"); but I got this error Error:(134, 42) error: method getText in class Context cannot be applied to given types; required: int found: no arguments – Lara Fab May 11 '16 at 11:00
  • i updated the answer. you have to get the text from editText. – Bob May 11 '16 at 11:07
  • thanks it works for the first fragment but I couldn't pass variables with fragmentmanager. I got NullException in the getArguments() View rootview = inflater.inflate(R.layout.account, container, false); String login = getArguments().getString("login"); et_login.setText(login); return rootview; – Lara Fab May 11 '16 at 12:24
  • FragmentManager fm = getSupportFragmentManager(); Bundle bundle = new Bundle(); bundle.putString("login", login); AccountFragment fragobj = new AccountFragment(); fragobj.setArguments(bundle); android.support.v4.app.FragmentManager sFm = getSupportFragmentManager(); int id = item.getItemId(); if (id == R.id.nav_camera) { fm.beginTransaction().replace(R.id.content_frame, new AccountFragment()).commit(); – Lara Fab May 11 '16 at 12:24
  • i updated the answer. you are creating a fragment instance and setting the bundle to it. Then again you are creating a new fragment instance which you use in fragment transaction. What you have to do is, to use the same fragment instance at both places. – Bob May 11 '16 at 13:00
  • thanks sir it works now but the et_nom is empty knowing that I used a php code extract it from the database et_login.setText(log); and protected String doInBackground(Void... v) { HashMap params = new HashMap<>(); params.put("log",login11); RequestHandler rh = new RequestHandler(); String res= rh.sendPostRequest(URL, params); return res; } – Lara Fab May 11 '16 at 17:33
  • private void showUser(String json){ try { JSONObject jsonObject = new JSONObject(json); JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY); JSONObject c = result.getJSONObject(0); String nom = c.getString("Nom"); et_nom.setText(nom); } catch (JSONException e) { e.printStackTrace(); } } – Lara Fab May 11 '16 at 17:34