Capitalize the first letter - android

i am confused why my code isn't capitalizing my first letter, i am getting the name of the user from the database with all letters lower case.
here is the code:
String email = null;
String name = null;
StringBuilder nameSB = new StringBuilder();
nameSB.append(name);
nameSB.setCharAt(0, Character.toUpperCase(nameSB.charAt(0)));
name = nameSB.toString();
try {
email = ((User) ParseUser.getCurrentUser()).getEmail();
name = ((User) ParseUser.getCurrentUser()).getFirstName() + " " + ((User) ParseUser.getCurrentUser()).getLastName();
} catch (NullPointerException npe) {
}

Just summarising as answer
String email = null;
String name = null;
StringBuilder nameSB = new StringBuilder();
try {
email = ((User) ParseUser.getCurrentUser()).getEmail();
name = ((User) ParseUser.getCurrentUser()).getFirstName() + " " + ((User) ParseUser.getCurrentUser()).getLastName();
nameSB.append(name);
nameSB.setCharAt(0, Character.toUpperCase(nameSB.charAt(0)));
name = nameSB.toString();
} catch (NullPointerException npe) {
}

Related

ArrayList skipping some data while adding data in RecyclerView

Well I am running a loop to get data...it get's all the data of the month June..but when it comes to 19-June-2019 it skips the record and moves further without adding data into ArrayList.
My code
this.connection = createConnection();
Statement stmt = connection.createStatement();
Calendar last_month_data = Calendar.getInstance();
last_month_data.add(Calendar.MONTH, -1);
n=last_month_data.getActualMaximum(Calendar.DAY_OF_MONTH);
String last_month_year = new SimpleDateFormat("MMM-
yyyy").format(last_month_data.getTime());
String month_name = lastMonth.getText().toString();
for (int i = 1; i <= n; i++) {
String date = i + "-" + last_month_year;
ResultSet resultSet = stmt.executeQuery("Select
ATTN_TYPE,TO_CHAR(ATTN_TIME,'HH24:MI'),REMARK from MATTN_MAS
where ATTN_DATE='" + date + "' and Username='" + Username + "'
ORDER BY TRAN_NO DESC");
String Attn_Type;
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
Date d=format.parse(date);
SimpleDateFormat fomat1=new SimpleDateFormat("EEEE");
String weekName=fomat1.format(d);
StringBuffer myweekDate=new StringBuffer(weekName+", "+date);
String weekDate=myweekDate.toString();
if (resultSet.next()) {
while (resultSet.next()) {
Attn_Type = resultSet.getString(1);
String Time = resultSet.getString(2);
String Reason = resultSet.getString(3);
if (Attn_Type.equals("I")) {
String Attn_Type_In = "In";
String Attn_Type_Out = null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type_In,
weekDate, Reason, i, date_no, month_name,Time));
} else{
String Attn_Type_Out = "Out";
String Attn_Type_In = null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type_Out,
weekDate, Reason, i, date_no, month_name,Time));
}
}
}else {
Attn_Type = "Absent";
String out = null;
String Reason=null;
String Time=null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type, weekDate,
Reason, i, date_no, month_name,Time));
}
}
}catch (Exception e){
System.out.println("My Error"+e);
}
}
I want all the data of June from date 1 to date 30 but if there are no records of the given date it should insert Absent in ArrayList, Above code is working fine for all the date but the problem is it is not adding data for 19-Jun-2019 in ArrayList even no error is been shown I am not getting what exactly the problem is please help me out.
Please Check this Screenshot for more details...after Thursday,20 it skips 19, Wednesday and displaying data for Tuesday
Ok I got the answer thanks...
We have to use do....while instead of just while....it was
just skipping single data each time.....bcoz while is not
going to check second time if data is available or not....below is
the code...
.
if (resultSet.next()) {
do {
Attn_Type = resultSet.getString(1);
String Time = resultSet.getString(2);
String Reason = resultSet.getString(3);
if (Attn_Type.equals("I")) {
String Attn_Type_In = "In";
String Attn_Type_Out = null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type_In,
weekDate, Reason, i, date_no, month_name, Time));
} else {
String Attn_Type_Out = "Out";
String Attn_Type_In = null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type_Out,
weekDate, Reason, i, date_no, month_name, Time));
}
} while (resultSet.next());
} else {
Attn_Type = "Absent";
String out = null;
String Reason = null;
String Time = null;
StringBuilder stringBuilder = new StringBuilder("" + i);
String date_no = stringBuilder.toString();
myOptions.add(new Attendance_Data(Attn_Type, weekDate,
Reason, i, date_no, month_name, Time));
}
}
}
..Output of above code is in below image..

Strings mixed up reading from internal storage

I'm using internal storage to store multiple strings entered by the user through multiples edit text.
So the layout is composed of multiples Textviews which correspond to the title of the fields, and multiples Edittexts which correspond to the fields where the user can enter his string.
When the user has finished, he presses the save button and this function is triggered :
public void save(View view) // SAVE
{
File file= null;
String name = editname.getText().toString()+"\n";
String marque = editmarque.getText().toString()+"\n";
String longueur = editlongueur.getText().toString()+"\n";
String largeur = editlargeur.getText().toString()+"\n";
String tirant = edittirant.getText().toString()+"\n";
String immatri = editImmatriculation.getText().toString()+"\n";
String port = editPort.getText().toString()+"\n";
String contact = editContact.getText().toString()+"\n";
String panne = editPanne.getText().toString()+"\n";
String poste = editPoste.getText().toString()+"\n";
String police = editPolice.getText().toString()+"\n";
String assurance = editAssurance.getText().toString();
FileOutputStream fileOutputStream = null;
try {
file = getFilesDir();
fileOutputStream = openFileOutput("Code.txt", Context.MODE_PRIVATE); //MODE PRIVATE
fileOutputStream.write(name.getBytes());
fileOutputStream.write(marque.getBytes());
fileOutputStream.write(longueur.getBytes());
fileOutputStream.write(largeur.getBytes());
fileOutputStream.write(tirant.getBytes());
fileOutputStream.write(immatri.getBytes());
fileOutputStream.write(port.getBytes());
fileOutputStream.write(contact.getBytes());
fileOutputStream.write(panne.getBytes());
fileOutputStream.write(poste.getBytes());
fileOutputStream.write(police.getBytes());
fileOutputStream.write(assurance.getBytes());
Toast.makeText(this, "Saved \n" + "Path --" + file + "\tCode.txt", Toast.LENGTH_SHORT).show();
editname.setText("");
editmarque.setText("");
editlargeur.setText("");
editlongueur.setText("");
edittirant.setText("");
editImmatriculation.setText("");
editPort.setText("");
editContact.setText("");
editPanne.setText("");
editPoste.setText("");
editPolice.setText("");
editAssurance.setText("");
return;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Than, in another file I retrieve this data through another button that triggers this function :
public void load(View view)
{
try {
FileInputStream fileInputStream = openFileInput("Code.txt");
int read = -1;
StringBuffer buffer = new StringBuffer();
while((read =fileInputStream.read())!= -1){
buffer.append((char)read);
}
fileInputStream.close();
String tab[] = buffer.toString().split("\n");
String boatname = tab[0];
String marque = tab[1];
String longueur = tab[2];
String largeur = tab[3];
String tirant = tab[4];
String immatri = tab[5];
String port = tab[6];
String contact = tab[7];
String panne = tab[8];
String poste = tab[9];
String assurance = tab[10];
String police = tab[11];
getboatname.setText(boatname);
getmarque.setText(marque);
getlongueur.setText(longueur);
getlargeur.setText(largeur);
getTirantdeau.setText(tirant);
getImmatriculation.setText(immatri);
getPort.setText(port);
getContact.setText(contact);
getPanne.setText(panne);
getPoste.setText(poste);
getAssurance.setText(assurance);
getPolice.setText(police);
} catch (Exception e) {
e.printStackTrace();
}
}
So in the save function I'm splitting the entered strings with \n, and I save the file to the internal storage, and in the load function I retrieve the strings using an array and splitting with every \n and I set the text with the correct index.
What I don't understand is that the results are all mixed up, the string of the first field is displayed in the last field for example, why ?
You can Make a Single String and write it. Also, use a different separator. You can use StringBuffer for it.
StringBuffer s=new StringBuffer();
s.append(editname.getText().toString());
s.append("##########");
s.append(editmarque.getText().toString());
s.append("##########");
s.append(editlongueur.getText().toString());
s.append("##########");
s.append(editlargeur.getText().toString());
s.append("##########");
s.append(edittirant.getText().toString());
s.append("##########");
s.append(editPort.getText().toString());
s.append("##########");
s.append(editContact.getText().toString());
s.append("##########");
s.append(editPanne.getText().toString());
s.append("##########");
s.append(editPoste.getText().toString());
s.append("##########");
s.append(editPolice.getText().toString());
s.append("##########");
s.append(editAssurance.getText().toString());
s.append("##########");
FileOutputStream fileOutputStream = null;
try {
file = getFilesDir();
fileOutputStream = openFileOutput("Code.txt", Context.MODE_PRIVATE); //MODE PRIVATE
fileOutputStream.write(s.toString().getBytes());
Toast.makeText(this, "Saved \n" + "Path --" + file + "\tCode.txt", Toast.LENGTH_SHORT).show();
editname.setText("");
editmarque.setText("");
editlargeur.setText("");
editlongueur.setText("");
edittirant.setText("");
editImmatriculation.setText("");
editPort.setText("");
editContact.setText("");
editPanne.setText("");
editPoste.setText("");
editPolice.setText("");
editAssurance.setText("");
return;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
When you get FileInputStream split the String with this "##########" vlaue

Android: android:autoLink=“web” does not support query parameters after domains

I have a TextView with autoLink set as
<TextView
android:id="#+id/messageDetail_privateText_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web|phone|email" />
But when I set a text with an url like http://www.test.com?p1=v1&p2=v2 the TextView's autolink doesn't recognize the query parameters after the domain.
I can understand that this kind of URL's doesn't have too much sense, but is there any workaround to this problem?
iOS is recognizing the parameters just fine.
Answering to my own question, what finally worked for me was to check the urls of the string and adding the slash manually. Not the coolest solution in the world but worked in this case.
Below the code:
protected String normalizeURLs(String html)
{
String[] pieces = html.split(" ");
ArrayList<String> textParts = new ArrayList<>();
for(String piece : pieces) {
try {
URL isURL = new URL(piece);
String protocol = isURL.getProtocol();
String host = isURL.getHost();
String query = isURL.getQuery();
String path = isURL.getPath();
String questionMark = "?";
if (path.equals("")) {
path = "/";
}
if (query == null) {
query = "";
questionMark = "";
}
String url = protocol + "://" + host + path + questionMark + query;
textParts.add(url);
} catch (MalformedURLException exception) {
textParts.add(piece);
}
}
String resultString = "";
for (String s : textParts)
{
resultString += s + " ";
}
return resultString;
}

How to parse Contact Vcard File from Sdcard in Android

I am trying to parse Vcard file. here is my code.
public void get_vcf_data(String file) throws VCardException, IOException{
VCardParser parser = new VCardParser();
VDataBuilder builder = new VDataBuilder();
//String file = path;
//read whole file to string
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));
String vcardString = "";
String line;
while ((line = reader.readLine()) != null) {
vcardString += line + "\n";
}
reader.close();
//parse the string
boolean parsed = parser.parse(vcardString, "UTF-8", builder);
if (!parsed) {
throw new VCardException("Could not parse vCard file: " + file);
}
//get all parsed contacts
List<VNode> pimContacts = builder.vNodeList;
//do something for all the contacts
for (VNode contact : pimContacts) {
ArrayList<PropertyNode> props = contact.propList;
//contact name - FN property
String name = null;
String number = null;
String tel = null;
for (PropertyNode prop : props) {
if ("FN".equals(prop.propName)) {
name = prop.propValue;
Contact_name.add(name);
Log.d("Name", name);
//we have the name now
break;
}
}
for (PropertyNode prop : props) {
if ("N".equals(prop.propName)) {
number = prop.propValue;
Contact_number.add(number);
Log.d("Name", number);
//we have the name now
break;
}
}
for (PropertyNode prop : props) {
if(" TEL".equals(prop.propName))
{
tel = prop.propValue;
Contact_tel.add(tel);
Log.d("Name", tel);
}
}
Log.d("Tag", ""+Contact_name.size()+"::"+Contact_number.size()+"::"+Contact_tel.size());
//similarly for other properties (N, ORG, TEL, etc)
//...
System.out.println("Found contact: " + name);
}
}
but facing problem in while loop
while ((line = reader.readLine()) != null) {
vcardString += line + "\n";
}
continuously looping inside a while loop and doesn't exit from loop once it is entered

Read HTML file from assets

I have an html file in assets, aaa.html.
I want to read the contents of html file and replace it from another string.
Is this way is right or is there any other option.
my code:
File f = new File("file:///android_asset/aaa.html");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
But its giving file not found, where as loading in web view loads the file.
InputStream is = getAssets().open("aaa.html");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String str = new String(buffer);
str = str.replace("old string", "new string");
If you want to load file in webview then use this
mWebView.loadUrl("file:///android_asset/myfile.html");
you want to replace content inside Html file tags so the solution class code is here..
public class CardDetail {
public static String newHtmlString = "";
// private Context context;
#SuppressWarnings("rawtypes")
public String getNewHtmlString(String htmlString, HashMap hm) {
try {
StringTokenizer st = new StringTokenizer(htmlString, "##");
CardDetail.newHtmlString = "";
while (st.hasMoreTokens()) {
String token = st.nextToken();
CardDetail.newHtmlString += token;
if (st.hasMoreTokens()) {
String token2 = st.nextToken();
if (token2.equals("NAME") || token2.equals("POSITION") || token2.equals("COMPANY") || token2.equals("PHOTOURL"))
CardDetail.newHtmlString += hm.get(token2);
if (token2.equals("SKYPE_CONTAINER1")
|| token2.equals("TWITTER_CONTAINER1")
|| token2.equals("PHONENUMBER_CONTAINER1")
|| token2.equals("EMAIL_CONTAINER1")
|| token2.equals("ADDRESS_CONTAINER1")) {
String replaceString = st.nextToken();
String tokenMiddle = (String) hm.get(st.nextToken());
if (!tokenMiddle.equals("")) {
replaceString += tokenMiddle;
CardDetail.newHtmlString += replaceString + st.nextToken();
st.nextElement();
} else {
st.nextElement();
st.nextElement();
}
}
}
}
// Log.i("convertedHTMLString", newHtmlString);
return CardDetail.newHtmlString;
// htmlString = "<img src='" + hm.get("PHOTOURL") + "' width=80 height=80>";
// return htmlString;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
#SuppressWarnings("unchecked")
public HashMap<?, ?> getProfileHashMap(JSONObject jsonObject) {
#SuppressWarnings("rawtypes")
HashMap hm = new HashMap();
jsonObject = (new JSONConverterClass()).convertJsonObjectToCardDetail(jsonObject);
try {
hm.put("EMAIL", jsonObject.getString("email"));
hm.put("NAME", jsonObject.getString("firstname") + " " + jsonObject.getString("lastname"));
hm.put("COMPANY", jsonObject.getString("company_name"));
hm.put("POSITION", jsonObject.getString("position"));
hm.put("WEBSITE", jsonObject.getString("website"));
hm.put("PHONENUMBER", jsonObject.getString("phonenumber"));
hm.put("PHOTOURL", jsonObject.getString("picture_url"));
hm.put("SKYPE", jsonObject.getString("skype_username"));
hm.put("TWITTER", jsonObject.getString("twitter_username"));
hm.put("ADDRESS", jsonObject.getString("generic_location"));
} catch (Exception e) {
e.printStackTrace();
}
return hm;
}
}
convertJsonObjectToCardDetail this class just replace string with values from Json
hope this solves your problem ....

Categories

Resources