Reputation: 568
I have created source and destination as a two edit-text in which i am placing the source and destination address from another activity but the first or second value is removed when coming back for placing second value. When first value is placed into first edit-text and going to place another value from another activity the first value is removed.
I have tried the Sharedpreferences but it is not working well any idea for this.
Here is my code.
Intent intent = getIntent();
String source = intent.getStringExtra("place");
search.setText(source);
Intent intent1 = getIntent();
String dest = intent1.getStringExtra("my1dest");
desti.setText(dest);
swap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1 = search.getText().toString();
String s2 = desti.getText().toString();
if (!s1.isEmpty() && !s2.isEmpty()) {
desti.setText(s1);
search.setText(s2);// this for swap the values source to destination viceversa
}
}
});
and from this adpter class i am passing the values
Intent intent=new Intent(context,MainActivity.class);
intent.putExtra("place",searchlist.getPlaces());
intent.putExtra("dest",searchlist.getPlaces());
intent.putExtra("lat",searchlist.getLat());
intent.putExtra("lang",searchlist.getLang());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
please give idea for this concept thank you..
Upvotes: 1
Views: 81
Reputation: 568
Here is the working code
Edittext source,destination;
swap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1 = search.getText().toString();
String s2 = desti.getText().toString();
if (s1.contains(s1) && s2.contains(s2)) {
desti.setText(s1);
search.setText(s2);
} else {
desti.setText(s2);
search.setText(s1);
}
}
});//for setting values from one edittext to another
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Added.class);
startActivityForResult(intent,1);
}
});
desti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Destinationsearch.class);
startActivityForResult(intent,2);
}
});// here i have started Activity For result to my Adapter class
getting values from Adapter class
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null)
{
String place=data.getStringExtra("place");
search.setText(place);
}
if (requestCode==2 && resultCode==RESULT_OK && data!=null)
{
String dest=data.getStringExtra("my1dest");
desti.setText(dest);
}
}
and this is my Adpter class
@Override
public void onBindViewHolder(@NonNull SearchedAdapter.Myholder holder, int position) {
Searchlist searchlist = list.get(position);
holder.textView.setText(searchlist.getPlaces());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("place", searchlist.getPlaces());
intent.putExtra("my1dest", searchlist.getPlaces());
intent.putExtra("lat", searchlist.getLat());
intent.putExtra("lang", searchlist.getLang());
((Activity) context).setResult(Activity.RESULT_OK, intent);
((Activity) context).finish();
}
});
}
That set (:
Upvotes: 0
Reputation: 1363
If you are trying to save values that are passed from two different activities than you should have a mechanism to save the first value and then should get other value from another activity.
The issue is that you are replacing the values with the same intent (intent from same activity):
Intent intent = getIntent(); //if this is intent from your MainActivity
String source = intent.getStringExtra("place"); //you will get this value
search.setText(source);
Intent intent1 = getIntent(); //and this as well is intent from same activity (MainActivity)
String dest = intent1.getStringExtra("my1dest"); //you will not get this value as this is not passed from MainActivity
desti.setText(dest);
The solution could be saving one value to SharedPreference or any other mean(db or network) and get other value from other intent: you should check if the value is null then don't replace it. Like :
Intent intent = getIntent(); //if this is intent from your MainActivity
String source = intent.getStringExtra("place"); //you will get this value
if(source.isNotEmpty){
//save to preferences and set text
search.setText(source);
}
Intent intent1 = getIntent(); //if this is intent from your otherActivity
String dest = intent1.getStringExtra("my1dest");
if(dest.isNotEmpty){
//save to preferences and set text
desti.setText(dest);
}
Upvotes: 1