在按下主活动的按钮时,将从某个活动获取的对象放入另一个活动的列表中

To put an object taken from an activity in a list from another activity, when pressing a button from the main activity

提问人:Cojo 提问时间:11/12/2023 更新时间:11/12/2023 访问量:14

问:

我想从活动“AdaugaInregistrare”中获取一个类型为“Restaurant”的对象,并在从主按钮 btnView 时将其放入活动“ListaInregistrari”中

MainActivity 代码:

package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Intent intent;
    public static final int REQUEST_CODE_ADD = 200;
    public static final int REQUEST_CODE_SHOW = 300;
    Button btnVizualizeaza;
    Intent intent2;
    Intent intent3;

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

        Button btnAdauga = findViewById(R.id.btnAdauga);
        btnAdauga.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(getApplicationContext(), AdaugaInregistrare.class);
                startActivityForResult(intent, RESULT_OK);
            }
        });
// În clasa MainActivity
        btnVizualizeaza = findViewById(R.id.btnVizualizeaza);
        btnVizualizeaza.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent3 = new Intent(getApplicationContext(), ListaInregistrari.class);
                startActivityForResult(intent3, REQUEST_CODE_SHOW);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD && resultCode == RESULT_OK && data != null) {
            Restaurant restaurant = (Restaurant) data.getSerializableExtra("restaurant");
            if(restaurant != null) {
                intent2 = new Intent();
                intent2.putExtra("resutarant2", restaurant);
                setResult(REQUEST_CODE_SHOW, intent2);

            }
        }
    }


}




AdaugaInregistrare: 
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AdaugaInregistrare extends AppCompatActivity {
    EditText etNume;
    EditText etNrAngajati;
    EditText etData;
    RadioGroup radioGroup;
    Spinner spinner;
    Button btnAdauaga;
    Intent resultIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adauga_inregistrare);
        etNume=findViewById(R.id.etNume);
        etNrAngajati=findViewById(R.id.etNrAngajati);
        etData = findViewById(R.id.etData);
        radioGroup = findViewById(R.id.radioGroup);
        spinner =findViewById(R.id.spinnerTipAfacere);
        String[] elemente = {"Srl1", "Srl2"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, elemente);
        spinner.setAdapter(adapter);
        btnAdauaga = findViewById(R.id.btnInregistrare);

        btnAdauaga.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                if (valid()) {
                    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
                    try {
                        Date dataInfintarii = new Date((sdf.parse(etData.getText().toString()).getTime()));
                        String numeFirma = etNume.getText().toString();
                        int nrAngajati = Integer.parseInt(etNrAngajati.getText().toString());
                        String program = spinner.getSelectedItem().toString();
                        String tipFirma = "deschis";
                        if (radioGroup.getCheckedRadioButtonId() == R.id.rbInchis) {
                            tipFirma = "inchis";
                        }
                        Restaurant restaurant = new Restaurant(numeFirma, nrAngajati, dataInfintarii, tipFirma, program);
                        resultIntent = new Intent();
                        resultIntent.putExtra("restaurant", restaurant);
                        setResult(RESULT_OK, resultIntent);
                        finish();

                        Log.i("RestaurantSmecher", restaurant.toString());

                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                }//end if
            } catch (Exception e){
                    e.printStackTrace();
                }
            }

            private boolean valid() {
                if(etNume.getText().toString().trim() == null || etNume.getText().toString().trim().isEmpty()){
                    Toast.makeText(getApplicationContext(), "Introdu nume", Toast.LENGTH_LONG).show();
                    return  false;
                }
            if(etData.getText() == null ){
                Toast.makeText(getApplicationContext(), "Introdu data", Toast.LENGTH_LONG).show();
                return  false;
            }
            if(etNrAngajati.getText().toString() == null){
                Toast.makeText(getApplicationContext(), "Introdu nr angajati", Toast.LENGTH_LONG).show();
                return  false;
            }


                return  true;
            }
        });
    }

}



ListaInregistrari: 

package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ListaInregistrari extends AppCompatActivity {
    private List<Restaurant> restaurantList;
    ListView listView;

    private ArrayAdapter<Restaurant> adapter;

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

        restaurantList = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, restaurantList);
       // restaurantList.add(new Restaurant("Test", 5, new Date(10/10/2020), "deschis", "Srl1"));
        listView = findViewById(R.id.lvLista);
        listView.setAdapter(adapter);



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_OK  && resultCode == MainActivity.REQUEST_CODE_SHOW  && data != null) {

            Restaurant restaurant = (Restaurant) data.getSerializableExtra("restaurant2");

            if (restaurant != null) {
                restaurantList.add(restaurant);
                adapter.notifyDataSetChanged();
           }
        }
    }
}

我想从活动“AdaugaInregistrare”中获取一个类型为“Restaurant”的对象,并在从主按钮 btnView 时将其放入活动“ListaInregistrari”中

java android-studio android-activity

评论


答: 暂无答案