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));
Related
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.
I've implemented a 'home screen' for my application, which consists of a gridview containing icons and text. This works fine, and I can add an OnItemClickListener so that tapping an icon will create a toast, for example. But I'm not sure how to call startActivityForResult() from here. I could pass in the application context and use this to create the intent etc, but this doesn't feel like the right way of doing it.
My code looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
GridView gridview = (GridView) findViewById(R.id.icons_gridview);
gridview.setAdapter(new HomeScreenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position){
case 0:
//need to start new activity 1 from here
break;
case 1:
//need to start new activity 2 from here
break;
}
}
});
Thanks for any help,
TLB
Method 1 (my prefered method)
Passing ActivityName.this as context is the way I do it. For example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
GridView gridview = (GridView) findViewById(R.id.icons_gridview);
gridview.setAdapter(new HomeScreenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch(position){
case 0:
Intent intent = new Intent(MyActivity.this, NextActivity.class);
startActivityForResult(intent, 0);
break;
case 1:
//need to start new activity 2 from here
break;
}
}
});
Method 2
You could pass getApplicationContext() as the context;
Method 3
Having a Context mContext field is a common method. Set it at the start of your onCreate then use mContext to start your activities.
private Context mContext;
then
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
mContext = this;
...
}
Then you can start a new activity using mContext as the context parameter
Intent intent = new Intent(mContext, NextActivity.class);
startActivityForResult(intent, 0);
Just use
startActivityForResult(new Intent(MainActivity.this, Activity1.class));
and everything is easy
The context should be the context of the current activity. 'this' would work admirably.
I have a problem to call a java class from OnItemClickListener widget..
private OnItemClickListener detailClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent productDetailsIntent = new Intent(getBaseContext(),MenuRestaurantActivity.class);
startActivity(productDetailsIntent);
}
};
Parameter in currentActivity isn't send to anotherActivity class..
How to fix it?
thanks for your attention..
you must declare following lines in your AndroidManifest.xml file, otherwise it will generate an error.
<activity android:name=".MenuRestaurantActivity" android:label="#string/app_name">
</activity>
Instead of this in your onclick
productDetailsIntent = new Intent(getBaseContext(),MenuRestaurantActivity.class);
use this line
Intent intent = new Intent(currentActivity.this, NextActivity.class);
startActivity(intent);
In FirstActivity.java file onclick button you should use below code.
Intent i1 = new Intent(firstactivity.this, secondactivity.class);
i1.putExtra("type", "edit");
startActivity(i1);
In secondActivity.java file oncreate .. use this code.
Bundle extras = getIntent().getExtras();
Strinjg Value = extras.getSerializable("type").toString();
private OnItemClickListener detailClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent productDetailsIntent = new Intent(ActivityName.this,MenuRestaurantActivity.class);
startActivity(productDetailsIntent);
}
};
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
}
}
i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.