how can i receive intent on 2 activities? - android

I want to receive intent on the two activity classes, but not sure how since startActivity can only start one intent to only one activity class
private void populateListView()
{
Log.d(TAG, "Populating listview");
Cursor data = dbManager.getTitle();
final ArrayList<String> listTitle = new ArrayList<>();
while (data.moveToNext())
{
listTitle.add(data.getString(1));
}
final ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listTitle);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String name = adapterView.getItemAtPosition(position).toString();
Cursor data = dbManager.getTitleID(name);
int titleID = -1;
while (data.moveToNext())
{
titleID = data.getInt(0);
}
if (titleID > -1)
{
Intent intent= new Intent(List_Title_Activity.this, MainActivity.class);
//Intent intent2= new Intent(List_Title_Activity.this, ViewListContents.class);
intent.putExtra("id", titleID);
intent.putExtra("title", name);
startActivity(edit);
}
else
{
Toast.makeText(List_Title_Activity.this, "No id associated with that name", Toast.LENGTH_SHORT).show();
}
}
});
MainActivity and ViewListContent:
Intent receiveIntent = getIntent();
selectedID = receiveIntent.getIntExtra("id",-1);

1 Activity is 1 page in android. That is, you can call/view 1 page/Activity at a time. If you want to share same data to multiple pages in use shared preference / Database. Or if you are using 1 activity and multiple fragments in it, use interface.

if you want to send data from one activity to 2nd activity you can use simple intent and pass data from first activity to second . But if you want to send the data first activity to second and get data from second activity to first activity i suggest you to use startActivityForResult. for more see here

Related

How to Pass the Data from Listview A to ListView B?

I have my Cart Activity which contains the items that the user wanted to purchase.
What I want is if I click the "CHECK OUT" button, all of the data in the cart will be sent to the next Activity having ListView B.
Here is a part of my CartCustomerAdpater.class where i set the textview fields
final Order data = items.get(position);
holder.cart_name.setText(data.getName());
holder.cart_price.setText("Php "+data.getPrice());
holder.cart_qty.setText(data.getQty());
Here is a part of my Cart.class where my CHECK OUT button is located.
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT SHOULD I DO HERE?
}
});
Suppose you declared your ListView:
ListView lv =(ListView) findViewById(R.id.lv);
For the case of a listView use setOnItemClickListener() , this is my sample code for sending data to the next Activity.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ModelClass jhild = (ModelClass) adapter.getItem(position);
Intent in = new Intent(getApplicationContext(), BuyApartmentsInformation.class);
in.putExtra(KEY_DISTRICT_NAME, jhild.getDistrict());
in.putExtra(KEY_FLOORS, jhild.getFloors());
in.putExtra(KEY_AREA, jhild.getArea());
in.putExtra(KEY_BUYLAND_CODE, jhild.getBuyno());
in.putExtra(KEY_PRICES, jhild.getPrice());
in.putExtra(KEY_INFORMATION, jhild.getInformation());
startActivity(in);
}
});
But my piece of Advice you have to update to RecyclerView
Hope it works well.
use intent
send
Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);
recieve
String data = getIntent().getExtras().getString("keyName");
1. You need to implement Serializable or Parcelable in your **Order.java** class so that you can pass the ArrayList of Order through Bundle or Intent .
public class Order implements Serializable{
}
2. Pass the list of items through intent and start new activity
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newActivity = new Intent(this , NewActivity.class);
newActivity.putExtra("orderList" , items);
startActivity(newActivity);
}
});
3. Get the list in NewActivity class
List<Order> itemsList = (List<Order>)getIntent.getSerializableExtra("orderList");
Hope it helps!

passing value from activity to adapter

I am devloping an app in which when user enter their mail id, i am storing that in a varible and passing to other activity.
For this i am using navigation drawer,in that from main activity i am passing the email to another activity that is internShips.In that i am passing the value by using its position:as shown below:
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if(position==3)
{
Intent intent = new Intent(MainActivity.this,internships.class) ;
intent.putExtra("email", email);
startActivity(intent);
}
In internships i am using recyclerview .In that if i click on any item it will start another activity.For clicking item i am giving clicking option in internshipAdapter as shown below:
holder.itemView.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), InternShipsDetails.class);
String email = i.getStringExtra("email");
i.putExtra("email" , email);
v.getContext().startActivity(i);
}
});
}
and in other activity i am getting the value using intent as shown below:
Intent intent = getIntent();
String email=intent.getStringExtra("email");
But the problem is, the value for email is showing null.So,where i am wrong,help here.
Change your code as this inside holder onClick. Because you are using intent for which you are changing your activity. So you need to use getIntent() to get email value.
Intent i = new Intent(v.getContext(), InternShipsDetails.class);
Intent emailIntent = ((Activity)v.getContext()).getIntent();
String email = emailIntent.getStringExtra("email");
i.putExtra("email" , email);
Log.d("###$email", email + "");
v.getContext().startActivity(i);
Instead of Intent i = new Intent(v.getContext(), InternShipsDetails.class)
use the context you should be passing to the adapter (The activity that the adapter is set in) and do like this:
Intent i = new Intent(context, InternShipsDetails.class);

Possible to pass intent extra from custom adapter to activity to another custom adapter?

So im trying to figure out if I can pass a intent extra from a custom adapter to an activity which then passes that intent extra to another activity?
Is something like that possible or is there a better way to do what I want to do?
Activity A:
Intent intent = new Intent(A.this, B.class);
intent.putExtra("someString", "string");
startActivity(intent):
Activity B:
onCreate(...) {
String myString = getIntent().getStringExtra("someString");
MyAdapter myAdapter = new MyAdapter(B.this, myString);
}
MyAdapter :
Context myContext;
String myString;
MyAdapter(Context context, String string) {
this.myContext = context;
this.myString = string
}
Now you have the String from activity A into your adapter :)
I assume, that you know how to send intent extras to an activity. And what you are looking for is a way to forward that same intent to another activity. You can do it like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(getIntent());
All the answers mentioned above would work. What I ended up using was an onItemClickListener on my listview and got the information that way. Instead of starting the intent in the custom adapter, i started the intent in the activity the custom adapter was in.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});

How to send data from one activity to another listview?

Hey guys I'm working on an app in android studio. I have a listview and when I make a selection i would like to add that selection to another listview in a different activity. What is the easiest/best way to do this? I've tried putExtra without any luck. Any examples or ideas would be great. Thank you guys.
Thanks for the examples everyone they've helped me understand a lot better the intent system. I've been trying the different examples everyone has posted and I've kind of gotten stuck. The goal is simply to have the items I select from the listview in the Walmart.java file to show up in the listview in GiftsSelected.java I have another place to open the activity so I don't need it to immediately open the new activity.
Here is my code:
This is Walmart.java
public class Walmart extends ActionBarActivity {
private String[]giftarray = {
"Apple" ,
"Bananas",
"Bed",
"Beef",
"Bottle",
"Bread",
"Broccoli",
"Carrots",
"Cat",
"Chicken",
"Chocolate",
"Computer",
"Cow",
"Crow",
"Dog",
"Dolphin",
"Dove",
"Drawer",
"Egg",
"Fish",
"Fork",
"Fridge",
"Giraffe",
};
Intent a = new Intent(Walmart.this,GiftsSelected.class);
private ListView giftListView;
private ArrayAdapter arrayAdapter;
ArrayList<String> list = new ArrayList<String>();
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){}
public boolean onContextItemSelected(MenuItem item){
return true;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walmart);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray );
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to registry";
list.add(item2);
a.putStringArrayListExtra("list",list);
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
This is my GiftsSelected.java code:
public class GiftsSelected extends ActionBarActivity {
private ListView giftListView;
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> list = new ArrayList<String>();
//This makes my app crash which makes me think I did this wrong...
list = getIntent().getStringArrayListExtra("list");
String[] giftarray = new String[list.size()];
list.toArray(giftarray);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifts_selected);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray);
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to your registry";
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
this is the list that i use
ArrayList<String> list = new ArrayList<String>();
add elements in it like this
list.add("something");
in first Activity
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putStringArrayListExtra("list",list);
startActivity(i);
in the second activity in the onCreate
list = getIntent().getStringArrayListExtra("list");
Let's say the first Activity is X and X holds a listview that updates another listview in Activity Y.
If X is tightly related to Y, that is to say Y launches X, gets data then immediately returns to Y, then you should use startActivityForResult from Y.
class ActivityY {
public static final int REQUEST_CODE = 2;
...
Intent i = new Intent(this, ActivityX.class);
startActivityForResult(i, REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String returndata = data.getStringExtra("rowdata");
//update your listView, do notifyDataSetChanged() etc;
}
}
}
}
class ActivityX {
// in listview onItemClickListener or elsewhere that listens to row click
Intent intent = new Intent();
returnIntent.putExtra("rowdata", rowdata); // whatever data you need to transfer
setResult(RESULT_OK,intent);
finish();
}
If X and Y are loosely related, that is to say X is not necessarily launched from Y, but goes to Y then you should just use the usual startActivity(intent).
if X and Y are completely independent, that is to say X is not necessarily launched from Y, may not go to Y or wanders other Activities before arriving in Y then you should cache the data. If the data size is small then the best way is to store it in Preferences. When you Y Activity starts, get the data, update your ListView, then if needed remove the cached data from Preferences.
a)You can make the listview item object parcelable and send it between activities via intent extra.
b)You can save the selected listview item in a global variable.(Not recommended)

Android finishing activity not working

Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.
I have tried using "finish();" after the activity starts but nothing seems to be working.
Activity that holds the EditText that launches the List activity:
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 1);
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
CPU.setText(product);
}
});
List view class
#Override
public void onCreate(Bundle OnsaveInstanceState) {
super.onCreate(OnsaveInstanceState);
setContentView(R.layout.activity_cpulist);
ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish();
}
});
You need to launch your activity in a way that it doesn't get added to back stack.
Here's how you do that: https://stackoverflow.com/a/12358563/375929
If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(),
ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(getIntent());
startActivity(i);
finish(); // finish here
}
and remove finish() from your EditText Activity
Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish(); // finish here
}

Categories

Resources