Connection conn = null;
String url = "jdbc:jtds:sqlserver://192.168.1.25:1433/";
String dbName = "Demo;Instance=MSSQLSERVER;";
String userName = "BIT";
String password = "1234";
String driver = "net.sourceforge.jtds.jdbc.Driver";
try {
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ItemDesc,Qty,NetPrice FROM TrxDetail ");
String a ="";
String b ="";
String c ="";
while (rs.next())
{
a += rs.getString("ItemDesc");
b += rs.getString("Qty");
c += rs.getString("NetPrice");
}
tv1.setText(a);
tv2.setText(b);
tv3.setText(c);
conn.close();
}
xml:
Why not all the information.
Just released the final data.
how to show all data.
how to edit xml Correct.
I do not know who to ask.
while (rs.next())
{
tv1.setText(rs.getString("ItemDesc"));
tv2.setText(rs.getString("Qty"));
tv3.setText(rs.getString("NetPrice"));
}
For every result you get, you set the contents of tv1, tv2 and tv3 to the contents of that result, overwriting the previous contents. If this is not what you want ( you didn't actually ask a question, so its hard to say, but I'm guessing your sparse "Why not all the information" means that you are complaining about seeing only the last entry?), you should not overwrite them.
What you can do is make a String, concatenate the new value to the current value, and then call setText once after the while?
Ok, you can do this. You might be better of using a StringBuffer btw, but that would need even more explaining...
String tv1S = "";
String tv2S = "";
String tv3S = "";
while (rs.next()) {
tv1S += rs.getString("ItemDesc"));
tv2S += rs.getString("Qty"));
tv3S += rs.getString("NetPrice"));
}
tv1.setText(tv1S);
tv2.setText(tv2S);
tv3.setText(tv3S);
Now you also will need some spacing and stuff like that, but I'm not going to write all for you, this should be enough to figure it out....
Related
i am building an android app in this I want to get information how to read data from textView and to send an http request i was not able to figure out what is to be done to make it work.
Following is the code that i have used in intent section which will get the data from Edit text .
Intent summeryPage = new Intent(Form_section.this ,summry_page.class);
// ((routernBoltBeam)this.getApplication()).setFirstName(FirstName);
summeryPage.putExtra("fname",fName.getText().toString());
summeryPage.putExtra("lname",lName.getText().toString());
summeryPage.putExtra("date",date.getText().toString());
summeryPage.putExtra("email",emailAddress.getText().toString());
summeryPage.putExtra("mobuleNumber",mobileNumner.getText().toString());
summeryPage.putExtra("adults",adultsNumber.getText().toString());
summeryPage.putExtra("totalChildren",childensNumber.getText().toString());
summeryPage.putExtra("childAge",childrensage.getText().toString());
summeryPage.putExtra("hotelRooms",numberOfhotelRooms.getText().toString());
summeryPage.putExtra("departureCity",departureCity.getText().toString());
summeryPage.putExtra("destination",yourDesiredDestination.getText().toString());
summeryPage.putExtra("days",numberOfDays.getText().toString());
summeryPage.putExtra("Budget",yourBudget.getText().toString());
summeryPage.putExtra("selectHotel",preferHotel);
summeryPage.putExtra("airtickets",airticketPrefer);
summeryPage.putExtra("intercity",vehiclePreferance);
summeryPage.putExtra("travelType",travelPreferances);
summeryPage.putExtra("mealPlan",mealPreferances);
summeryPage.putExtra("addInfo",additionalInformation.getText().toString());
startActivity(summeryPage);
Following is the code which will set the data into the respective textView on other activity
public void showData(){
nameText =(TextView)findViewById(R.id.content);
viewDateText =(TextView)findViewById(R.id.showDate);
viewEmailText=(TextView)findViewById(R.id.showEmail);
viewMobileText=(TextView)findViewById(R.id.showMobileNumber);
viewTotalAdultText = (TextView)findViewById(R.id.showTotalAdults);
ViewTotalChildren =(TextView)findViewById(R.id.showTotalChildrens);
viewChildAge =(TextView)findViewById(R.id.showChildrensAge);
viewTotalRoomsText =(TextView)findViewById(R.id.showTotalRooms);
viewDepartureText =(TextView)findViewById(R.id.showDepartureCity);
viewDeatinationText =(TextView)findViewById(R.id.showDestination);
viewDaysText =(TextView)findViewById(R.id.showTotalDays);
viewBudgetText =(TextView)findViewById(R.id.showBudget);
viewPreferHotelText =(TextView)findViewById(R.id.showHotelPreferance);
viewAirticketText =(TextView)findViewById(R.id.showAirticketRequired);
viewIntercityText = (TextView)findViewById(R.id.showIntercityTravel);
viewTravelTypeText = (TextView)findViewById(R.id.showTraveType);
viewMealText =(TextView)findViewById(R.id.showMealPlan);
viewInfoText = (TextView)findViewById(R.id.showAdditionalInfo);
//String s =((routernBoltBeam)this.getApplication()).getFirstName();
Intent summery = getIntent();
String fName = summery.getStringExtra("fname");
String lName = summery.getStringExtra("lname");
String Date = summery.getStringExtra("date");
String Email = summery.getStringExtra("email");
String Mobile = summery.getStringExtra("mobuleNumber");
String totalAdults = summery.getStringExtra("adults");
String totalChildrens = summery.getStringExtra("totalChildren");
String ChildresnAge = summery.getStringExtra("childAge");
String TotalRooms = summery.getStringExtra("hotelRooms");
String Departure = summery.getStringExtra("departureCity");
String Destination = summery.getStringExtra("destination");
String TotalDays = summery.getStringExtra("days");
String TotalBudget = summery.getStringExtra("Budget");
String PreferHotel = summery.getStringExtra("selectHotel");
String SelectAirticket = summery.getStringExtra("airtickets");
String InterCityText = summery.getStringExtra("intercity");
String travelType= summery.getStringExtra("travelType");
String MealPlan = summery.getStringExtra("mealPlan");
String AddInfo = summery.getStringExtra("addInfo");
nameText.setText(fName +" "+lName);
viewDateText.setText(Date);
viewEmailText.setText(Email);
viewMobileText.setText(Mobile);
viewTotalAdultText.setText(totalAdults);
ViewTotalChildren.setText(totalChildrens);
viewChildAge.setText(ChildresnAge);
viewTotalRoomsText.setText(TotalRooms);
viewDepartureText.setText(Departure);
viewDeatinationText.setText(Destination);
viewDaysText.setText(TotalDays);
viewBudgetText.setText(TotalBudget);
viewPreferHotelText.setText(PreferHotel);
viewAirticketText.setText(SelectAirticket);
viewIntercityText.setText(InterCityText);
viewTravelTypeText.setText(travelType);
viewMealText.setText(MealPlan);
viewInfoText.setText(AddInfo);
}
so now what i want do is to read the data from these TEXTVIEWS and to send an HTTP REQUEST to an link and i am not able to understand how to figure it out
I am very new to android programming and am trying to complete my first app. It is a recipe converter.
I have stored my recipe details in a SQLite DB and the text for ingredients is just one multiline string separated by carriage returns. I have used a cursor to get the ingredient data into a textview which returns text like (could be numerous variants):
100ml Water
500 g Mince
2 x 400g can crushed tomatoes
etc.
I originally had each Qty, Unit and Ingredient Description stored separately in the database which made life easy when converting but I chose to store it in a multiline string to allow copying and pasting of ingredients from the internet or another source.
I am attempting to extract the numbers and then multiply them by a percentage, then return the new converted numbers, and the corresponding unit and description to get something like this:
(multiplied by 200%)
200ml Water
1000g Mince
4 x 400g can crushed tomatoes
I just don't know how to do it though. Can anyone help please?
Thanks
UPDATE:
I have tried to do something like this to get the numbers.
public void Split() {
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
tvSplit.setText("");
String text = s.replaceAll("\\D+", ",");
tvSplit.append(text);
tvSplit.append("\n");
}
That shows me all of the numbers with a "," between them but it also includes all numbers in the string like in the above example prior to conversion it would show 100,500,2,400 when I only need 100,500,2. Then from that point I'm not sure how I would convert them all. My "fresh to programming mind" thought that I could store these in a temp SQL table by INSERT INTO tablename (id, originalvalues) VALUES (my string ie 100,500,2).
I could then pull them back out, do the calculation, update the table, then add them back into my textview with the remaining string. I haven't got that far yet, so I'm just wondering what the correct way to do it is.
UPDATE 2:
As per my comments, this is the code I used to show an alert dialog with each item listed on a separate line, I then used the selected line to find the number before any " " to then display the text on the screen.
public void PopUpSpinnerDialogue() {
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
String s = tvTest.getText().toString();
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, s.split("\n"));
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
}
It is this idea that I think I can use but I don't know how to code it and then display the results.
UPDATE 3:
The code or at least the idea of the code I am trying to use is this.
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
String[] ingreds = s.split("\n");
tvSplit.setText("");
String[] parts = ingreds.split(" ");
String Qty = parts[0];
String Units = parts[1];
String Ingredients = parts[2];
Integer QtyInt = Integer.parseInt(Qty);}
ingreds.split doesn't work and also, I don't know how to specify splitting the parts for each i.
I ended up using regex. It allowed the data to be entered with or without a space. So I ended up using this code to pull out the Qty of each line, multiply it by a percentage, append the text (units,ingredient description) to the line, then add it to a string array, to add to my alert dialog.
Code is here.
public void Split() {
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
TextView tvTest = (TextView) findViewById(R.id.tvTest);
TextView tvPercent = (TextView) findViewById(R.id.tvPercent);
String tvP = tvPercent.getText().toString();
String tvNumOnly = tvP.replaceAll("\\D+", "");
Integer PercentVal = Integer.parseInt(tvNumOnly);
String s = tvTest.getText().toString();
StringBuilder sb = new StringBuilder();
Pattern p = Pattern.compile("((\\d*) ?(.*)(?:\\n*))");
Matcher m = p.matcher(s);
while (m.find()) {
String Qty = m.group(2) + ".00";
String Ingred = m.group(3);
Float QtyFloat = Float.parseFloat(Qty);
Float newQTY = (QtyFloat * PercentVal) / 100;
String newQTYstr = newQTY.toString();
sb.append(newQTYstr + " " + Ingred + "\n");
}
String[] lines = sb.toString().split("\n");
String[] IngredArray = Arrays.copyOfRange(lines, 0, lines.length - 1);
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, IngredArray);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D.\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
// Toast.makeText(SelectConvertMethod.this, sb, Toast.LENGTH_LONG).show();
}
I have not found documentation on how I can get the first letter of a value in a TextView?
Very easy,
String strTextview = textView.getText().toString();
strTextView = strTextView.substring(0,1);
Alternatively you can try following way too
char firstCharacter = textView.getText().toString().charAt(0);
To get the first letter you'll have to make this call:
char firstCharacter = myTextView.getText().charAt(0);
Use the method from below. Provide the string from TextView as the parameter.
public String firstStringer(String s) {
String str= s.substring(0, Math.min(s.length(), 1));
return str;
}
You can use this
TextView tv = (TextView) findViewById(R.id.textview);
String frstLetter = tv.getText().substring(0, 1);
it is simple. To retrieve the Text from the TextView you have to use getText().toString();
String textViewContent = textViewInstance.getText().toString();
and the first letter textViewContent.charAt(0)
To fetch the content of the string from TextView:
String content = textView.getText().toString();
To fetch the first character
char first = content.charAt(0);
Try this
String value = text.getText().toString();
String firstTen = value.substring(0, 1);
As a result of the two for-loops below I should get the word an Album name, but I get this name in reverse order. For example if I expect "LINK", I get "KNIL"... Where am I going wrong?
Here is the code:
jsonobj = new JSONObject(param);
JSONObject datajson = jsonobj.getJSONObject("data");
JSONArray news = datajson.getJSONArray(TAG_NEWS);
JSONArray actual = datajson.getJSONArray("actual");
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String album = c.getString(TAG_ALBUM);
TextView tv = (TextView) findViewById(R.id.imgtest);
//tv.setText(album);
if(!(album == "null")){
String var[] = album.split("|");
for(int a=0;a<var.length;a++){
String t = var[a].intern();
String al = ((TextView) findViewById(R.id.imgtest)).getText().toString();
String b = t+al;
tv.setText(b);
}
}else{
break;
}
}
This line is adding the newest letter t to the beginning of your existing String al:
String b = t+al;
However rather than simply switching this line around (b = al + t), I recommend a faster method:
String album = c.getString(TAG_ALBUM).replaceAll("|", "");
TextView tv = (TextView) findViewById(R.id.imgtest);
tv.setText(album);
I looks like you only wanted to remove the "|" character from your album String, so just use String.replaceAll().
If you need album split into an array, then when you rebuild the String don't waste your time calling findViewById() on a view that you already have. In fact you already have the String, so there is no need to use getText() either:
String var[] = album.split("|");
StringBuilder builder = new StringBuilder();
for(int a=0;a<var.length;a++)
builder.append(a);
// use builder.toString() when you want the cleaned album name.
To add the latest letter t to the end of the current string al:
b = al + t
I have lots of editText fields and the user can add info into them.
from these editTexts i want to create one string. im using the stringBuilder at the moment. however if the user does not enter anything to some of the editTexts, i want the stringbuilder to ignore these fields. is this possible? and if so, how can i do it?
this is what im doing at the moment:
String baseString = editText1.getText().toString();
String string2= editText2.getText().toString();
String string3= editText3.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(baseString);
superStringBuilder.append(string2 + string3);
String superString = superStringBuilder.toString();
thank you
You can do something like:
If (string2.equals("")){
//Then do something when the edit text is blank.
superStringBuilder.append(string3);
} else{
superStringBuilder.append(string2 + string3);
}
Hope that helps.
thanks to your help this is an example for others if they have the same problem.
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5 = editText5.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(string1);
if (string2.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string2);
}
if (string3.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string3);
}
if (string4.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string4);
}
if (string5.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string5);
}
String superString = superStringBuilder.toString();
this will make the string filter out the editText with no text in them :) so the new string created with stringbuilder is either 4 strings or 2 strings :)
thank you