Passing values from one screen to another in Android - android

Heres my first class
public void onClick(View view) {
Intent i = new Intent(First.this,second.class);
startActivity(i);
cat=(EditText) findViewById(R.id.textView_cat);
String s = getIntent().getStringExtra("myString");
cat.setText(s);
Heres my second class
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
String selectedFromList = o.get("name");//(String) (lv.getItemAtPosition(position));
Intent i = new Intent(second.this, First.class);
i.putExtra("myString", selectedFromList);
startActivity(i);
}
I want to go to the second class (second screen) after clicking button of first(on first screen). Then i want to access the value of listitem selected in second class(screen) in first(screen). But having problem in doing that. Help would be appreciated..!!
I am enabled to pass the value from first.class to second.class but i want to take value in second.class and access it in first.class.. !! Theres the problem...!!!
Thanx.

There are two ways to pass variables.
1) Use the extra value of your intent:
myIntent.putExtra(String name, Bundle value);
Here is the android documentaton on that.
http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)
2) Use an application class.
http://developer.android.com/reference/android/app/Application.html
The application class creates a singleon that you can use to make data avaible to any activity. The activity references the application through
activity.getApplication();
http://developer.android.com/reference/android/app/Activity.html#getApplication()

Related

How do i get the rowID of a list item in another activity class?

In my main activity I have a list view of database items, when i long press on an item in a list it opens into a new activity that allows you to edit the item clicked.
The problem I'm having is that i'm not quite sure how to get the information of the rowID/primary key to the other activity java class so that i can start manipulating in that class e.g. set the initial values as the current row information and then being able to apply an update to it.
Incase this is of use to helping you help me here is what i have to take me to my new activity
dialog.setNegativeButton(R.string.dialogEdit,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
long id = idInDB;
Intent intent = new Intent(MainActivity.this, edit_expense.class);
startActivity(intent);
}
});
onClick send the row id using putExtra
intent.putExtra("rowid", rowid);
On second activity get the extra string in onCreate()
String rowid = getIntent().getExtras().getString("rowid");
I'm assuming id is the id you want to pass to the other activity, use Intent extras to pass it.
intent.putExtra("key", id);
and to get the value in the other activity:
Intent intent = getIntent();
long id = intent.getLongExtra("key", -1);
Intent reference: https://developer.android.com/reference/android/content/Intent.html
Hope fully in your list row you have a textView that contains the ID.
So when you load the listView, every row will add its own unique ID in the textView. Now when you click/hold the any specific listItem, it will trigger the click event and will retrieve the ID into the String rowID. Now you can pass this ID using intent.
list.setOnLongClickListener(new AdapterView.OnLongClickListener() {
#Override
public void onLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String rowID =((TextView) view.findViewById(R.id.rowID)).getText().toString();
Intent nextActivity = new Intent();
nextActivity.putExtra("id",rowID);
startActivity();
}

how can i go from one fragment to another fragment?

I want to go from one fragment to another fragment via click on a widgets.
I used this code in the main activity and used onclick in wedget property:
public void goOther(View v) {
Intent go_To_FragmentTwo = new Intent(this, FragmentTwo.class);
startActivity(go_to_FragmentTwo);
}
please with sample code. and please introduce some articles.
You would need to do the following:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Intent intent = new Intent(view.getContext(), EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
The code below is actually also passing the id of the selected item from one fragment to another, to then display the details according to the value in the ID.
Hope this helps :)

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.

How to pass the selectedListItem's object to another activity?

I have designed an activity in which there is a listView. I want that on click of the list item a new Activity should start and it should contain detail data related to the ListItem clicked.Please help me out in doing this.Thanks in advance.
While starting an activity use putExtra to send data to another activity
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("listitemselected", listitemvalue);
startActivity(i);
In SecondActivity.java use getStringExtra to get the values.
Intent i = getIntent();
String listItem = i.getStringExtra("listitemselected");
In the action handler start the new activity with putextra you can add some data
Intent intent = new Intent(ShowLocation.this, ListLocationActions.class);
intent.putExtra("infoName1", myData);
intent.putExtra("infoName2", MyData2);
startActivity(intent);
in the new activity:
String myData = getIntent().getStringExtra("infoName1");
String myData2 = getIntent().getStringExtra("infoName2");
In your onItemClick. Try something like this;
you can send id of the item if your are using Sqlite database. or your can send the Object (If Serializable) from the Object array at specific postion. The postion will be the index of the selected List Item as well as index of the Object in that specific array, Or it may be a String.
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
Intent status = new Intent(CurrentActivity.this,NextActivity.class );
status.putExtra("_ID", id);
//Or
status.putExtra("OBJECT", mArray[position]);
startActivity(status);
}
String temp = listView(v.getId()).getText().toString();
Intent i1 = new Intent(currentAct.this,nextAct.class);
i1.putExtra("listitem",temp);
startActivity(i1);
listView() ==>It is having all the listItem that you are displayed on listItem

Categories

Resources