我的多余数据不会立即显示

时间:2018-08-14 09:57:06

标签: android android-intent

在我的文件Reservation.java中,我有一个加载LieuActivity的函数。 这就是函数

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()
    {
    }
}

在我的LieuActivity.java中,我有以下代码

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);
            }
        });
    }
}

问题是,当我在LieuActivity中进行编辑并返回到保留状态时,直到我再次单击editText时,它才会返回多余的值,因此我在第二次看到更新的数据之前重新进入LieuActivity并再次修改它。

3 个答案:

答案 0 :(得分:0)

在预订创建时执行

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

if (extras != null) {
    string = extras.getString("mytext");
    lieu.setText(string);
}

答案 1 :(得分:0)

将这些行放在onCreate()活动的Reservation

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

并进行更改:

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

答案 2 :(得分:0)

在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 );
        });
    }
}

在“保留”中放上ActivityResult()

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