如何将不同活动中的复选框传递给android中的单个活动?

时间:2017-04-14 11:45:12

标签: android android-intent

我正在创建一个Android应用程序,其中有不同的城市在不同的活动。 例如,dalhousie有一项活动,西姆拉有另一项活动。 在每个活动中,有一个像复选框一样的心形,就像在Instagram中一样。如果我们勾选复选框,那么该城市将出现在另一个活动中,例如ProfileActivity.java,其中包含我已检查过的所有最喜欢城市的列表。我不知道如何将不同活动的意图转化为单一活动。

这是我的DalhousieActivity.java代码

package com.example.srushtee.dummy;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import me.relex.circleindicator.CircleIndicator;

public class DalhousieActivity extends AppCompatActivity {

private ViewPager viewPager;
private CustomSwipeAdapter adapter;
private static int currentpage = 0;
private int[] img;
private CheckBox favouriteBtn;
private TextView textview;
boolean flag=false;
private Button continueBooking;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dalhousie);
    boolean isChecked = getBooleanFromPreferences("isChecked");
    img = new int[]{R.drawable.dalhousie, R.drawable.dalhousie3, R.drawable.dalhousie2, R.drawable.dalhousie1};
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    adapter = new CustomSwipeAdapter(DalhousieActivity.this, img);
    viewPager.setAdapter(adapter);
    CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(viewPager);

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            currentpage = 1;

        }

        @Override
        public void onPageScrollStateChanged(int i) {
            if (i == viewPager.SCROLL_STATE_IDLE) {
                int pagecount = img.length;
                if (currentpage == 0) {
                    viewPager.setCurrentItem(pagecount - 1, false);
                } else if (currentpage == pagecount - 1) {
                    viewPager.setCurrentItem(0, false);
                }

            }

        }
    });

    favouriteBtn = (CheckBox) findViewById(R.id.buttonfav);
    Log.i("start", "" + isChecked);
    favouriteBtn.setChecked(isChecked);
    favouriteBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton view, boolean isChecked) {
            Log.i("boolean", "" + isChecked);
            DalhousieActivity.this.putBooleanInPreferences(isChecked, "isChecked");
            flag=true;
            Intent i=new Intent(getApplicationContext(),ProfileActivity.class);
            i.putExtra("Dalhousie",flag);


        }
    });
   continueBooking=(Button) findViewById(R.id.continue_booking);
    continueBooking.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent i=new Intent(DalhousieActivity.this,ConfirmationActivity.class);
                startActivity(i);
        }
    });


}





public void putBooleanInPreferences(boolean isChecked,String key){
    SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, isChecked);
    editor.commit();
}
public boolean getBooleanFromPreferences(String key){
    SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
    Boolean isChecked = sharedPreferences.getBoolean(key, false);
    return isChecked;
}
}

这是我的ProfileActivity.class代码

package com.example.srushtee.dummy;

import android.content.Intent;
import android.os.Handler;
import android.os.Process;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

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

public class ProfileActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

private boolean backPressedToExitOnce;
private boolean doubleBackToExitPressedOnce;
 private NavigationView navigationView;
private FirebaseAuth firebaseAuth;
private Button upload_btn;
boolean flag=false;
ListView listView;
String favName;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_profile);
    firebaseAuth=FirebaseAuth.getInstance();
    if(firebaseAuth==null)
    {
        finish();
        startActivity(new Intent(this,MainActivity.class));
    }
    FirebaseUser user=firebaseAuth.getCurrentUser();



   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      navigationView=(NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new 
  NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            if(item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            mDrawerLayout.closeDrawers();
            switch(item.getItemId())
            {
                case R.id.nav_account:
                    Toast.makeText(ProfileActivity.this,"called account",Toast.LENGTH_SHORT).show();
                    //startActivity(new Intent(ProfileActivity.this,Account.class));
                    return true;
                case R.id.nav_LogOut:
                    signout();
                    return true;
                case R.id.nav_cities:
                    startActivity(new Intent(ProfileActivity.this,CitiesActivity.class));
                    return true;


                default:
                    Toast.makeText(ProfileActivity.this,"default",Toast.LENGTH_SHORT).show();
                    return true;
            }
        }
    });

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open, R.string.close){

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }
    };
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    });



    //List<list_item> data = new ArrayList<>();
   /* for (int j = 0; j < 6; j++) {
        list_item item = new list_item();
        item.setImageResId(place_image[j]);
        item.setTitle(place_name[j]);
        item.setId(place_name[j]);
        item.setStatus(place_desc[j]);
        data.add(item);
    }

    list_data = (ArrayList) data;

    //data stored and retrieved until now

    rec_view = (RecyclerView) findViewById(R.id.places_recyler_view);
    rec_view.setLayoutManager(new LinearLayoutManager(this));

    adapter = new derp_adapter(data, this);
    rec_view.setAdapter(adapter);

    adapter.setItemClickCallback(this);*/

    upload_btn=(Button) findViewById(R.id.upload_photo_btn);
    upload_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(ProfileActivity.this,UploadPhotoActivity.class));
        }
    });








}

private void signout() {
    if(firebaseAuth!=null)
    {
        firebaseAuth.signOut();
    }
    finish();
    startActivity(new Intent(this,MainActivity.class));

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        moveTaskToBack(true);
        Process.killProcess(Process.myPid());
        System.exit(1);

    }
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press again to Exit", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 1000);

}
}

0 个答案:

没有答案