I have a spinner control in my android app, I want the user to select an option from the spinner. Once the option is selected the user can be redirected to another intent. My problem is how do i call intent from onitemselcted method of the spinner??.
Or i am doing wring there is other way in which i can do this. I need the user to set the option from the dropdown first before proceeding to the next page.
If i put startactivity(intent) in my onitemselected method i get this
error The method startActivity(Intent) is undefined for the type
MyOnItemSelectedListener (Myonitemselectedlistner is my class which
implements OnItemSelectedListener)
this is my onitemslectedlistner code
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Context ctx = view.getContext();
SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE );
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
startActivity(intent);
}
public void onNothingSelected(AdapterView parent) {
//do nithong
}
}
Try doing something like:
YourParentClassName.this.startActivity(intent);
Here's the fully tested implementation... THIS WORKS:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (position == 1){
Intent intent = new Intent(MyActivity.this, AnotherActivity.class);
MyActivity.this.startActivity(intent);
}
}
public void onNothingSelected(AdapterView<?> parentView) {
// To do ...
}
});
Your onitemselected method is inside a private class that implements OnItemSelectedListener, which doesn't define startActivity. The startActivity method is part of Activity.
You should create a method in the parent activity, and call startActivity from there. You can make a call from your private class into your parent activity.
make your activity implements this interface
public class MyApp implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Context ctx = view.getContext();
SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE );
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
MyApp.this.startActivity(intent);
}
public void onNothingSelected(AdapterView parent) {
//do nithong
}
}
Related
In Fragment, click on listView item to open new activity and pass value to other activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sowing sowing=new sowing();
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer,sowing,sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList =(list.getItemAtPosition(position).toString());
}
});
What you are looking for is Bundles
Here is an example of how you'd use it in your case:
list.setOnClickListener(new AdapterView.onItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
sowing sowing = new sowing();
FragmentManger fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.FragmentContainer, sowing, sowing.getTag())
.addToBackStack("fragBack").commit();
String selectedFromList = (list.getItemAtPosition(position).toString));
// THE BUNDLE
// start a new intent to open the activity
Intent newIntent = new Intent(Context, Activity.class);
newIntent.putStringExtra("NAME_OF_BUNDLE", selectedFromList); // I used putStringExtra because you defined 'selectedFromList' to be a string
startActivity(newIntent);
}
});
Now in the activity you opened just call the name of the bundle like so:
Intent intent = getIntent(); // get the intent that caused this activity to be opened
String selectedFromLastActivity = intent.getStringExtra("NAME_OF_BUNDLE"); // from the intent that caused this activity to be opened, get any extras passed through
The best way is to use SharedPreferences.
https://developer.android.com/reference/android/content/SharedPreferences
https://developer.android.com/training/data-storage/shared-preferences
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);
}
});
I'm trying to pass data from a fragment to an activity using a method in the Activity:
public void setID(int i,int j) {
theme = i;
thread = j;
}
and then, from the fragment I use the Intent to create the activity and pass the data with:
private class ListItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
int pos=position+1;
ThreadActivity threadactivity = new ThreadActivity();
threadactivity.setID(id,pos);
Intent q = new Intent(getActivity(), threadactivity.getClass());
startActivity(q);
}
}
but, when the activity starts the values of "i" and "j" are 0. What I'm doing wrong?
The activity you created and the one you started with the intent are different. You should pass the parameters into the intent and then get them back in the activity.
private class ListItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3)
{
Intent intent = new Intent(context, threadactivity.class);
intent.putExtra("theme", id);
intent.putExtra("thread", pos);
startActivity(intent);
}
}
then in the onCreate of the activity you would do something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
theme = (int) getIntent().getExtras().get("theme");
thread = (int) getIntent().getExtras().get("thread");
}
This ThreadActivity threadactivity = new ThreadActivity(); is pointless because you don't control the creation of activity. The system does. So all data transfer between activities is done through intent, not getter and setter.
Intent q = new Intent(getActivity(),ThreadActivity.class);
q.putExtra("theme", i);
q.putExtra("thread" j);
startActivity(q);
Then you get the values in the next activity using getIntent().getIntExtra(VARNAME, defaultValue);
This is not the right way to pass data to activity. Use Intent to pass data.
Read this:
http://startandroid.ru/en/lessons/complete-list/241-lesson-28-extras-passing-data-using-intent.html
Please use like below
((YourActivity)getActivity()).yourMethod();
yourMethod() is declared in Activity.
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
}
Here's my code
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(this,lastview.class);
startActivity(i);
}
});
the "this" is a ListActivity,but I want the next activity is a normal activity
and my code is wrong in this line
Intent i = new Intent(this,lastview.class);
the wrong message is
The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<lastview>) is undefined
how can I fix it ?
change this line
Intent i = new Intent(this,lastview.class);
like this, change your activity to MyListActivity
Intent i = new Intent( MyListActivity.this, lastview.class);
Here is the sample code .This will help you.
public class YourListActivity extends ListActivity {
private Context context;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
context =this;
.....
......
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(context,lastview.class);
// else you can use the code like below
//Intent i = new Intent(YourListActivity.this ,lastview.class);
startActivity(i);
}
});
}
You can also write it like :
startActivity(new Intent(Yourclass_name.this,lastview.class));