Passing data from listview - android

Good night, to pass data from one listview generated a database I do so by Intent and capturing the value:
intent.putExtra("name", listView.getItemAtPosition(position).toString());
To pick do with Bundle:
Bundle bundle = getIntent().getExtras();
String myData = bundle.getString("name");
nombre.setText(miData);
The problem is that I receive is in the form of array:
{value = first name}
I tried various shape and not get capture only the string "first course" because it is what is shown in the listview, any suggestions? Thank you!
PD -> Sorry if my English is not good.

Thanks for the reply but I do not get what I want, when I pick up the data from the database and pick them up
arrayList.add (createList ("value" name));
With the index or value
But I have solved:
int start = myData.indexOf("=");
int end = myData.indexOf("}", start + 1);
nombre.setText(myData.substring(start + 1, end));
is not an elegant solution, but eventually get him in the EditText name.

Related

how to send a string to listView in another activity ? in android

Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

Anyway to have more than one extra added to intent?

I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent

How to set an image according to string sent using i.putExtra?

I want to have an image sent from recipe_button_list.java to recipe_display_screen.java,
and my current code highlights an error in my code...
This is how image is being sent in recipe_button_list:
Intent i= new Intent(getBaseContext(),recipedisplayscreen.class);
//Sending data to the next screen
i.putExtra("textView1", inputIngredients1.getText().toString());
i.putExtra("textView2", inputMethod1.getText().toString());
i.putExtra("image_string",R.drawable.blustudios);
Log.e("n", inputMethod1.getText()+"."+ inputIngredients1.getText());
This is how image is recieved in recipe_display_screen:
Intent i = getIntent();
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
String RecipeImage = i.getStringExtra("image_string");
And this is how it is set (gives error(highlights setImageResource)
MethodDisplay.setText(Method);
IngredientsDisplay.setText(Ingredients);
RecipeDisplay.setImageResource(RecipeImage);
What is my error???
Thanks in advance :P
R.drawable.blustudios is not a String. The auto-generated R.java class contains integers which are actually resource ids.
Change this line...
String RecipeImage = i.getStringExtra("image_string");
...to this...
int RecipeImage = i.getIntExtra("image_string", 0);
You can't send image as string.
You can either use Serialized or Parcable.
I know bitmap is already parcable, maybe ImageView is parcable too check it out.
In case it's not, just save the ImageView as a bitmap and send it.
Whatever the case use setParcableExtra() and same with get.
You clearly don't understand the concept of passing data between activites.
Check out This question, you will probably figure out the answer from there.

NullPointerException from getIntent().getExtras().getString("to")

InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");

Categories

Resources