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.
Related
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.
I'm developing a Androidapplication and I want to be able to start an activity through a spinner value the user have selected.
As an example: In the main activity you'll see a spinner with the values "Color", "Animals". If you choose "Color" and click on a button called "Proceed", you will come to an activity that list different colors in a listview, but if you choose "Animal" from the spinner, you will come to the same activity, but this time it shows a listview of animals instead.
Can anybody give me a hint on how to do this?
(PS this is an example on how I want it to work, I actually need to call everything from a Web Service)
you can implement android.widget.AdapterView.OnItemSelectedListener and override the method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
// Call the following method to get the selected value of the spinner and perform your
//task to start your desired activity
String selectedValue=parent.getItemAtPosition(pos).toString();
if(selectedValue.equals("Colors"))
{
//do your task using color
}
else if(selectedValue.equals("Animal"))
{
//do your task using animal
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//Do Nothing
}
The simplest Solution would be for example:
Spinner yourSpinner = (Spinner)view.findViewById(R.id.your_spinner);
String value = yourSpinner.getSelectedItem().toString();
if(value.equalsIgnoreCase("ANIMAL")){
Intent intent = new Intent(yourActivity.this, yourNextActivity.class);
intent.putExtra("VALUE", value);
startActivity(intent)
}
Then in that Activity that You want to start:
Intent intent = getIntent();
String value= intent.getStringExtra("VALUE");
if(value.equalsIgnoreCase("ANIMAL")){
//start the part with animals
}
Its really simple just use bundle and send the appropriate data to your next activity. A small sample would look like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String spinnerText = spinner.getSelectedItem().toString();
Intent intent = new Intent(A.this,B.class);
intent.putExtra("selection",spinnerText);
}
});
Then in your next activity get the sent text and manipulate accordingly.
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..
I have a ListView where I have some elements. Every item has one ListViewand two TextBoxes. Here is my question: When I clock on element from the list a new activity starts, where I have one ListView and two TextBoxes. How I can do this if I click first element in the new activity in ListView will be ListViewfrom this item and in TextBoxeswill be data from TextBoxes from the list.
You can pass extras to the Intent you use when starting the new Activity.
Let's say your current activity is MyActivity, and the one you want to start by clicking on a list item is MyNewActivity; Then in your MyActivity class, inside the list item click listener should be modified as:
Intent intent = new Intent(MyActivity.this, MyNewActivity.class);
intent.putExtra("my.picture.id", images[itemPosition]);
intent.putExtra("my.header.id", headers[itemPosition]);
intent.putExtra("my.text.id", texts[itemPosition]);
startActivity(intent);
and in your MyNewActivity class' onCreate method you are able to retrieve the passed extras, and fill the proper fields with the correct values:
final Intent intent = getIntent();
final int pictureId = intent.getIntExtra("my.picture.id", 0);
final int headerId = intent.getIntExtra("my.header.id", 0);
final int textId = intent.getIntExtra("my.text.id", 0);
((ImageView)findViewById(R.id.my_image)).setImageResource(pictureId);
((TextView)findViewById(R.id.my_header)).setText(headerId);
((TextView)findViewById(R.id.my_text)).setImageResource(textId);
the images, headers and texts arrays -I suppose- contain the resource ids for the images and strings you want to display. They are probably accessible via the data of your current item's renderer.
I would go about it a bit differently (in retrieving the information at least) than rekaszeru.
In your first activity you would use onListItemClick and put the information into the extras bundle to be passed with the intent to start the second activity. In this method, you use the view passed in to retrieve the info, so it doesn't matter what kind of adapter you are using and the position in the adapter doesn't matter.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent myIntent = new Intent(FirstClass.this, SecondClass.class);
myIntent.putExtra("ImageRef", v.findViewById(R.id.imageview)).getTag());
myIntent.putExtra("Text1", v.findViewById(R.id.TextView1).getText().toString());
myIntent.putExtra("Text2", v.findViewById(R.id.TextView2).getText().toString());
FirstClass.this.startActivity(myIntent);
}
Then in the second activity retrieve the info to be used:
private TextView NewTextView1;
private TextView NewTextView2;
private ImageViewView NewImageView;
Bundle extras = getIntent().getExtras();
NewTextView1 = (TextView)findViewBYId(R.id.newtextview1).setText(extras.getString("Text1"));
NewTextView2 = (TextView)findViewBYId(R.id.newtextview2).setText(extras.getString("Text2"));
NewImageView = (ImageView)findViewBYId(R.id.newimageview).setImageResource(extras.getInt("ImageRef"));
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()