How to start multiple activity By onClick method - android

I am working on a project where I am showing multiple image in SwipeView with a button. So whenever the button is clicked a new activity will start according to the position of the image in the array. How to do this?

With each item in your list, you need a Class reference on which Activity you want to start, then assign the click listener to pass that Class to an Intent and then startActivity
Assuming you have some ArrayList<Class> list or an ArrayAdapter<SomeObject> adapter, you could do
public void onClick(View v) {
Class activityClass = list.get(clickPosition);
// Class activityClass = adapter.getItem(clickPosition).getActivityToStart();
Intent intent = new Intent(this, activityClass);
startActivity(intent);
}
You could also use a String tag on the View with the name of a class, but you'll need to catch the ClassNotFoundException.
public void onClick(View v) {
String className = (String) v.getTag();
Class activityClass = Class.forName(className);
Intent intent = new Intent(this, activityClass);
startActivity(intent);
}

Set a tag (relevant to the activity being launched) to each ImageView while inflating, using setTag method.
In the onClick handler retrive the tag using the getTag method. Use this information to launch the required activity, using a simple switch case .
The tag can be anything like activity name, array index etc.
More info: What is the main purpose of setTag() getTag() methods of View?

Related

How to get the current value from a spinner and pass it to text View of another activity?

I want to retrieve a value from the spinner and pass it to the textView of another class and I am not figuring out the way how to do this any help would be appreciated? I am a new to Android :)
Simply this is how you get selected value from a spinner:
String text = mySpinner.getSelectedItem().toString();
There are many ways to pass it to another activity, here are some of them:
Pass it through an intent as extras
Save it as a Singleton
Save it in the shared preferences
Make it some kind of a static variable in a static class
Here is an example using the first way. Suppose you will click a button after selecting a value from a spinner and want to pass the value to the next activity
String text = mySpinner.getSelectedItem().toString();
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("mySpinnerValue", text);
startActivity(intent);
}
});
And inside the onCreate method of MainActivity2, do the following:
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("mySpinnerValue");
myTextView.setText(text);

How to start new activity on button click event in custom adapter?

I am very new in Android, doing code using google only, plz help.
Here is my code from custom adapter, which is for custom view which is having list of text and buttons, on button click event i want to start new activity, like for example, if user press button which is in front of "Geometry" text, then new activity should get start, what should I pass to intent in above code:
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(txtListChild.getText().toString()=="Geometry")
{
Toast.makeText(_context, txtListChild.getText().toString(),
Toast.LENGTH_LONG).show();
**Intent i=new Intent();**
_context.startActivity(i);
}
}
});
return convertView;
}
Make intent like
Intent i=new Intent(_context,secondActivity.class);
_context.startActivity(i);
Remember you must register secondActivity in manifest.xml
and also you should change
if(txtListChild.getText().toString()=="Geometry")
to
if(txtListChild.getText().toString().equals("Geometry"))
always used .equals() method for string comparison.
-By passing context from activity to the adapter like
Youadapter obj=new Youradapter(Youactivity.this);
-In you adapter class set you Context con;
-and using con.startactivity;
How to start new activity on button click event in custom adapter?
First, use String.equals for comparing strings:
if(txtListChild.getText().toString().equals("Geometry"))
Second, to start Activity using string class name use Class.forName to get class:
Class<?> c = Class.forName(txtListChild.getText().toString());
Intent i=new Intent(v.getContext(),c);
And make sure all activities are declared in AndroidManifest.xml
NOTE: if txtListChild.getText() return Activity name then no need to use if-else just use Class.forName to get class and pass it as last parameter of Intent constructor :

ListView Click change from another activity

I have a Listview activity and a Mainactivity.
In my Listview activity i have a array that is
public static final String[] link = {
"http://3313.live.streamtheworld.com/WKRKFM_SC",
"http://3573.live.streamtheworld.com/KXXRFMAAC",
"http://2713.live.streamtheworld.com/KXXRFMAAC",
"http://r16---sn-npo7enel.gvt1.com/videoplayback" };
Im using putExtra and passing this array to my Mainactivity using this.
Intent intent =new Intent(MainActivity.this, Linkview.class);
intent.putExtra("link", link[position]);
startActivity(intent);
And i received my extra using this in Mainactivity.
final Bundle bundle = getIntent().getExtras();
String link = bundle.getString("link");
and i can use it very well but in my MainActivity
I have a next button
next.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unused")
#Override
public void onClick(View v) {
//??????
}
});
I want that when i pressed the next button the next array comes from listview activity
and i can show this in my MainActivity.
How i can do that??
So from your question, I assume you want to show the next item in the array.
As long as you keep your array as a static final variable (i.e. a constant array), you can directly access it from your main class. You don't need an instance of the class to access a static variable. For instance, the following should work from your main Activity:
String item = ListviewActivity.link[position];
Thus, you can simply pass the position as an integer, look up the string with the above code, and add one to the position to get the next item.

Passing variables to another class via Intent and Adapter

I'm trying to pass a bunch of variables to another Activity,
but in the receiving Activity it only got access to the first element.
My listView1 contains 3 Elements: a 2 TextViews and 1 ImageView...
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(),
DisplayListEntry.class);
intent.putExtra("TOPIC", listView1.getAdapter().getItem(position).toString());
startActivityForResult(intent, 0);
Receiving Activity:
Intent intent = getIntent();
String s1 = intent.getStringExtra("TOPIC");
And i want to access them via the Intent...
Could somebody please be so kind and tell me how its done? :/
Thanks in advance!
Ok, you have three views and want to these data to another activity.
The first problem is you are starting another activity inside the onItemClick and at this point, you are using the item position to get current value. That is ok, but you are only getting a value for one specific view, whose is being clicked.
listView1.getAdapter().getItem(position).toString();
If you really want to pass the three values my suggestion is:
Create a field on your class for each view
Ex:
Textview t1, t2;
ImageView i1;
And onClick event you will use it to put inside the intent like that:
onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(parent.getContext(), DisplayListEntry.class);
intent.putExtra("key1",t1.getText());
intent.putExtra("key2",t2.getText());
intent.putExtra("key3",i2.get***());
startActivityForResult(intent, 0);
In you another activity you can access those values:
Intent intent = getIntent();
String s1 = intent.getStringExtra("key1");
String s2 = intent.getStringExtra("key2");
*** = intent.get****("key3");
You should change *** for the data type you want to pass.
you can Add PutExtra as many as you want with different key and position of your item and in another class get it using key.
First you need to correct in this in next activity
Bundle b = getIntent().getExtras();
if( b != null ){
String s1 = intent.getStringExtra("TOPIC");
}
Intent intent = getIntent();
String s1 = intent.getStringExtra("TOPIC");
here you s1 declare as string..
it will not work because there are u pass arraylist.....
declare arraylist variable and try to implement it..

Acessing data form database on click of listView

I have a ListView in activity1 where I have a few items which are the word from the database . The User Clicks on an item in the Listview and it will navigate to activity2 where it should show the details of the word which are stored in database as column word, definitions . But What appears in Screen2 depends on what item was clicked in Screen 1
For Ex - User Clicks A in Screen 1 - Words starting from A come up in Screen 2. Is there any way to pass the row id so that in next screen the word and definition from the database can be displayed.
Thank you in adv..
code for first activity:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
Cursor c = mDictCursor;
c.moveToPosition(position);
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle=new Bundle();
//intent.putExtra("position",position);
bundle.putLong(DBAdapter.KEY_ROWID, id);
bundle.putString(DBAdapter.KEY_TITLE, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_TITLE)));
bundle.putString(DBAdapter.KEY_DEFINITION, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_DEFINITION)));
i.putExtras(bundle);
startActivity(i);
}
SecondActivity code:
Bundle extras = getIntent().getExtras();
mRowId = extras.getLong(DBAdapter.KEY_ROWID);
String title = extras.getString(DBAdapter.KEY_TITLE);
String body = extras.getString(DBAdapter.KEY_DEFINITION);
TextView word = (TextView) findViewById(R.id.word);
word.setText(title);
TextView definition = (TextView) findViewById(R.id.definition);
definition.setText(body);
}
Whenever I am clicking the listview item it is showing dialog to force close. Please help...
you can use Intent object putExtra(String name, int value) method in Screen1(see1) and pass the intent object to Screen2,in Screen2 use getIntExtra(String name, int defaultValue) method of Intent object(see 2).
1.use startActivity(Intent intent) method in Screen1
2.use getIntent() method in Screen2 to get Intent object which you passed in the Screen1
I guess you are looking something similar like this .
https://market.android.com/developer?pub=acharya
https://market.android.com/details?id=com.acharyaapp.malayalam.aksharam.full
(Those are my apps)
Why dont you try using a Singleton class to store information. One Intent can set it and the other intent can read it. I employed that logic in my apps.
EDIT
public class MySingleton {
private static final MySingleton INSTANCE = new MySingleton();
//TODO ... all your variable as public static variables. eg. KEY_TITLE
private MySingleton() { }
public static MySingleton getInstance()
return INSTANCE;
}
}
Inside activities, use them as normal instances. Just make sure to call the getInstance() instead of constructor to get the shared instance.
hope this helps.

Categories

Resources