need to retrieve data from the database - android

I have a very long string in the database that needs to be retrieved into a swipe view.
But,the problem is that the string comprises of set of "\n\n"
Whenever it is separated with this expression i need to put it in another slide,i mean i am using SWIPE view here..
if(tablecolumn==\\n\\n)
{
code to break it to parts
}
Is this how i should be doing it?
If i am wrong,how to break this string to different parts and enable it into SWIPE VIEW in to different swipe view?

You can simply break your string comprising of a special character like this :-
String str ="mynameisjhon.yournameisdash.bla";
, here you have a string concatenated with " . " (period character)
to break this string do this :-
StringTokenizer st = new StringTokenizer(str, "."); //break the string whenever "." occurs
String temp =st.nextToken(); // it will have "my name is jhon" break
String temp2 = st.nextToken();// it will have "your name is dash"
String temp3 = st.nextToken();//it will have "bla"
now your string is breaked into parts!
Anything else?

Load the whole string into your ViewAdapter and seperate it via substring
or load the string in your Activity/Fragment seperate it via substring, put the strings in an ArrayList, an initiate your ViewAdapter with the ArrayList as data source
either way use substring

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

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

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

Android - Get single String value using HashMap

In my application, I've preloaded Spinner with ArrayList.The ArrayList contains multiple Text String. These text Strings will serve as message template user can select from spinner. Also I want these String keyword might be replaced with variable when message is being sent. Like "Text" will be replaced with EditText content, "Phone No " replaced with Sender's no, "Date" replaced with Message Date
I have tried to search on HashMap in Android, but getting problem:
HashMap<String, String> template=new HashMap<String, String>();
template.put("Text", editText.getText().toString());
template.put("Phone No",senderPhone);
template.put("Date",receivedDate);
now I want to display it as Single string like Text, Phone No, Date. can this String be editable.
You can use
StringBuilder builder = new StringBuilder();
for (String name : template.values())
{
builder.append(name + " ");
}
String templateString = builder.toString();
This will get all the values from the HashMap using the values() and concat it into a String
To get a value from Hashmap, use:
String Text = (String)template.get("Text");
String phoneNo= (String)template.get("PhoneNo");
String date = (String)template.get("Date");
You can combine all the above 3 and display it where ever you want to
For Example,
String displayString = Text + "-" + phoneNo + " -" + date
editText.setText(displayString);
This edit text by default would be editable until and unless you make it non editable.!
EDIT: (Refer comments for purpose of Edit)
(1) Set the edit text value as the value selected by user from spinner.
editText.setText(displayString);
(2) After user selects and modifies the edittext, let the user click a confirm button.
(3) In confirm button code, add the value to your local storage (IF REQUIRED!).
(4) Refresh the spinner to include this modified value
To refresh, if you want to modify the existing list in spinner then refer Refresh spinner data
To refresh, if you want to keep the existing spinner list as such, and add the new value, then refer dynamic add data to spinner but not update the data on the spinner

How to remove brackets and quotation marks from JSON String

I have a ArrayAdapter the places JSON strings into a list view. The problem is when it places the strings into the textview it leaves brackets and quotation marks. For example, if the string contained the name Bob. The string would show up in the ListView as ["Bob"]. How do I remove the brackets and quotation marks?
Here is what I use to get the JSON strings,
String username = json2.getString(KEY_USERNAME);
String number = json2.getString(KEY_NUMBER);
String content = json2.getString(KEY_COMMENT);
tempList2.add (new Item (username, number, content));
customAdapter.addAll(tempList2);
You can use a simple replace:
String bob = "[\"Bob\"]";
bob = bob.replace("[", "").replace("\"", "").replace("]", "");
Log.i("Test", bob);
This will now just print Bob
As an addon to Phil's answer, the end quote (in my chrome browser) did not get removed. I fixed it by using .replace("\"", "") again at the end.

Categories

Resources