在另一个活动中创建的对象在主活动中不可见

时间:2017-07-03 13:10:56

标签: java android listview android-activity

我的代码中有两个基本类:机场和飞行(出发和到达)。机场包括到达航班对象列表和出发航班对象列表。 在我的主要活动中,我保存了一个机场对象列表,并在列表视图中表示它们。通过触摸列表视图对象,我启动了一个新活动,用于显示该机场对象(ActivityArrivalFlights)的所有到达航班,其中我将机场对象保存为字段。在此活动中,有一项功能可以将到达航班添加到同一对象。我也在列表视图中表示航班,并通过阵列适配器添加航班。

问题是这样的:当飞行物体被创建并添加到机场时,只要活动存活,它就在那里。当ActivityArrivalFlights被销毁并且焦点在主要活动上时,之前添加的航班对象不在该机场内。

这是两个活动的代码。谁能告诉我,如果我做错了什么?

public class MainActivity extends AppCompatActivity {

    private static final int FILL_AIRPORT_REQUEST = 1;
    private static final int EDIT_AIRPORT_REQUEST = 2;

    static final String EDIT_AIRPORT_OBJECT_KEY = "editable";
    static final String EDIT_AIRPORT_POSITION_KEY = "position";
    static final String SELECTED_AIRPORT_KEY = "selectedAirport";
    static final String ALL_FLIGHTS_LIST_KEY = "allFlights";

    private static final String AIRPORTS_STATE = "airportsState";

    private Button btnAddAirport;
    private ListView lvAirports;

    private ArrayList<Airport> airports;
    private ArrayAdapter<Airport> airportArrayAdapter;

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

        btnAddAirport = (Button) findViewById(R.id.btnAddAirport);
        lvAirports = (ListView) findViewById(R.id.lwAirports);
        btnAddAirport.setOnClickListener(this::onButtonAddAirportClick);
        lvAirports.setOnItemClickListener(this::onListViewItemClicked);


        if (savedInstanceState != null)
            airports = savedInstanceState.getParcelableArrayList(AIRPORTS_STATE);
        else
            airports = new ArrayList<>();

        airportArrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, airports);
        lvAirports.setAdapter(airportArrayAdapter);
        lvAirports.invalidateViews();
        registerForContextMenu(lvAirports);
        if(savedInstanceState == null)
            addCodeForTesting();
    }

    private void addCodeForTesting() {
         /* TEMPORARY CODE FOR TESTING*/
        Airport munich = new Airport("MunichAirport", "Munich", "MU", 1234);
        Airport skopje = new Airport("Aleksandar Veliki", "Skopje", "SK", 4321);
        skopje.addArrivalFlight(munich, new Date(), new Date());
        airportArrayAdapter.add(skopje);
        airportArrayAdapter.add(munich);
    }

    private void onButtonAddAirportClick(View view){
        startActivityForResult(new Intent(this, ActivityAddAirport.class), FILL_AIRPORT_REQUEST);
    }

    private void onListViewItemClicked(AdapterView parent, View view, int position, long id){
        Airport airport = airports.get(position);
        showFlightsActivity(airport);
    }

    private void showFlightsActivity(Airport airport) {
        Intent intent = new Intent(this, ActivityArrivalFlights.class);
        ArrayList<Airport> filteredList = filterAirports(airports, airport);
        intent.putExtra(SELECTED_AIRPORT_KEY, airport);
        intent.putParcelableArrayListExtra(ALL_FLIGHTS_LIST_KEY, filteredList);
        startActivity(intent);
    }

    private static ArrayList<Airport> filterAirports(List<Airport> original, Airport except){
        ArrayList<Airport> filtered = new ArrayList<>();
        for (Airport airport : original) {
            if(!airport.equals(except))
                filtered.add(airport);
        }
        return filtered;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == FILL_AIRPORT_REQUEST){
            if(resultCode == RESULT_OK){
                Airport airport = data.getParcelableExtra(ActivityAddAirport.AIRPORT_OBJECT_KEY);
                airportArrayAdapter.add(airport);
            }
        }
        else if(requestCode == EDIT_AIRPORT_REQUEST){
            if(resultCode == RESULT_OK){
                Airport editedAirport = data.getParcelableExtra(ActivityAddAirport.AIRPORT_OBJECT_KEY);
                int airportPosition = data.getIntExtra(ActivityAddAirport.AIRPORT_POSITION, 0);
                airports.remove(airportPosition);
                airports.add(airportPosition, editedAirport);
                airportArrayAdapter.notifyDataSetChanged();
                lvAirports.invalidateViews();
                Toast.makeText(this, "Airport changed", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(AIRPORTS_STATE, airports);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.main_menu_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.context_edit) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            editListViewItem(info.position);
            return true;
        }
        else if (item.getItemId() == R.id.context_delete){
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            deleteListViewItem(info.position);
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void editListViewItem(int position) {
        Intent editIntent = new Intent(this, ActivityAddAirport.class);
        Airport changingAirport = (Airport) lvAirports.getItemAtPosition(position);
        editIntent.putExtra(EDIT_AIRPORT_OBJECT_KEY, changingAirport);
        editIntent.putExtra(EDIT_AIRPORT_POSITION_KEY, position);
        startActivityForResult(editIntent, EDIT_AIRPORT_REQUEST);
    }

    private void deleteListViewItem(int position){
        airportArrayAdapter.remove(airports.get(position));
    }
}
public class ActivityArrivalFlights extends AppCompatActivity {

    private static final int FILL_ARRIVAL_FLIGHT_REQUEST = 1;

    static final String AIRPORT_THIS_KEY = "thisAirport";
    static final String ALL_AIRPORTS_EXCEPT_THIS_KEY = "allExceptThis";

    private Airport airport;
    private ArrayList<Airport> allAirportsExceptThis;

    private ArrayAdapter<ArrivalFlight> lvArrivalsAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arrival_flights);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarAF);
        setSupportActionBar(toolbar);

        FloatingActionButton fabAddArrival = (FloatingActionButton) findViewById(R.id.fabAddArrival);
        fabAddArrival.setOnClickListener(this::onFabAddArrivalClick);

        Intent intent = getIntent();
        airport = intent.getParcelableExtra(MainActivity.SELECTED_AIRPORT_KEY);
        allAirportsExceptThis = intent.getParcelableArrayListExtra(MainActivity.ALL_FLIGHTS_LIST_KEY);

        setUpListView();
    }

    private void setUpListView(){
        ListView lvArrivals = (ListView) findViewById(R.id.lvArrivals);
        lvArrivalsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, airport.getArrivals());
        lvArrivals.setAdapter(lvArrivalsAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_choose_flights, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem arrivals = menu.findItem(R.id.arrivals);
        MenuItem departures = menu.findItem(R.id.departures);
        if (arrivals.isEnabled())
            arrivals.setEnabled(false);
        if (!departures.isEnabled())
            departures.setEnabled(true);
        return super.onPrepareOptionsMenu(menu);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.departures){
            Intent intent = new Intent(this, ActivityDepartureFlights.class);
            intent.putExtra(MainActivity.SELECTED_AIRPORT_KEY, airport);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onFabAddArrivalClick(View view){
        Intent intent = new Intent(this, ActivityAddArrivalFlight.class);
        intent.putExtra(AIRPORT_THIS_KEY, airport);
        intent.putParcelableArrayListExtra(ALL_AIRPORTS_EXCEPT_THIS_KEY, allAirportsExceptThis);
        startActivityForResult(intent, FILL_ARRIVAL_FLIGHT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == FILL_ARRIVAL_FLIGHT_REQUEST){
            if(resultCode == RESULT_OK){
               ArrivalFlight flight = data.getParcelableExtra(ActivityAddArrivalFlight.FILLED_FLIGHT_ENTRY);
               lvArrivalsAdapter.add(flight);
               Toast.makeText(this, "Flight added!\n" + airport.getArrivals().size(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为没有理由在ActivityArrivalFlights的销毁活动中使用新的航班更新MainActivity。

您没有将任何结果返回给MainActivity(来自ActivityArrivalFlights) 为了将数据返回到MainActivity,在您返回已添加的航班的情况下,您需要执行以下操作:

在MainActivity中:

final int NEW_FLIGHT_RESULT = 5; // Just an identifier number / what ever you want;

// Start ActivityArrivalFlights for result (which you are not doing):

private void showFlightsActivity(Airport airport) {
    Intent intent = new Intent(this, ActivityArrivalFlights.class);
    ArrayList<Airport> filteredList = filterAirports(airports, airport);
    intent.putExtra(SELECTED_AIRPORT_KEY, airport);
    intent.putParcelableArrayListExtra(ALL_FLIGHTS_LIST_KEY, filteredList);
    startActivityForResult(intent, NEW_FLIGHT_RESULT);  // Here start for result
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == NEW_FLIGHT_RESULT){
        if(resultCode == RESULT_OK) {
            Flight f = data.getParcelableExtra("NEW_FILGHT_DATA");
            // Here add the new flight to the airport object you are maintaining !!!
        }
     }

来自ActivityArrivalFlights:

@Override
public void onBackPressed() {

    // You can wrap this with: If(newFlightsAdded()) { ... }:
    Intent output = new Intent();
    output.putExtra("NEW_FILGHT_DATA", newFlight);
    setResult(RESULT_OK, output);
    finish();
}

这就是我猜你错过的整个概念,我的代码很快而且很脏,所以要清理它并按照你的意愿使用。

注意!!! :使用意图在活动之间传递对象(intent.putExtra(...)将传递对象的副本而不是同一个对象)。