0

In my file Reservation.java I have a function that loads LieuActivity. So this is the function

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Reservation extends AppCompatActivity implements View.OnClickListener {
    private Button end,deb,lieu,type,cherch;
    private TextView type_v ;
    private ImageView car;
    private View division;
    private ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reservation);
        end = (Button)findViewById( R.id.btn_fin );
        end.setOnClickListener( this);
        deb= (Button)findViewById( R.id.deb_btn);
        deb.setOnClickListener( this );
        lieu = (Button)findViewById( R.id.btn_local);
        lieu.setOnClickListener( this );
        type = (Button)findViewById( R.id.btn_type);
        type.setOnClickListener( this );
        cherch= (Button)findViewById( R.id.btn_cherch);
        cherch.setOnClickListener( this );
        division= (View)findViewById( R.id.div3);
        type_v= (TextView)findViewById( R.id.type);
        car= (ImageView) findViewById( R.id.pic_car);
        list=(ListView)findViewById(R.id.fin_list);

        ArrayAdapter<String> mAdapter= new ArrayAdapter<String>(Reservation.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.values));

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 3 )
                {
                    Intent i = new Intent(view.getContext(),Deb_FinActivity.class);
                    startActivityForResult(i,3);
                }
            }
        });

        list.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.deb_btn:
                loadDebActivity();
                break;
            case R.id.btn_fin:
                loadFin();
                break;
            case R.id.btn_local:
                loadLieuActivity();
                break;
            case R.id.btn_type:
                loadTypeActivity();
                break;
            case R.id.btn_cherch:
                loadSearch();
                break;
        }
    }

    private void loadDebActivity()
    {
        startActivity(new Intent( this,Deb_FinActivity.class ) );
        finish();
    }

    private void loadFin()
    {
        lieu.setVisibility( View.INVISIBLE );
        type.setVisibility(View.INVISIBLE);
        cherch.setVisibility(View.INVISIBLE);
        car.setVisibility(View.INVISIBLE);
        division.setVisibility(View.INVISIBLE);
        type_v.setVisibility(View.INVISIBLE);
        list.setVisibility(View.VISIBLE);
    }

    private void loadLieuActivity()
    {
        startActivity(new Intent( this,LieuActivity.class ) );

       lieu.setText(getIntent().getStringExtra("mytext"));
    }

    private void loadTypeActivity()
    {
        startActivity(new Intent( this,TypeActivity.class ) );
        finish();
    }
    private void loadSearch()
    {
    }
}

In my LieuActivity.java I have this code

public class LieuActivity extends AppCompatActivity {
    private EditText lieuu;
    private  TextView local;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lieu);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        local = (TextView) findViewById(R.id.position);
        local.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                EditText lieuu = (EditText)findViewById(R.id.local);
                String text =lieuu.getText().toString();

                Intent myIntent = new Intent(view.getContext(),Reservation.class);
                myIntent.putExtra("mytext",text);
                startActivity(myIntent);
            }
        });
    }
}

The problem is when I edit in my LieuActivity and go back to reservation it does not return the extra value until I click another time on the editText so I see the updated data for a second before re-going to LieuActivity and modify it another time .

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Maryam
  • 5
  • 5
  • post your Reservation.java code – sasikumar Aug 14 '18 at 10:03
  • I'm surprised your app doesn't crash in line `lieu.setText(getIntent().getStringExtra("mytext"));` in `loadLieuActivity()` method. How do you refer to a view that has not been created yet in another activity? –  Aug 14 '18 at 10:07
  • I have edited the question = – Maryam Aug 14 '18 at 10:09
  • So `lieu` is a button inside Reservation activity and after you finish the activity you set its text?!!! –  Aug 14 '18 at 10:12
  • I'm referring to `lieu` Button inside `Reservation` activity. Your code does not make sense. –  Aug 14 '18 at 10:14
  • lieu is a button that contains default data when I click on it , another activity starts and returns a new data in the text of this button – Maryam Aug 14 '18 at 10:15
  • In `loadLieuActivity()` you call `finish()` for the `Reservation` activity and close it. How do you set the `lieu` button's text since the activity is closed? –  Aug 14 '18 at 10:20
  • even when I removed it that returns the same result , after editing the data in my LieuActivity interface and return to Reservation interface the button has the previous text but when I click on it the edited text appears == – Maryam Aug 14 '18 at 10:24

3 Answers3

0

Do this in oncreate of Reservation

Bundle extras = getIntent().getExtras(); 
String string;

if (extras != null) {
    string = extras.getString("mytext");
    lieu.setText(string);
}
Mbuodile Obiosio
  • 1,463
  • 1
  • 15
  • 19
  • it's not a toast, it returns a text that should be written in the button of another activity , plz see the edited question – Maryam Aug 14 '18 at 10:18
  • My answer will actually help you fetch the string. You can set button text as button.setText(string); – Mbuodile Obiosio Aug 14 '18 at 10:33
  • I have done what you said but it returns the same result . after editing the data in my LieuActivity interface and return to Reservation interface the button has the previous text but when I click on it the edited text appears – Maryam Aug 14 '18 at 10:36
  • Send me a message. I'll love to have a look at your code and show you how to make your code more tidy. https://twitter.com/cazewonder – Mbuodile Obiosio Aug 14 '18 at 10:37
0

Put these lines in onCreate() of Reservation activity

String text = getIntent().getStringExtra("mytext");
if (text != null) {
    lieu.setText(text);
}

and make this change:

private void loadLieuActivity()
{
    startActivity(new Intent( this,LieuActivity.class ) );
    finish();
}
  • the same result :( – Maryam Aug 14 '18 at 10:38
  • when I exit LieuActivity it stays the same data but when i click on the button it takes the new one for few seconds – Maryam Aug 24 '18 at 13:17
  • yes it has solved 2 min ago :) i created a condition on the extra string if it is null it takes the default data else the data as you wrote in your proposition – Maryam Aug 24 '18 at 15:23
  • @Maryam if the answer helped you mark it as the right answer –  Aug 24 '18 at 15:39
0

Make changes like this in LieuActivity :-

public class LieuActivity extends AppCompatActivity {
    private EditText lieuu;
    private  TextView local;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lieu);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        local = (TextView) findViewById(R.id.position);
        local.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                EditText lieuu = (EditText)findViewById(R.id.local);
                String text =lieuu.getText().toString();

                Intent myIntent = new Intent(view.getContext(),Reservation.class);
                myIntent.putExtra("mytext",text);
                setResult(RESULT_OK, myIntent );
        });
    }
}

In Reservation put onActivityResult()

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Reservation extends AppCompatActivity implements View.OnClickListener {
    private Button end,deb,lieu,type,cherch;
    private TextView type_v ;
    private ImageView car;
    private View division;
    private ListView list;
            String mNewText="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reservation);
        end = (Button)findViewById( R.id.btn_fin );
        end.setOnClickListener( this);
        deb= (Button)findViewById( R.id.deb_btn);
        deb.setOnClickListener( this );
        lieu = (Button)findViewById( R.id.btn_local);
        lieu.setOnClickListener( this );
        type = (Button)findViewById( R.id.btn_type);
        type.setOnClickListener( this );
        cherch= (Button)findViewById( R.id.btn_cherch);
        cherch.setOnClickListener( this );
        division= (View)findViewById( R.id.div3);
        type_v= (TextView)findViewById( R.id.type);
        car= (ImageView) findViewById( R.id.pic_car);
        list=(ListView)findViewById(R.id.fin_list);

        ArrayAdapter<String> mAdapter= new ArrayAdapter<String>(Reservation.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.values));

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 3 )
                {
                    Intent i = new Intent(view.getContext(),Deb_FinActivity.class);
                    startActivityForResult(i,3);
                }
            }
        });

        list.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.deb_btn:
                loadDebActivity();
                break;
            case R.id.btn_fin:
                loadFin();
                break;
            case R.id.btn_local:
                loadLieuActivity();
                break;
            case R.id.btn_type:
                loadTypeActivity();
                break;
            case R.id.btn_cherch:
                loadSearch();
                break;
        }
    }

    private void loadDebActivity()
    {
        startActivity(new Intent( this,Deb_FinActivity.class ) );
        finish();
    }

    private void loadFin()
    {
        lieu.setVisibility( View.INVISIBLE );
        type.setVisibility(View.INVISIBLE);
        cherch.setVisibility(View.INVISIBLE);
        car.setVisibility(View.INVISIBLE);
        division.setVisibility(View.INVISIBLE);
        type_v.setVisibility(View.INVISIBLE);
        list.setVisibility(View.VISIBLE);
    }

    private void loadLieuActivity()
    {
        startActivity(new Intent( this,LieuActivity.class ) );

       lieu.setText(mNewText);
    }

    private void loadTypeActivity()
    {
        startActivity(new Intent( this,TypeActivity.class ) );
        finish();
    }
    private void loadSearch()
    {
    }


     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data)      {
         super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == QUEUE_MSG) {
             if (resultCode == RESULT_OK) {
                 Serializable myText= data.getSerializableExtra("mytext");
                 if (myText!= null)
                     mNewText= myText;
             }
         }
     }
}

Hope this may help you

InsaneCat
  • 2,115
  • 5
  • 21
  • 40