I know this is probably one of dumbest questions ever but my brain seems off.
I have this method which makes a string out of vCard:
public static String process(String vCard) {
ArrayList<ArrayList<String>> vCardData = parseData(vCard);
if (vCardData != null) {
StringBuilder readableVCard = new StringBuilder();
for (int i = 0; i < FIELD_COUNT; i++) {
ArrayList<String> vCardDataField = vCardData.get(i);
if (vCardDataField.size() > 0) {
String field = null;
if (i == FORMATTED_NAME) {
field = "Name: ";
} else if (i == PHONE_MOBILE) {
field = "Phone (mobile): ";
} else if (i == PHONE_HOME) {
field = "Phone (home): ";
} else if (i == PHONE_WORK) {
field = "Phone (work): ";
} else if (i == PHONE) {
field = "Phone: ";
} else if (i == FAX_HOME) {
field = "Fax (home): ";
} else if (i == FAX_WORK) {
field = "Fax (work): ";
} else if (i == PAGER) {
field = "Pager: ";
} else if (i == EMAIL_HOME) {
field = "Email (home): ";
} else if (i == EMAIL_WORK) {
field = "Email (work): ";
} else if (i == EMAIL) {
field = "Email: ";
} else if (i == ORGANISATION) {
field = "Company: ";
} else if (i == JOB_TITLE) {
field = "Job title: ";
} else if (i == ADDRESS_HOME) {
field = "Address (home): ";
} else if (i == ADDRESS_WORK) {
field = "Address (work): ";
} else if (i == ADDRESS) {
field = "Address: ";
} else if (i == IM_SKYPE) {
field = "Skype: ";
} else if (i == IM_GOOGLE) {
field = "Google Talk: ";
} else if (i == IM_JABBER) {
field = "Jabber: ";
} else if (i == IM_YAHOO) {
field = "Yahoo: ";
} else if (i == IM_MSN) {
field = "MSN: ";
} else if (i == IM_ICQ) {
field = "ICQ: ";
} else if (i == IM_AIM) {
field = "AIM: ";
} else if (i == TWITTER) {
field = "Twitter: ";
} else if (i == BIRTHDAY) {
field = "Birthday: ";
} else if (i == ANNIVERSARY) {
field = "Anniversary: ";
} else if (i == NOTES) {
field = "Notes: ";
} else if (i == WEBSITE) {
field = "Website: ";
} else {
continue;
}
if (readableVCard.length() != 0) {
readableVCard.append("\n");
}
readableVCard.append(field);
for (int j = 0; j < vCardDataField.size(); j++) {
if (j != 0) {
readableVCard.append("; ");
}
readableVCard.append(vCardDataField.get(j));
}
}
}
if (readableVCard.length() != 0) {
String textVCard = readableVCard.toString();
try {
textVCard = qpDecoder.decode(readableVCard.toString());
} catch (Exception e) {
Logger.e("VCard to UTF-8", e.getMessage());
}
return (textVCard);
}
}
return (null);
}
So my current output is like this:
Name: Marko
Phone(mobile):1312
Phone(fax):441231
Phone(home):543534
Email(home):dddd
Email(work):eeee
Email(other):aaaa
What I want is to add a line break between groups (name/phone/email) so I get something like this:
Name: Marko
Phone(mobile):1312
Phone(fax):441231
Phone(home):543534
Email(home):dddd
Email(work):eeee
Email(other):aaaa
Addressblablabla
.
.
.
IMsblablabla
.
.
.
Can someone help please?
Thanks.
You could iterate the string array, splitting by : and then checking if next item starts with current. If it does, then do nothing, if it doesn't you add the line break to current item and replace it in the array.
Note: splitting with : also separates all phone (xxx) types. To avoid this, you can create enum type, map type to Phone if the string starts with "phone" and use that enum as current type while iterating through the array. If current type if different than next type you add line break.
You can append line separator via
System.getProperty("line.separator");
Related
I am writing a regex for an android calculator. The "+/-*" are okay for non decimal values but the problem lies in the ".". How do i check for the "." ?
Here is my regex :
"(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"
This is my method for calculations:
private double calc(String resultString) {
String[] result = resultString.split("(?<=[^\\\\d.])(?=\\\\d)|(?<=\\\\d)(?=[^\\\\d.])");
String op = null;
double res = 0;
for (String s : result) {
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") ) {
op = s;
}
else if(s.equals(".")){
}
else {
if (op == null) {
res = Double.parseDouble(s);
} else {
if (op.equals("+")) {
res = res + Double.parseDouble(s);
} else if (op.equals("-")) {
res = res - Double.parseDouble(s);
} else if (op.equals("*")) {
res *= Double.parseDouble(s);
} else if (op.equals("/")) {
res /= Double.parseDouble(s);
}
}
}
}
return res;
}
Exclude the . from the \D shorthand with a negated character class [^\d.] that will match any char but a digit and a literal . (note the . does not have to be escaped in the character class to be parsed as a literal dot):
"(?<=[^\\d.])(?=\\d)|(?<=\\d)(?=[^\\d.])"
See the regex demo
And an online demo (in Java):
String resultString = "1-1+0.4/2+0.5*4";
String[] result = resultString.split("(?<=[^\\d.])(?=\\d)|(?<=\\d)(?=[^\\d.])");
String op = null;
double res = 0;
for (String s : result) {
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") ) {
op = s;
}
else {
if (op == null) {
res = Double.parseDouble(s);
} else {
if (op.equals("+")) {
res = res + Double.parseDouble(s);
} else if (op.equals("-")) {
res = res - Double.parseDouble(s);
} else if (op.equals("*")) {
res *= Double.parseDouble(s);
} else if (op.equals("/")) {
res /= Double.parseDouble(s);
}
}
}
}
System.out.println(res); // => 2.8 (= 1-1+0.4/2+0.5*4)
I'm developing softkeyboard in updateCandidates(). My question is how to retrieve each text when Composing have some words then load my dictionary in candidate view.
Image:
How to load words.
Image
like this
Thanks for share me any information about it.
Solution
You can make method like this :
public ArrayList<String> readFile() {
ArrayList<String> list = new ArrayList<String>();
try {
AssetManager am = getAssets();
InputStream data = am.open("data.txt");
InputStreamReader dataInputStream = new InputStreamReader(data);
BufferedReader reader = new BufferedReader(dataInputStream);
String each = null;
while ((each = reader.readLine()) != null) {
list.add(each);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
Edit Softkeyboard
pickSuggestionManually()
public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCandidateView != null) {
mCandidateView.clear();
}
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (mComposing.length() > 0) {
// If we were generating candidate suggestions for the current
// text, we would commit one of them here. But for this sample,
// we will just commit the current text.
mComposing.setLength(index);
mComposing = new StringBuilder(mCandidateList.get(index) + " ");
commitTyped(getCurrentInputConnection());
}
}
Important here updateCandidates()
ArrayList<String> listData = readFile();
System.out.println("Sonu Kumar: " + listData);
if (!mCompletionOn) {
if (mComposing.length() > 0) {
ArrayList<String> list = new ArrayList<String>();
// list.add(mComposing.toString());
for (int j = 0; j < listData.size(); j++) {
String str = mComposing.toString().toLowerCase();
if (listData.get(j).startsWith(str)) {
list.add(listData.get(j));
}
}
mCandidateList = list;
setSuggestions(list, true, true);
} else {
setSuggestions(null, false, false);
}
}
Thank
To show the suggestion words in candidate view, please modify the updateCandidates() method of SoftKeyboard.java file:
ArrayList<String> list = new ArrayList<String>();
updateCandidate(){
if (!mCompletionOn) {
list.clear();
if (mComposing.length() > 0) {
String mStr= mComposing.toString().toLowerCase();
for (int i = 0; i < Dictionary.data.length; i++) {
String str = Dictionary.data[i];
if (str.startsWith(mStr)) {
list.add(str);
}
}
setSuggestions(list, true, true);
} else {
setSuggestions(null, false, false);
}
}
}
To enter the picked word from candidate view to input text, please modify following method in SoftKeyboard example project.
public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0
&& index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCandidateView != null) {
mCandidateView.clear();
}
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (mComposing.length() > 0) {
mComposing.setLength(index);
mComposing = new StringBuilder(mCandidateList.get(index) + " ");
commitTyped(getCurrentInputConnection());
}
}
SpannableStringBuilder authorText = new SpannableStringBuilder("");
ImageSpan is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);
for (Author a : mStory.authors) {
if (!TextUtils.isEmpty(a.authorName)) {
String prefix = "";
if (count == 0) {
prefix = "By: ";
} else if (count > 0 && count < mStory.authors.size()-1) {
prefix = ", ";
} else {
prefix = " and ";
}
authorText.append(prefix + a.authorName + " ");
authorText.setSpan(is, authorText.length()-1, authorText.length(), 0);
//authorText.setSpan(is, authorText.length()-2, authorText.length()-1, 0);
//^ I put a second one there just to check is two will populate
count++;
}
So.. It goes through a for loop anyway, but I put 2 setSpan()'s to see if the last iteration would populate 2 images. It only populates an image at the very end of the string. Maybe there is a certain flag i have to put in for setspan to produce multiple?
Try this:
SpannableStringBuilder authorText = new SpannableStringBuilder("");
ImageSpan is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);
for (Author a : mStory.authors) {
if (!TextUtils.isEmpty(a.authorName)) {
String prefix = "";
if (count == 0) {
prefix = "By: ";
} else if (count > 0 && count < mStory.authors.size()-1) {
prefix = ", ";
} else {
prefix = " and ";
}
authorText.append(prefix + a.authorName + " ");
authorText.setSpan(is, authorText.length()-1, authorText.length(), 0);
is = new ImageSpan(getActivity(), R.drawable.ic_birdhead);
authorText.setSpan(is, authorText.length()-2, authorText.length()-1, 0);
//^ I put a second one there just to check is two will populate
count++;
}
I can't understand what is wrong in my code . I am getting error in myAddressUniqueness. don't know why.Before i have tried it with string data type but got the same error. It is saying java.null exception.
ArrayList<String> myAddressUniqueness = null;
String name = "hello";
if (indexBody < 0 || !cursor.moveToFirst())
return;
smsList.clear();
do {
// int cursorPostion = cursor.getPosition();
String address;
String msgStr = cursor.getString(indexBody);
String senderNumber = cursor.getString(indexAddr);
Log.d("Name : ", senderNumber);
// String name = cursor.getString(cursor
// .getColumnIndex(PhoneLookup.DISPLAY_NAME));
// Log.d("Name : ",name);
if (name != null) {
address = name;
} else {
address = senderNumber;
}
Log.d("Address: ", address);
flag = 1;
//Log.e("Number: ", addressUniqueness.length + "");
for (j = 0; j < myAddressUniqueness.size(); j++) {
if (myAddressUniqueness.contains(address)) {
flag = 0;
break;
}
}
if (flag == 1) {
myAddressUniqueness.add(new String(address));
i++;
String str = "Sender: " + address + "\n";
smsList.add(str);
}
// TODO Auto-generated catch block
} while (cursor.moveToNext());
Change your declaration to
ArrayList<String> myAddressUniqueness = new ArrayList<String>();
You have it initialized to null. This will instantiate it then you can add data to it.
You should instantiate myAddressUniqueness by myAddressUniqueness = new ArrayList<String>() before using it.
I have an email project in Android and I can receive and download my mails and its attachments but now I want to learn if a message has an attachment or not, and how many attachments it has got.
I mean I just need to do an if clause like:
if(messages[i].hasAttachment)
{
int numberOfAttachments = messages[i].attachmentNumber;
// do smthng
}
else
{
// do smthng
}
Maybe its help: I am receiving my body part with this code sample. Meanwhile, as per my subject, if the isMimeType = "Multipart/alternative" or "Multipart/*" , does it mean it has an attachment?
public String getText(Part p) throws MessagingException, IOException {
if (p.isMimeType("text/*")) {
boolean textIsHtml = false;
String s = (String) p.getContent();
textIsHtml = p.isMimeType("text/html");
return String.valueOf(s);
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return String.valueOf(s);
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
// fileName = bp.getFileName();
if (s != null)
return String.valueOf(s);
}
}
return null;
}
Here is the code I use to parse attachments. I used to parse with multipart/attachment or multipart/*, and changed to this:
if( mimeMessage.getContent() instanceof Multipart) {
Multipart multipartContent = (Multipart) mimeMessage.getContent();
List<BodyPart> deleteThese = new ArrayList<BodyPart>();
for( int i = 0; i < multipartContent.getCount(); i++ ) {
MimeBodyPart part = (MimeBodyPart) multipartContent.getBodyPart(i);
String disposition = part.getDisposition();
if( disposition != null
&& ( disposition.equalsIgnoreCase( Part.ATTACHMENT )
|| ( disposition.equalsIgnoreCase( Part.INLINE )
&& !part.isMimeType( PLAIN_TEXT_MIME_TYPE )
&& !part.isMimeType( HTML_MIME_TYPE ) )
) ) {
// do something with part
}
}
Essentially if an email has a Multipart there is potential for an attachment, but you have to look at the content disposition to really know. Part.ATTACHMENT would be what you are interested in, and optionally you can ignore or parse the Part.INLINE.