How to fill a listview with an edit text? - android

i have to activities:
activity 1 with twoo edit text and
activity 2 with a list view.
Everytime i fill the forms in activity1 and press the button "send" i want that all i have wrote in the two edit texts go in one row of the list view of activity 2.
I have tried and it is the result but i don't know how i have to continue:
this is the code of activity1 when the button is pressed:
buttonSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "Button Send Report Clicked......");
object=object.getText().toString();
description = editDescription.getText().toString();
getCoordinates();
Intent i=new Intent(FirstReporting.this, MyList.class);
i.putExtra("object", object);
i.putExtra("description", description);
startActivity(i);
}
});
this is the code of activity2:
arrayAdapterListReports arrayAdapter = new ArrayAdapterListReports(this, R.layout.item_list_reports, listReports);
listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
and then?

In that other activity you can do this to get the values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String objStr = extras.getString("object");
String descStr = extras.getString("description");
// Do what you want with these now ...
}

Check out the Receiving Simple Data from Other Apps guide.
In your second Activity, you can retrieve the Intent used to start Activity2 using getIntent().
You can then use that Intent object to get your data like so:
Intent i = getIntent();
Bundle extras = i.getExtras();
String obj = extras.getString("object");
String desc = extras.getString("description");

Related

Android how to pass image from image view and text from text from one activity to another activity

Hi in android i know how to send image from one activity to another and send text from text view to another,separately. But i want to know in an activity we have both text view and image view.But my need is i want to send both text and image from one activity to another.Any suggestions is welcomed.
Thank you.
What do you mean by sending along an image? For passing a string, use Intent extras and Bundle.
In your first activity...
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("text_contents", someTextView.getText().toString();
startActivity(intent);
}
});
In the second activity onCreate(), you retrieve the intent extras you passed via Bundle...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String someString = bundle.getString("text_contents");
}
}
If your "image" is an R.drawable resource, then you could simply add that as well to the intent extras:
intent.putExtra("image_resource", R.drawable.some_image_resource);
And retrieve it from the Bundle like:
int someImageResource = bundle.getInt("image_resource");
And from there you can apply it to some ImageView:
someImageView.setImageResource(someImageResource);
EDIT: made a slight correction + if your "image" is a bitmap, then see Anjali's answer.

Using intent to pass spinner objects to a new activity via a button

On the first activity I have a spinner and a button. I want to make it so that when a spinner option is chosen and then the button is clicked, a new activity is opened and the spinner option is displayed (along with additional information about it) in a TextView. In short, I'm trying to use intent to send and retrieve data, and then add additional information afterwards, which I think I would use putExtra() method.
Here I have the button directing to the second page and I am trying to store the spinner's object via intent.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.done);
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
}
});
Here is Selected.class (second activity)
I want to retrieve the spinner info and put it into a textview.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
}
Also, here is the spinner array named "person_array":
<string-array name="person_array">
<item>Nick</item>
<item>Isaac</item>
<item>Sally</item>
<item>Matt</item>
<item>Tim</item>
</string-array>
problem in your code is that you are creating two different intent object. The former filled up with the string:
Intent i = new Intent(getApplicationContext(), Spinner.class);
i.putExtra("new_variable_name","value");
and you use the latter to star the Selected activity.
startActivity(new Intent(MainActivity.this, Selected.class));
You should use only one
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
In your first code snippet, you create an Intent (i), pointing to something that is not an Android application component (Spinner), put an extra on it, and then throw it away. This will not be useful.
Instead, put your extra on the Intent that you are using with startActivity():
startActivity(new Intent(MainActivity.this, Selected.class)
.putExtra("new_variable_name","value"));

Activity That has Several Extras from Other Activities?

I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data

getIntent.getExtras() does not return null but has no values.

Question is simple, I am not exactly new to Android but I cannot, for the life of me, retrieve the extras passed via an intent from Activity A to Activity B.
See Activity A: This is actually a ListFragment, that implements onListItemClick() to start another activity via an intent.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
Intent i = new Intent(getActivity(), ExpandedTweetView.class);
twitter4j.Status status = adapter.getItem(position);
Bundle extras = new Bundle();
extras.putString(KEY_TEXT, status.getText());
extras.putString(KEY_HANDLE, status.getUser().getScreenName());
extras.putString(KEY_NAME, status.getUser().getName());
extras.putString(KEY_TIMESTAMPS, status.getCreatedAt().toString());
extras.putLong(KEY_RETWEETS, status.getRetweetCount());
i.putExtra(KEY_EXTRAS, extras);
startActivity(i);
}
This part just works, I tested it usng Log.v(TAG, "status.getText()" to make sure that the error was not coming from the Adapter passing an empty item via getItem().
Here is the code on Activity B:
public class ExpandedTweetView extends Activity {
TextView text;
TextView name;
TextView handle;
TextView createdAt;
TextView retweets;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_list_item);
Bundle extras = getIntent().getExtras();
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
text = (TextView) findViewById(R.id.h_content);
name = (TextView) findViewById(R.id.h_name);
handle = (TextView) findViewById(R.id.h_handle);
createdAt = (TextView) findViewById(R.id.h_timestamp);
retweets = (TextView) findViewById(R.id.h_retweet_count);
if(extras != null) {
text.setText(extras.getString(TimelineFragment.KEY_TEXT));
name.setText(extras.getString(TimelineFragment.KEY_NAME));
handle.setText(extras.getString(TimelineFragment.KEY_HANDLE));
createdAt.setText(extras.getString(TimelineFragment.KEY_TIMESTAMPS));
retweets.setText(String.valueOf(extras.getLong(TimelineFragment.KEY_RETWEETS)));
}
}
As you can see, I believe I am using the right code to obtain the extras, using the same code on other applications worked. Not sure why, when the ExpandedTweetView is created via an intent, ALL of the textViews are empty. See: https://www.dropbox.com/s/pso6jbyn6rpks9n/empty_activity.png
What is even MORE strange is that I had initially tried checking to see if the bundle was null by calling this:
if (extras == null) {
Log.v(TAG, "Extras are empty :(");
}
But that line was never executed, meaning the bundle is not null. I also thought that maybe the keys being used to retrieve the individual Strings from the bundle were mismatching; however, in order to remedy that I decided to create constants that could be used on both sides. As you can see on the code, both the key to set the Extra and the Key to retrieve the Extra are the same.
Any ideas as to what the heck is going on?
Bundle extras = getIntent().getExtras();
if (extras != null) {
extras = extras.getBundle("KEY_EXTRAS");
String status = extras.getString("KEY_TEXT");
}
Try adding the extra variable to intent rather than in Bundle
Ex:
i.putExtra(KEY_1, a);
i.putExtra(KEY_2, b);
i.putExtra(KEY_3, c);
Then retrieve it from other activity from intent
Ex:
getIntent().getStringExtra(KEY_1) ;
In Activity A:
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
Bundle b = new Bundle();
b.putString("thisc", "my name");
i.putExtra("bundle", b);
startActivity(i);
In Activity B:
**Bundle bun = getIntent().getBundleExtra("bundle");
if (bun.containsKey("thisc")) {
Log.i("TAG", bun.getString("thisc"));
} else {
Log.i("TAG", "no thisc");
}**
Check the first line of code in Activity B, that's the main difference actually!!
//put value
Intent inatent = new Intent(this,text.class);
inatent_logo.putExtra("message","hello");
startActivity(inatent);
//get vale
String msg = getIntent().getStringExtra("message").toString();
It's Difficult to maintain intent to Bundle and Bundle to Intent if number of data you want ti share from one Activity to Other Activity.
just Simply use Intent with PuExtra() with different argument.
you can pass number of data in intent like :
Sender's Side :
Create your Intent.
Intent My_Intent = new Intent(FromClass.this,ToClass.class);
Add your value which you want to share with other activity.
My_Intent.putExtra("VAR_A",a_value);
My_Intent.putExtra("VAR_B",b_value);
My_Intent.putExtra("VAR_C",c_value);
Call your Intent.
StartActivity(My_Intent);
Receiver's Side :
Intent My_Intent = getIntent();
First_Value=My_Intent.getStringExtra("VAR_A");
Sec_Value=My_Intent.getStringExtra("VAR_B");
Thred_Value=My_Intent.getStringExtra("VAR_C");
I Think its Easy for you to Handel your data from one Activity to other .

Android Intent issue

I want to show the details of a calculation in another activity when click on a button.
How to achieve this.
my first activity java code is
public void onClick(View v)
{
if(v.getId()==R.id.Button07)
{
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
startActivity(intent);
}
The calculation i want to do is
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = textview1.getText().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
i want the result 'tv' to be shown in next activity when i press the submit button.
Where I need to include this calculation.and what are the further steps
Any help is greatly appreciated
Thanks
In your first activity:
public void onClick(View v) {
//Put your calculation code here
Bundle b = new Bundle();
b.putString("answer", youranswer);
//You could also use putInteger, whichever you prefer.
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
intent.putExtras(b);
startActivity(intent);
}
In your second activity, in the onCreate put this:
Bundle b = getIntent().getExtras();
String answer = b.getString("answer");
Answer is your key. it is used to identify what you want to get from the bundle. Using unique keys means you can pass more than one value to the next activity, if you want.

Categories

Resources