如何在其他类中使用if / else输出

时间:2017-02-28 13:24:18

标签: android

我在FlightAct活动中尝试使用departureCity值(它在FlightCityOnItemSelectedListener类中的onItemSelected方法中设置if / else)(值将使用URL来解析json)。

现在我怎样才能获得所有FlightAct.java的departureCity值?或其他活动? 因为我会在循环中使用很多变量,而不是方法吗?

package ir.safarbazha.safarbazha.Core.adapter;

import android.provider.Settings;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class FlightCityOnItemSelectedListener
        implements AdapterView.OnItemSelectedListener {

    String departureCity;

    public void onItemSelected(
            AdapterView<?> parent,
            View view,
            int pos,
            long id
    ) {
        String selectedCity = parent.getItemAtPosition(pos).toString();
        if (selectedCity.equals("آبادان")) {
            departureCity="ABD";

            Toast.makeText(parent.getContext(),
                    "آنچه انتخاب شد : " +
                            parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

        // TODO Auto-generated method stub
    }
}

=============================================== =================================

package ir.safarbazha.safarbazha.Acts;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ButtonBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import ir.safarbazha.safarbazha.Core.adapter.DrawerCustomListAdapter;
import ir.safarbazha.safarbazha.Core.adapter.FlightCityOnItemSelectedListener;
import ir.safarbazha.safarbazha.R;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public class FlightAct extends AppCompatActivity {

    Context currentActivity=FlightAct.this;
    ActionBar mainActionbar;
    ImageView mainToolbarMenu;
    ImageView mainToolbarHome;
    ImageView mainToolbarAbout;
    ImageView mainToolbarAccount;
    ImageView mainToolbarSearch;

    DrawerLayout mainDLayout;
    DrawerCustomListAdapter mainDCLAdapter;
    String[] mainDitems;
    List<String> mainDItems;
    ListView mainDLV;

    //Just For This Activity...
    Spinner departureCity , arrivalCity;
    TextView departureDate , arrivalDate , capacityCount;
    RadioButton radioButtonOneWayFlightType , radioButtonRoundTripFlightType;
    Button btnSubmitFlightForm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Localize...
        if (!Locale.getDefault().getLanguage().equals("fa")){
            String languageToLoad  = "fa";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            this.setContentView(R.layout.act_flight);
        }
        setContentView(R.layout.act_flight);

        //Set Custom ToolBar...
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mainActionbar=getSupportActionBar();
        final LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.toolbar, null);
        mainActionbar.setCustomView(v);
        mainActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        //Set Custom ToolBar Objects...
        //MainToolBar Menu Image Object...
        mainToolbarMenu=(ImageView)findViewById(R.id.nav_drawer_menu);
        mainToolbarMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mainDLayout.isDrawerOpen(GravityCompat.START))
                    mainDLayout.closeDrawer(GravityCompat.START);
                else mainDLayout.openDrawer(GravityCompat.START);
            }
        });
        //Main ToolBar Home Image Object...
        mainToolbarHome=(ImageView)findViewById(R.id.toolbar_home_icon);
        mainToolbarHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(currentActivity,HomeAct.class));
            }
        });
        //Main ToolBar About Image Object...
        mainToolbarAbout=(ImageView)findViewById(R.id.toolbar_about_icon);
        mainToolbarAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(currentActivity,AboutAct.class));
            }
        });
        //Main ToolBar Account Image Object...
        mainToolbarAccount=(ImageView)findViewById(R.id.toolbar_account_icon);
        mainToolbarAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(currentActivity,AccountAct.class));
            }
        });
        //Main ToolBar Search Image Object...
        mainToolbarSearch=(ImageView)findViewById(R.id.toolbar_search_icon);
        mainToolbarSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(currentActivity,SearchAct.class));
            }
        });

        //Set Custom Navigation Drawer...
        mainDLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        mainDLV=(ListView)findViewById(R.id.drawer_lv);
        mainDitems= getResources().getStringArray(R.array.main_titles);
        mainDItems=new ArrayList<String>(Arrays.asList(mainDitems));
        mainDCLAdapter=new DrawerCustomListAdapter(this,mainDItems);
        mainDLV.setAdapter(mainDCLAdapter);
        mainDLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = getResources().getStringArray(R.array.main_titles)[position];
                if (position==0)
                    startActivity(new Intent(currentActivity, HomeAct.class));
                else if (position == 1)
                    startActivity(new Intent(currentActivity,AboutAct.class));
                else if (position == 2)
                    startActivity(new Intent(currentActivity, PostAct.class));
                else if (position == 3)
                    Snackbar.make(view, "کاربر گرامی شما هم اکنون در *پرواز ها* برنامه سفر باز ها هستید!", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                else if (position == 4)
                    startActivity(new Intent(currentActivity, ProductAct.class));
                else if (position == 5)
                    startActivity(new Intent(currentActivity, AccountAct.class));
                else if (position == 6)
                    startActivity(new Intent(currentActivity, ContactAct.class));
            }
        });

        //Just For This Activity...
        addListenerOnButton();
        addListenerOnSpinnerItemSelection();

        //Selected Departure And Arrival Airports...
        departureCity=(Spinner)findViewById(R.id.departure_city);
        arrivalCity=(Spinner)findViewById(R.id.arrival_city);

        //Selected Flight Type (OneWay or RoundTrip)...
        radioButtonOneWayFlightType=(RadioButton)findViewById(R.id.btnRadioOneWayFlightType);
        radioButtonRoundTripFlightType=(RadioButton)findViewById(R.id.btnRadioRoundTripFlightType);

        radioButtonOneWayFlightType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arrivalDate.setVisibility(View.GONE);
            }
        });

        radioButtonRoundTripFlightType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arrivalDate.setVisibility(View.VISIBLE);
            }
        });

        //Selected Departure And Arrival Date...
        departureDate=(TextView)findViewById(R.id.departure_date);
        arrivalDate=(TextView)findViewById(R.id.arrival_date);

        //Selected Count Of Capacity...
        capacityCount=(TextView)findViewById(R.id.capacity_count);

    }
    //Set Custom Font For All Views in Core/app/AppController ...
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    //Just For This Activity...
    //DropDown City Choosing...
    public void addListenerOnSpinnerItemSelection() {
        departureCity = (Spinner) findViewById(R.id.departure_city);
        arrivalCity = (Spinner)findViewById(R.id.arrival_city);
        departureCity.setOnItemSelectedListener(new FlightCityOnItemSelectedListener());
        arrivalCity.setOnItemSelectedListener(new FlightCityOnItemSelectedListener());
    }
    // get the selected dropdown list value
    public void addListenerOnButton() {

        departureCity = (Spinner) findViewById(R.id.departure_city);
        arrivalCity = (Spinner) findViewById(R.id.arrival_city);
        btnSubmitFlightForm = (Button) findViewById(R.id.btnSubmitFlightActForm);

        btnSubmitFlightForm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(currentActivity,"OnClickListener : "
                        +"\nشهر مبدا : "+
                        String.valueOf(departureCity.getSelectedItem())
                        +
                        "\nشهر مقصد : "+
                        String.valueOf(arrivalCity.getSelectedItem())
                        ,
                        Toast.LENGTH_SHORT).show();
            }

        });
    }
}

2 个答案:

答案 0 :(得分:1)

将所需数据保存在类变量中。

您可以使用bundle将数据发送到可以访问该数据的其他活动。

将数据存储在用于启动第二个活动的意图中的代码示例

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putString("stringvalue", strValue);
b.putLong("longvalue", lngValue);
b.putDouble("doublevalue", doubValue);
b.putBoolean("boolvalue", boolValue);
intent.putExtras(b);
startActivity(intent);

在第二个活动中从包中获取数据的代码(此代码放在 onCreate()方法中):

    // get the Intent that started this Activity
   Intent in = getIntent();

   Bundle b = in.getExtras();
   String strValue= b.getString("stringvalue");

   long longValue = b.getLong("longvalue");
   double doubleValue= b.getDouble("doublevalue");
   boolean boolValue= b.getBoolean("boolvalue");

答案 1 :(得分:0)

您可以将其设置为静态,因此您可以在其他课程的任何位置使用它。

更改&#34;公共静态字符串离开城市&#34;而不是&#34; String departureCity&#34;;

然后你就可以这样称呼它:FlightCityOnItemSelectedListener.departureCity (没有实例化FlightCityOnItemSelectedListener)。