I have two activities. One has a a form with few EditTexts. The values in these inputs should come from a master list and so, on focus of the EditText, I am taking the control to the another activity which uses 'android.support.v7.widget.SearchViewwith aListView`
OnFocus of a EditText I invoke the SearchActivity with following code:
ETVehicleRegnLoc.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));
}
}
});
Once the value is selected I am sending the value back with the following code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(SearchActivity.this , VehicleActivity.class);
Bundle bundle = new Bundle();
bundle.putString("SelectedVal", dataArray.get(arg2).toString());
intent.putExtras(bundle);
startActivity(intent);
// Toast.makeText(SearchActivity.this, dataArray.get(arg2).toString(), Toast.LENGTH_SHORT).show();
}
});
This works fine. But because I am calling the VehicleActivity through Intent, its clearing the entire form of the values which are already entered.
But when I press the "back" button on the mobile, the values persist. But the purpose of going to the SearchActivity is not met :)
I think I have to simulate the back button press. how can i do it??
Use
startActivityForResult(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"),2);
instead of
startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));
And in onItemClickListener Do
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();
And get the data in OnActivityForResult of your VehicleActivity
Related
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!
I have created a list view something like this: http://imgur.com/a/bhWhR in activity1
Onitemselect i want to launch another acitivity which shows description about listitem1 like below
http://imgur.com/a/agGma
but did not find any helpful material to create and activity with same layout to get different content like this one ::
I did explore for quite some time and I could not find any solution that helped me .
Use OnItemClickListener and use intent to open other activities on item clicks
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 1) {
Intent intent = new Intent(MainActivity.this, Lu1Activity.class);
startActivity(intent);
} else if (i == 2) {
Intent intent = new Intent(MainActivity.this, Lu2Activity.class);
startActivity(intent);
} else if (i == 3) {
Intent intent = new Intent(MainActivity.this, Lu3Activity.class);
startActivity(intent);
} else if (i == 4) {
Intent intent = new Intent(MainActivity.this, Lu4Activity.class);
startActivity(intent);
}
}
});
If you want to use same 2nd activity for each intent and show different content then use a global variable to know which item is selected and change content of 2nd activity as you wish.
Add a variable like this
public static int itemNumber;
Use OnItemClickListner
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemNumber = i;
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
In OtherActivity, Set different content to your views depending on itemNumber's value.
Note: You can also use putExtra to extend data to the intent and use that to determine which item is clicked.
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);
In my project, when i click on the button in the first Activity it goes to a second activity. Second Activity plays a video. Video works fine, but when i the press back button on phone it goes back to the first Activity but video sound is still on, meaning video is still playing. How can i stop it when i press back button or click back ?
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(), gnrl.class);
startActivity(int0);
}
#SuppressLint("CutPasteId") public class gnrl extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matematik);
String[] myArray= getResources().getStringArray(R.array.Matematik1);
ArrayAdapter<String> aad= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray);
setListAdapter(aad);
ListView listview=(ListView)findViewById(R.id.listmat);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(),Test.class);
startActivity(int0);
}
}
});
}
private void setListAdapter(ArrayAdapter<String> aad) {
// TODO Auto-generated method stub
}
}
Try overriding the onPause method of the second activity and stop the video playback there.
Just call finish() when you leave the activity playing the video.
protected void onPause() {
finish();
}
Or if you want to leave the activity after pressing a button, do something like
Button goBackButton = (Button) view.findViewById(R.id.back_button);
edit.setOnClickListener(new OnClickListener() {
....
#Override
public void onClick(View view) {
Intent goBackToMainActivity = new Intent(this, MainActivity.class);
startActivity(goBackToMainActivity);
finish();
}
});
But remember,this also stops the video from playing if for instance your phone goes into sleep mode, or anything else that interferes with the activity (dialogs etc.).
I advice you take a couple of minutes to read up on the basics of Activity in Android.This is a good place to start.
Alternatively, in the manifest you can set the "noHistory" attribute in your activity element to true.
Read more on that here
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
}