how to set single value and arrays on same edittext in android - 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));

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

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 a string from arraylist of strings, ANDROID

I created an arraylist of strings
List<String> textArray = new ArrayList<String>();
and then I added the strings(which I am getting from edittext) to textArray as follows
String text = editText1.getText().toString();
textArray.add(text);
Now I created a button and need to remove the string from the array when the button is clicked.But i dont know what to do.
I know for arrays of bitmaps we clear a bitmap from array using recycle but Please suggest me how to remove or clear the string from arraylist.
You can call one of:
to remove all entries
textArray.clear();
to remove from a specific position (e.g. first item)
textArray.remove(0);
to remove a specific string (that equals yours)
textArray.remove("myString");
Try this..
Getting position from the textArray array list
int pos = textArray.indexOf(text);
and then remove from the string position
textArray.remove(pos);
because we cannot directly remove the string. we can remove the string contains position.
You can do it with more then one way as per your need.
textArray.clear();
It will clear whole ArrayList.
If you want to remove only some specifis data from index then,
textArray.remove(INDEX TO REMOVE);
Try This
String text = editText1.getText().toString();
textArray.add(text);
to remove
textArray.remove(text);

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();
//...
}

Getting value of choosed option in Spinner

I want to get value of choosed option in Spinner.
I know, I can get this from setOnItemSelectedListener, but I don't want to use this.
I have this:
String spinner1odp = spinnerSubject.getSelectedItem().toString();
But result of this code is: android.database.sqlite.SQLiteCursor#40f828e8. I want to get String, not something like that :/
I think you are popping up Spinner from database.
So considering that you will have to get the selected index first and fetch the required data from the Cursor:
Code Snippet :
int position = mySpinner.getSelectedItemPosition();
Cursor cursor = (Cursor) myAdapter.getItem(position);
String myText = cursor.getString(cursor.getColumnIndex(KEY_NAME));
Further Reference : Android Spinner Selected Item
By using this code you can get...
String value= (String)spinnerSubject.getItemAtPosition(spinnerSubject.getSelectedItemPosition());

Categories

Resources