How to pass dynamic data in intent from a qr code? - android

I am done a Qr code which have data like
url=www.google.com;type=T;location=xyz;company=aaa
or
url=www.google.com;
type=T;
location=5Q01;
company=SYS
or url=www.google.com|type=T|location=5Q01|company=SYS
and I have done reading QR with this tutorial in android.
so here I want to pass this data separately via intent
like url=www.google.com in one intent and type=T;location=xyz;company=aaa in other intent
but here this entire content I am getting in one rewtext format...
can any one help me how to resolve..

Well the response is in String format, you can simply split the string with ';' char and get a separate array which will serve your purpose.
String s = "url=www.google.com;type=T;location=xyz;company=aaa";
String sArray [] = s.split(";");
then -
sArray[0] = url=www.google.com
sArray[1] = type=T
sArray[2] = location=xyz
sArray[3] = company=aaa

Related

How do I read only a specific part of the string in Android Studio?

If a send a string in the following format: 1234,1234,1234,1234; from Arduino to Android Studio (java (intelliJ)) based. with the amount of characters between every komma changing. How do i make it so that my code only reads the string from for example 0 to the , Or from the first , to the second ,?
If you are using Java, I would recommend looking into the Java.String.split() method. This method will split your string in an array of strings, depending on your delimiter. For example :
String s = "1234,1234,1234,1234";
String[] result = s.split(",");
You can split the input string based on any special character,in your case ,, as follows
String inputString = "1234,1234,1234,1234";
String[] separated = inputString.split(",");
Log.i("MainActivity",separated[0]) // prints the first string which is 1234
// to loop over all strings
for(String s : separated){
Log.i("MainActivity",s)
}

Read out Bundle content

I use intents to communicate from a service to the activity. I use intent.putExtra(Tag, message); if I have a error or something.
In the Activity I can get these Extras like following
String message_error_1 = intent.getStringExtra("Message_Error_1");
String message_error_2 = intent.getStringExtra("Message_Error_2");
String message_error_3 = intent.getStringExtra("Message_Error_3");
but just one has data. I can now make an If statement for every entry but I think there is a way to figere out which entry has data. Is there a way?
If there's only going to be one error, I'd consider passing an ERROR_TYPE int as one extra and the ERROR_MESSAGE string as another extra, so you don't have to write so many if statements. For example:
int type = intent.getIntExtra("error_type"); // 1, 2, or 3
String message = intent.getStringExtra("error_message");
To answer your question, you could have the service put in an extra that indicates which error message key to use. For example:
// In Service
intent.putExtra("error_key", "Message_Error_1"); // error 1 has the message!
// In Activity
String key = intent.getStringExtra("error_key");
String actualErrorMessage = intent.getStringExtra(key);

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);

JSON Problem in android (objects disappear)

I'm trying to make a small app (an image gallery from images from the web, were the url's I get from the JSON file that I received). The context of JSON looks like that:
{"images":{
"yXVak":{
"image_hash":"yXVak",
"imgur_page":"http:\/\/imgur.com\/yXVak",
"original_image":"http:\/\/imgur.com\/yXVak.gif",
"large_thumbnail":"http:\/\/imgur.com\/yXVakl.gif",
"small_thumbnail":"http:\/\/imgur.com\/yXVaks.gif",
"message":"I didn't know they made you see THAT well.",
"source":" ",
"date_popular":"2011-07-18 18:45:05"},
.....
I have about 30 more objects that looks like "yXVak".
Now, the problem is, when I'm trying to parse the text, the program can't find the object "yXVak", the exception looks like that: org.json.JSONException: JSONObject["yXVak"] not found.
I parse the JSON file like that:
jObject = new JSONObject(jString);
JSONObject jImages = jObject.getJSONObject("images");
getImages(jImages);
getImages function looks like that:
JSONObject jHash = jImages.getJSONObject("yXVak") ;
String hash = jHash.getString("yXVak");
String page = jHash.getString("http:\\/\\/imgur.com\\/yXVak");
Image[] images = new Image[3];
images[0] = new Image(jHash.getString("original_image"), jHash.getString("http:\\/\\/imgur.com\\/yXVak.gif"));
images[1] = new Image(jHash.getString("large_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVakl.gif"));
images[2] = new Image(jHash.getString("small_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVaks.gif"));
String message = jHash.getString("I didn't know they made you see THAT well.");
String source = jHash.getString(" ");
String date = jHash.getString("2011-07-18 18:45:05");
listOfImages.add(new ImageHash(hash, page, images, message, source, date));
...
By debugging I found that the jString object looks right (the whole string that in the file), but the jImages object missing two first objects ("yXVak", and the second one that I didn't show here "6k9yE").
Can someone help me with that please, what did I do wrong?
I thing you should change this lines in your getImages function:-
JSONObject jHash = jImages.getJSONObject("yXVak");
//Changes in this lines.
String hash = jHash.getString("image_hash");
String page = jHash.getString("imgur_page");
// Rest of your code is same.
Please try this out.I think that it will solve your problem,

Categories

Resources