Android - Get single String value using HashMap - android

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

Related

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

how to set single value and arrays on same edittext in android

I'm getting contacts from the "+" button and retrieving email from MULTIPLE_CHOICE_LIST and getting in array form and setting on edittext, but when i manually write the value in edittext and put "," and then add '+' button to retrive more emails than it replaces the value i have written manually.Please, tell me how to keep the value.
for eg-
abc#gmail.com, and when i retrive from contacts it replaces with an array like [xyz#sdsd.com,qwe#wer.com] and i want it like abc#gmail.com,xyz#sdsd.com,qwe#wer.com
Thanks in advance...
try this way
String[] emails = {"emai1#abc.com","emai2#abc.com","emai3#abc.com"};
String finalStr="";
for (String string : emails) {
finalStr+=string+",";
}
edtEmail.setText(finalStr.substring(0, finalStr.length()-1));

how to get the value of different editText inside the hashmap in android

Please help me fix this one. I'm already stock in this. I am trying to get the values of the editText inside the hashmap using the code below. It creates multiple editText depending upon the number of files that the user selected.
//SAVE All Attachment
EditText txt_iDesc = (EditText)findViewById(R.id.txt_iDesc);
SQLiteDatabase db = databaseHandler.getWritableDatabase();
db.beginTransaction();
for(HashMap<String, String> map : mylist)
{
String desc = txt_iDesc.getText().toString();
ContentValues cv = new ContentValues();
cv.put(Constants.ATTACH_REPORTCODE, ReportCode);
cv.put(Constants.ATTACH_FILENAME, map.get(FILE_NAME));
cv.put(Constants.ATTACH_DESCRIPTION, desc);
cv.put(Constants.ATTACH_FILELOCATION, map.get(FILE_URI));
cv.put(Constants.ATTACH_CREATEDBY, map.get(UPLOADED_BY));
cv.put(Constants.ATTACH_DATECREATED, map.get(DATE_UPLOADED));
db.insert(Constants.TABLE_ATTACH, null, cv);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
But when I save it. It only gets the value of the first editText. When I retrieved the data that has been saved it shows that the value "one" of the first editText was the only saved, thats my problem, I don't know what to do save the value of different editText inside the hasmap.
Ummm...you only show one EditText. Where are the others in your code?
Anyway, you say, "It creates multiple editText depending upon the number of files that the user selected." Then when you create each EditText put them in an Array then you can iterate through them to get the text of each to put in the HashMap
EditText txt_iDesc = (EditText)findViewById(R.id.txt_iDesc);
for(HashMap<String, String> map : mylist)
{
// you are using the same edit text reference in your code, you should create multiple edit text
String desc = txt_iDesc.getText().toString();
//...
}

need to retrieve data from the database

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

Android - Reading common data between Activities using a Spinner

I have an Android application which retrieves from a external server, a name and a corresponding ID (this could be 1 name and ID combo or multiple name ID combinations), these are all stored in a HashMap<String, String> - The ID as the key and the Name as the value.
What I then would like to implement is a dynamic Spinner that populates itself with the 'Names' from this HashMap and when the user selects one of these names a setting somewhere I set to the ID number. This ID number will then be used in later server requests.
My question is what is the best way to implement this custom Spinner from the HashMap so that when the option is selected the ID number is set somewhere. This ID number has to be accessible from several activities - the spinner is present in several different activities... but should have the same effect on each screen.
Design patterns and pseudo code would be hugely appreciated. At the moment the Spinner is on 2 different screens, at the top below the ActionBar, but obviously the code is in both XML layout files.
Cheers.
EDIT
Code to set names and IDs in HashMap:
// Returns a Map of blog name to blog ID associated with the authenticated user
public Map<String, String> extractBlogInfo(XMLDataExtracter blogData)
{
Log.d(TAG, "BlogOperations::extractBlogInfo()");
ArrayList<String> blogIDs = new ArrayList<String>();
ArrayList<String> blogNames = new ArrayList<String>();
Map<String, String> blogIDNamePairs = new HashMap<String, String>();
// Get ID and Names and store them in blogIDs and blogNames variables
if (blogIDs.size() == blogNames.size())
{
for(int i = 0; i < blogIDs.size(); i++)
{
blogIDNamePairs.put(blogIDs.get(i), blogNames.get(i));
}
}
else
{
// An error occured
Log.d(TAG,
"BlogOperations::extractBlogInfo() > An error occured - ID and Name array sizes do not match");
return null;
}
return blogIDNamePairs;
}
For this you can use Shared preference.. You get the Id of the selected value from spinner and stored it in shared preference..
So that you can access this value any where inside your Apps..
If you want to display the selected spinner value from Say Activity1 in Activity2 get the value from Shared preference, now it will be Id so get the corresponding Name from the Id in hash Map..
For shared preference take a look at this... and this....

Categories

Resources