edit spinner in one activity from another activity - android-activity

i created Activity5 for manage values on other activities...
in Activity3 i have a spinner...
Spinner spinner1;
ArrayList<String> SAlist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
ArrayList<String> SAList = intent.getStringArrayListExtra("StringArrayList");
spinner1=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,SAList);
spinner1.setAdapter(adp);
}
i think if i build new spinner (for showing it to user) in activity5 and edit it , then passing array list of this spinner to activity3, this is possible...
Activity5:
Spinner sp;
EditText et;
ArrayList<String> li;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
getActionBar().setDisplayHomeAsUpEnabled(true);
li=new ArrayList<String>();
li.add("first item");
sp=(Spinner) findViewById(R.id.spinner1);
Button butt=(Button) findViewById(R.id.button1);
Button butt1=(Button) findViewById(R.id.button2);
et=(EditText)findViewById(R.id.editText1);
add();
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
li.add(et.getText().toString());
et.setText(null);
add();
}
});
butt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity5.this, MainActivity3.class);
intent.putStringArrayListExtra("StringArrayList", li);
startActivity(intent);
}
});
}
private void add() {
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
sp.setAdapter(adp);
}
but when i launch app and open activity3 it shows "unfortunately app has stopped" !!!
help me plz!

when you open activity3 from main menu, on that event your getting SAList null,
try doing this,when SAList is null it will show empty spinner.
Intent intent = getIntent();
if(intent.getStringArrayListExtra("StringArrayList")!=null)
{
ArrayList<String> SAList = intent.getStringArrayListExtra("StringArrayList");
}else
{
ArrayList<String> SAList=new ArrayList<String>();
}

Related

Why is the beaconmanager just finding a beacon?

My problem is that the beacon manager just finds a beacon.
My code is below;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1234);
}
button = (Button) findViewById(R.id.b1);
//button onClick
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBeaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
mBeaconManager.bind(MainActivity.this);
}
});
}
#Override
public void onBeaconServiceConnect() {
ArrayList<Identifier> identifiers = new ArrayList<>();
identifiers.add(null);
Region region = new Region("all-beacons-region", identifiers);
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
mBeaconManager.addRangeNotifier(this);
}
//display beacon size
#Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) { Log.i(TAG,"" + beacons.size()); }
}
The result beacon.size 1. But there are three beacons.
What should I do to find more than one beacon? I'll be happy if you can help me.

Second activity does not load in login form

I created a simple app of login form but it did not go to second activity after login. There is no error in the code. Will you please help me, here is the code:
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter=5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.editText);
Password = (EditText) findViewById(R.id.editText2);
Info = (TextView)findViewById(R.id.textView);
Login = (Button) findViewById(R.id.btn);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
private void validate(String userName, String userPasswor) {
if ((userName == "admin") && (userPasswor == "1234")) {
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
} else {
counter--;
Info.setText("No of Attempts Remaining: " + String.valueOf(counter));
if (counter == 0) {
Login.setEnabled(false);
}
}
}
}
You write your code
if((userName =="admin") && (userPasswor=="1234"))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
change this code as
if((userName.equals("admin")) && (userPasswor.equals("1234")))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
also enter Second activity in Android.mainfeast file.

Get value from Android listview and put it in extra

In my code below I created a method which is: I would like to put a variable in putExtra that I can get it in the new activity. The problem is in the new activity gave me the first value(numtelephone) from first element in my list. I know that because of getItem(0), but how can I get the value( numtelephone ) for every element from the list?
public class ListClient extends Activity {
String myJSON,test;
TextView numt;
private static final String TAG_RESULTS="result";
private static final String TAG_NOMCLIENT = "nomclient";
private static String TAG_NUMTELEPHONE ="numtelephone";
private static final String TAG_DEPART ="depart";
private static final String TAG_DESTINATION ="destination";
private static final String TAG_NBRPERSONNE ="nbrpersonne";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
//json parsing etc...
ListAdapter adapter = new SimpleAdapter(
ListClient.this, personList, R.layout.list_item,
new String[]{TAG_NOMCLIENT,TAG_NUMTELEPHONE,TAG_DEPART,TAG_DEPART,TAG_NBRPERSONNE},
new int[]{R.id.name, R.id.numt, R.id.depart, R.id.destination, R.id.nbrpersonne}
);
list.setAdapter(adapter);
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// json dowload from web, not needed in this question
return "";
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
public void GoUpdate(View v ) {
Intent in = new Intent(getBaseContext(), Update.class);
in.putExtra("NUMTELEPHONE",((HashMap<String, String>)list.getAdapter().getItem(0)).get("numtelephone"));
}
startActivity(in);
}
}
There is simple and only one solution. Delete your xml android:click property of list and add this list#setOnItemClickListener in your onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
getData();
//this thing has been added
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
Intent in = new Intent(getBaseContext(), Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int)id)).get("numtelephone")); //TAG_NUMTELEPHONE
startActivity(in);
}
}
});
}
To move it from onCreate do:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_client);
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String, String>>();
getData();
list.setOnItemClickListener(new OnMyListItemClickListener());
}
Context context = this;
private class OnMyListItemClickListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
Intent in = new Intent(context, Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get("numtelephone")); //TAG_NUMTELEPHONE
startActivity(in);
}
}
}
Lesson learned - dont create sucha buttons - they are not aware in which item they are, so you cant extract the id - instead just use something like AlertDialog
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id!=-1){
AlertDialog.Builder al = new AlertDialog.Builder(context);
al.setMessage("Are you sure you want to do it?");
al.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent in = new Intent(context, Update.class);
in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get(TAG_NUMTELEPHONE));
startActivity(in);
}
});
al.setNegativeButton(android.R.string.no, null);
al.show();
}
}

android code for switching between activities

In an activity there are 2 buttons. 1st button should open one activity and the 2nd button should open another activity. The following code is what I wrote. Please help me.
Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
msg=(Button)findViewById(R.id.text);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.voice)
{
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_voice_msg);
}
else if(view.getId()==R.id.text)
{
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_text_msg);
}
}
Replace this;
`Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
voice.setOnClickListener(this);
msg=(Button)findViewById(R.id.text);
msg.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.voice)
{
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_voice_msg);
}
else if(view.getId()==R.id.text)
{
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_text_msg);
}
}`
By this:
Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
msg=(Button)findViewById(R.id.text);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.voice:
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
break;
case R.id.text:
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
break;
default:
break;
}
}
Also implements OnClickListener in your class
please try below code:
button1 = (Button)findViewById(R.id.Button1);
button1.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
intent = new Intent(CurrentActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
});
button2 = (Button)findViewById(R.id.Button2);
button2.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
intent = new Intent(CurrentActivity.this, ThirdActivity.class);
startActivity(intent);
finish();
}
});

return value to fragment from activity android

I have Fragment(F) which calls the activity(A). On press of button(B) in activity(A) the activity must return selected arraylist value back to fragment(F) and finish activity(A)... is this possible?
I know from Activity you can send data with intent as:
Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
Fragmentclass fragmentobj = new Fragmentclass();
fragmentobj.setArguments(bundle);
in Fragment onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("key");
return inflater.inflate(R.layout.fragment, container, false);
}
but this works for calling new fragment not working with same fragment.
My question: is it possible for called Activity to return value to caller fragment?
public class HomeFragment extends Fragment{
Button attachment;
String strtext = "check";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, null);
attachment = (Button) rootView
.findViewById(R.id.btn_activity);
Toast.makeText(getActivity(),
"Key= " + strtext,
Toast.LENGTH_LONG).show();
attachment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getActivity(), AndroidCustomGalleryActivity.class);
getActivity().startActivityForResult(myIntent,999);
}
});
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 999){
strtext = getArguments().getString("key");
Toast.makeText(getActivity(),
"Key= " + strtext,
Toast.LENGTH_LONG).show();
}
}
}
In Called Activity
Bundle bundle = new Bundle();
bundle.putString("key", "value");
HomeFragment fragmentobj = new HomeFragment();
fragmentobj.setArguments(bundle);
finish();
In your fragment(F) call the startActivityForResult() method to start the activity A, then override onActivityResult() method in the fragment.
So when the activity A finishes you can get get the result bundle in onActivityResult() method.
EDIT
In your fragment onClick() call the activity like this
Intent myIintent=new Intent(getActivity(), AndroidCustomGalleryActivity.class);
startActivityForResult(myIintent, 999);
And in your activity
Intent intent = getIntent();
Bundle bundle = new Bundle();
bundle.putString("key", "value");
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
Now you will get the result in onActivityResult() method of the fragment.
EDIT2
In your fragment onActivityResult() method you have to get the data like this...
Bundle extras = data.getExtras();
strtext = extras.getString("key");