Store EditText values in android - android

I developed an android application... in which... users name last name date of birth, mobile number, email id everything is collected... I wanted to store these details... and when user wants these data's back... a button is there called recall... when user press recall button .. the place where this data's is stored can be displayed.. and he can select his name from this names... any one know how this data can be saved....I am giving the code...here ...anyone pls.. help.. the code is working.... no problem in it...
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3)
{
String date=Integer.toString(arg3);
String month=Integer.toString(arg2);
String year=Integer.toString(arg1);
tv1.setText(date+ month+1 +year);
tv2.setText(month+1 + year);
tv3.setText(year);
tv4.setText(month+1);
tv5.setText(date);
tv6.setText(date + year);
}
});}
public void sum(View v)
{
long sum1=0;
long sum2=0;
long sum3=0;
long sum4=0;
long sum5=0;
long sum6=0;
long sum7=0;
long sum8=0;
long sum=0;
long sum11=0;
long sum12=0;
long sum13=0;
long sum14=0;
EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et4 = (EditText) findViewById (R.id.editText4);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum2=getSum1(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum3=getSum2(tv1.getText().toString());
/*sum4=getSum3(tv2.getText().toString());*/
sum5=getSum4(tv3.getText().toString());
sum6=getSum5(tv4.getText().toString());
sum8=getSum7(tv5.getText().toString());
String yearf = tv3.getText().toString();
int yearb = Integer.valueOf(yearf);
int val = 2013 - yearb;
long val1 = sum3;
/*int val2 = Integer.valueOf(val1);*/
long val3 = 36 - val1;
int val4 = 9;
long val5 = val3 + val4;
long val6 = val5 + val4;
long val7 = val6 + 50;
sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());
if (val <= val3)
{
sum4=getSum3(tv2.getText().toString());
}
else if ((val <= val5) && (val > val3))
{
sum4=getSum8(tv6.getText().toString());
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
/*sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());*/
sum4=getSum9(Integer.toString((int) sum));
}
else
{
sum4=getSum9(Integer.toString((int) sum));
}
if (val <= val3)
{
if (sum5>sum6)
{
sum7 = (sum5 - sum6);
}
else
{
sum7 = (sum6 - sum5);
}
}
else if ((val <= val5) && (val > val3))
{
if(sum5>sum8)
{
sum7=(sum5 - sum8);
}
else
{
sum7 = (sum8 - sum5);
}
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum5);
sum14 = (sum5 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
else
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum5);
sum14 = (sum5 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
String edtxt1 = et1.getText().toString();
String edtxt4 = et4.getText().toString();
if(edtxt1.equals(""))
{
Toast.makeText(getApplicationContext(),"First Name should not be left blank, Please enter your First Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt4.equals(""))
{
Toast.makeText(getApplicationContext(),"Last Name should not be left blank, Please enter your Last Name ", Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(this, result.class);
i.putExtra("name", sum1 + "");
i.putExtra("name1", sum2 + "");
i.putExtra("name2", sum3 + "");
i.putExtra("name3", sum4 + "");
i.putExtra("name4", sum7 + "");
startActivity(i);
}

You should have a look at Storage Options. But if this isn't for a lot of users you can use SharedPreferences. That link has a good example of getting started using them.
If you are going to have multiple users with that much different pieces of data then you may want to look at using SQLite DB. If you aren't used to using SQLite Db then this tutorial might be a good one to get you started.
You also could use the other storage options in the link I provided but a DB would probably be the best with storing a lot of different information for different users.

Have you considered using SharedPreferences?
You can store any variable with the SharedPreferences and it's super easy!
Here's a quick tutorial
SharedPreferences prefs = this.getSharedPreferences(
getApplicationContext(), Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

Related

java: Convert Location into String and back

For android Room, i need a type converter to save Location objects. Converting Locations to String is rather simple, e.g.:
public static String locationStringFromLocation(final Location location) {
if (location==null) return "Location was null";
return Location.convert(location.getLatitude(), Location.FORMAT_DEGREES) + " " + Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
}
But the other way around seems less obvious. I could append all the properties and separate them with a delimiter, but that seems not quite elegant. Any ideas would be appreciated.
My current workaround is to just save latitude and longitude, but I'd prefer to have the object.
public static Location locationFromString(String location){
if (location != null && (location.contains(","))) {
Location result = new Location("Generated_location");
String[] locationStrings = location.split(",");
if (locationStrings.length == 2) {
result.setLatitude(Double.parseDouble(locationStrings[0]));
result.setLongitude((Double.parseDouble(locationStrings[1])));
return result;
} else { return null; }
} else return null;
}
static Location parseLocationString(String locString){
if(locString != null && locString.contains("Location")) {
Location location = new Location("");
String rep = locString.replace("Location", "")
.replace("[", "")
.replace("]", "");
String[] k = rep.split(" ");
for (int part = 0; part < k.length; part++) {
String string = k[part];
if(part == 0)location.setProvider(string);
else if (part == 1){
String[] latlng = string.split(",");
if(latlng.length > 1){
location.setLatitude(Double.parseDouble(latlng[0]));
location.setLongitude(Double.parseDouble(latlng[1]));
}
}
else if (part > 1){
String[] locationDetails = string.split("=");
if (locationDetails.length > 1){
String locationKey = locationDetails[0];
String locationValue = locationDetails[1];
if (locationKey.contains("hAcc")) {
if (!locationValue.contains("?"))
location.setAccuracy((float)Float.parseFloat(locationValue));
}else if (locationKey.contains("et")){
if( !locationValue.contains("?") )
if(Build.VERSION.SDK_INT >= 17) {
if(locationValue.contains("+")) {
long days = 0, hours, min, sec, mill;
//separate string value
String nmbr = locationValue.split("\\+")[1];
String[] hr;
//isolate each value
String[] d = nmbr.split("d");
if(d.length > 1){
days = Long.parseLong(d[0]);
}
if(d.length > 1)
hr = d[1].split("h");
else
hr = nmbr.split("h");
if(hr.length > 1) {
hours = Long.parseLong(hr[0]);
}else hours = 0;
if(days > 0)
hours += 24*days;
String[] m;
if(hr.length > 1)
m = hr[1].split("m");
else
m = hr[0].split("m");
if(m.length > 1) {
if(m[0].length() > 0)
try {
min = Long.parseLong(m[0]);
}catch(NumberFormatException e){
min = 0;
}
else min = 0;
} else min= 0;
String[] s = m[0].split("s");
if(s.length > 1) {
sec = Long.parseLong(s[0]);
}else sec = 0;
if(min != 0 || hours != 0) {
String[] ms = s[0].split("ms");
if (ms.length >= 0 && !ms[0].contains("s")) {
mill = Long.parseLong(ms[0]);
} else mill = 0;
}else{
String ms = s[1];
if (ms.length() >= 0 && !ms.contains("s")) {
mill = Long.parseLong(ms);
} else mill = 0;
}
long millInNano = 1000000;
long nano = mill * millInNano;
nano += (sec * 1000) * millInNano;
nano += ((min * 60) * 1000) * millInNano;
nano += (((hours * 60) * 60) * 1000) * millInNano;
location.setElapsedRealtimeNanos(nano);
}
}
}else if (locationKey.contains("vel")) {
if (!locationValue.contains("?"))
location.setSpeed(Float.parseFloat(locationValue));
}else if (locationKey.contains("vAcc")) {
if (!locationValue.contains("?"))
if(Build.VERSION.SDK_INT >= 26)
location.setVerticalAccuracyMeters(Float.parseFloat(locationValue));
}else if (locationKey.contains("sAcc")) {
if (!locationValue.contains("?"))
if(Build.VERSION.SDK_INT >= 26)
location.setSpeedAccuracyMetersPerSecond(Float.parseFloat(locationValue));
}else if (locationKey.contains("bAcc")) {
if (!locationValue.contains("?"))
if(Build.VERSION.SDK_INT >= 26)
location.setBearingAccuracyDegrees(Float.parseFloat(locationValue));
}
}
}
}
return location;
}else{
return null;
}
}

Cart item Count increment/decrement & Sum of cart price

I need to increment / decrement of cart product.Valuse are increased / decreased.But from here I need to get Total sum of my all cart product. Got some calculation mistakes.So please help me to fix it.
cartList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, View itemView, final int position, final long rowId) {
txt_cartProduct = (TextView) itemView.findViewById(R.id.cartProduct);
txt_cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity);
txt_cartPrice = (TextView) itemView.findViewById(R.id.cartPrice);
txt_cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum);
txt_cartCount = (TextView) itemView.findViewById(R.id.cartCount);
img_ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease);
img_ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease);
but_addTowish = (Button) itemView.findViewById(R.id.addTowish);
but_remove = (Button) itemView.findViewById(R.id.remove);
img_ivIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter++;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum += price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
netAmount = sum;
Log.e("Sum is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
int count_check = 1;
if (count_check >= position) {
count_check++;
} else if (count_check == 0) {
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
}
}
});
img_ivDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter--;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum -= price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
Log.e("Sum - is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
}
}
}
});
}
});

The method notifyDataSetChanged() not working in custom Listview

I custom listview with adapter extend BaseAdapter.
In method getView I have a button Add, and I set listener to it
item.btnAddBasket.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showChooseSize(products.get(pos), pos);
DialogFragmentCustom.setPosition(pos);
}
});
And I have two method, which are call when I press button Add.
public void showChooseSize(ProductInfo productInfo, int pos) {
ArrayList<Stock> stocks = new ArrayList<Stock>();
stocks.addAll(productInfo.getStock());
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> sizes = new ArrayList<String>();
if (stocks != null && stocks.size() > 0) {
for (int i = 0; i < stocks.size() - 1; i++) {
colors.add(stocks.get(i).getColorName());
sizes.add(stocks.get(i).getSize());
}
}
if (sizes != null && sizes.size() > 0) {
HashSet<String> hs_size = new HashSet<String>(); // remove elements
// duplicate
hs_size.addAll(sizes);
sizes.clear();
sizes.addAll(hs_size);
if (sizes.size() == 1) {
size = sizes.get(0);
} else {
for (int j = 0; j < sizes.size() - 1; j++) {
size = size + sizes.get(j) + ", ";
}
size = size + sizes.get(sizes.size() - 1);
}
a_sizes = null;
if (size.split(",") != null) {
a_sizes = size.split(",");
}
if (a_sizes == null) {
a_sizes[0] = "This product do not have size";
}
if (a_sizes != null && a_sizes.length > 0) {
Config.isWishList = true;
((Activity) context).setTheme(R.style.ActionSheetStyleIOS7);
ActionSheetChooseSize.setProductInfo(productInfo);
ActionSheetChooseSize.createBuilder(context, ((FragmentActivity) context).getSupportFragmentManager()).setOtherButtonTitles(a_sizes)
.setCancelableOnTouchOutside(true).setListener(this).show();
isShowChooseSize = true;
} else {
addToBag(productInfo, pos);
}
}
Method add product into Bag when press button Add, it inside my adapter
public void addToBag(ProductInfo productInfo, int pos) {
// Config.productSaleInfos.add(products.get(pos));
Config.productSaleInfos.add(productInfo);
Config.numProductSale = Config.productSaleInfos.size();
Config.numProductWishlist = Config.numProductWishlist - 1 < 0 ? Config.numProductWishlist - 1 : 0;
//SlideMenuFragment.setNumWish(Config.numProductWishlist + "");
new DeletionItemWishlist(context).execute(products.get(pos));
products.remove(pos);
notifyDataSetChanged();
float sumaryPrice = 0;
for (int i = 0; i < products.size(); i++) {
String price = products.get(i).getSalePrice();
int index = price.indexOf(",");
if (index > -1) {
price = price.replace(",", "");
}
float p = Functions.getFloatFromString(price);
sumaryPrice = sumaryPrice + p;
}
basket.setText(Config.numProductSale + "");
subtotal.setText(Html.fromHtml("<b>Subtotal: </b>" + "<font color=\"#FF7F00\">" + sumaryPrice + " ฿</font>"));
Functions.showLog(context, "add bag success!");
}
But notifyDataSetChanged(); is not working in my code. Can you help me please?

Android data storage and retrieving

I developed an android application in which a users provides 6 pieces of information in 6 separate text boxes (information such as first name, last name, middle name, mobile number email id etc)
When the user presses a Save button, I need to store this information and be able to recall this information when the Recall button is pressed.
What is the next step to accomplish this
My code so far is below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),new OnDateChangedListener()
{
#Override
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
String date=Integer.toString(arg3);
String month=Integer.toString(arg2);
String year=Integer.toString(arg1);
tv1.setText(date+ month+1 +year);
tv2.setText(month+1 + date);
tv3.setText(year);
tv4.setText(month+1);
tv5.setText(date);
tv6.setText(date + year);
}
});}
public void sum(View v)
{
long sum1=0;
long sum2=0;
long sum3=0;
long sum4=0;
long sum5=0;
long sum6=0;
long sum7=0;
long sum8=0;
long sum=0;
long sum11=0;
long sum12=0;
long sum13=0;
long sum14=0;
long sum100=0;
EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et4 = (EditText) findViewById (R.id.editText4);
EditText et5 = (EditText) findViewById (R.id.editText5);
EditText et6 = (EditText) findViewById (R.id.editText6);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum2=getSum1(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum3=getSum2(tv1.getText().toString());
/*sum4=getSum3(tv2.getText().toString());*/
sum5=getSum4(tv3.getText().toString());
sum6=getSum5(tv4.getText().toString());
sum8=getSum7(tv5.getText().toString());
String yearf = tv3.getText().toString();
int yearb = Integer.valueOf(yearf);
int val = 2013 - yearb;
long val1 = sum3;
/*int val2 = Integer.valueOf(val1);*/
long val3 = 36 - val1;
int val4 = 9;
long val5 = val3 + val4;
long val6 = val5 + val4;
long val7 = val6 + 50;
sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());
sum100 = getSum4(tv3.getText().toString()) + getSum5(tv4.getText().toString());
if (val <= val3)
{
sum4=getSum3(tv2.getText().toString());
}
else if ((val <= val5) && (val > val3))
{
sum4=getSum8(tv6.getText().toString());
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
/*sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());*/
sum4=getSum9(Integer.toString((int) sum));
}
else
{
sum4=getSum9(Integer.toString((int) sum100));
}
if (val <= val3)
{
if (sum8>sum6)
{
sum7 = (sum8 - sum6);
}
else
{
sum7 = (sum6 - sum8);
}
}
else if ((val <= val5) && (val > val3))
{
if(sum5>sum8)
{
sum7=(sum5 - sum8);
}
else
{
sum7 = (sum8 - sum5);
}
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum8);
sum14 = (sum8 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
else
{
if (sum5>sum6)
{
sum7 = (sum5 - sum6);
}
else
{
sum7 = (sum6 - sum5);
}
}
String edtxt1 = et1.getText().toString();
String edtxt4 = et4.getText().toString();
String edtxt5 = et5.getText().toString();
String edtxt6 = et6.getText().toString();
if(edtxt1.equals(""))
{
Toast.makeText(getApplicationContext(),"First Name should not be left blank, Please enter your First Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt4.equals(""))
{
Toast.makeText(getApplicationContext(),"Last Name should not be left blank, Please enter your Last Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt5.equals(""))
{
Toast.makeText(getApplicationContext(),"Mobile Number should not be left blank, Please enter your Mobile Number ", Toast.LENGTH_LONG).show();
}
else if(edtxt6.equals(""))
{
Toast.makeText(getApplicationContext(),"E-Mail id should not be left blank, Please enter your E-Mail id ", Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(this, result.class);
i.putExtra("name", sum1 + "");
i.putExtra("name1", sum2 + "");
i.putExtra("name2", sum3 + "");
i.putExtra("name3", sum4 + "");
i.putExtra("name4", sum7 + "");
startActivity(i);
}
public long getSum(String text)
{
// TODO Auto-generated method stub
long sum1 = 0;
char[] name = new char[text.length()];
name = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum1 += value( name[i] );
}
while (sum1>9)
{
sum1 = findDigitSum(sum1);
}
return sum1;
}
public long getSum1(String text)
{
// TODO Auto-generated method stub
long sum2 = 0;
char[] name1 = new char[text.length()];
name1 = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum2 += value1( name1[i] );
}
while (sum2>9)
{
sum2 = findDigitSum(sum2);
}
return sum2;
}
db = openOrCreateDatabase("MyDBI", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS TABLE1 ();");
You can create database using this code
db.execSQL("INSERT INTO TABLE1 VALUES('"+fname"');
And values can be inserted to this database using this code.
A tablelayout can be created and the saved details can be displayed in this tablelayout. Give clickable option and clicking all the details can be transferred using intents to the previous activity.

JSON remove special characters

I want to do the replication between Android sqlite & MS SQL server.That Time i want to take Tables values from Databse.
This is my JSON
{
"Table1":[
{
"BusinessUnit":"MASS",
"ProductCode":"SLD0201",
"Description":"Lou Difan C.Blue 12"3- Commode",
"Description2":"301 0201"
},
{
"BusinessUnit":"MASS",
"ProductCode":"SLN0502",
"Description":"Lou Napoli I"vory- Cistern",
"Description2":"2011 0502"
},
{
"BusinessUnit":"MASS",
"ProductCode":"LDMBL6H",
"Description":"Dortek Taper Bullet Handle 6"5 serr ",
"Description2":"Taper Bullet Ha"
}
],
"Table2":[
{
"chk":6,
"currentchk":1
}
]
}
In Here JSON Description column value contain "(double quotation) .If we check http://jsonformatter.curiousconcept.com/ , it show error.Its a Invalid JSON.
WCF service I have converted DataSet to JSON. Some table column contain special charters.
I converted like this :
public String ConverTableToJson(DataSet dsDownloadJson,int currentSplit)
{
StringBuilder Sb = new StringBuilder();
String result = "";
int start = 0;
int end =500;
int chk = 0;
int currentChk = currentSplit;
if (dsDownloadJson.Tables.Count > 0)
{
Sb.Append("{");
foreach (DataTable dt in dsDownloadJson.Tables)
{
DataTable dtDownloadJson = dt;
string[] StrDc = new string[dtDownloadJson.Columns.Count];
string HeadStr = string.Empty;
double total = dtDownloadJson.Rows.Count;
Console.WriteLine("--1--" + dtDownloadJson.Rows.Count);
if (dtDownloadJson.Rows.Count < 500)
{
end = dtDownloadJson.Rows.Count;
}
if (chk == 0)
{
if (dtDownloadJson.Rows.Count > 500)
{
if ((dtDownloadJson.Rows.Count / 500) == 0)
{
chk = dtDownloadJson.Rows.Count / 500;
}
else
{
chk = dtDownloadJson.Rows.Count / 500 + 1;
}
}
else
{
chk = 1;
}
currentChk = 1;
}
else
{
currentChk = currentChk + 1;
start = currentChk * 500;
end = start + 500;
currentChk = chk;
}
Sb.Append("\"" + dtDownloadJson.TableName + "1\" : [");
if (dtDownloadJson.Rows.Count > 0)
{
for (int i = 0; i < dtDownloadJson.Columns.Count; i++)
{
StrDc[i] = dtDownloadJson.Columns[i].Caption;
HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
}
if (HeadStr.Length > 0)
{
HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
Console.WriteLine("--2--" + start);
Console.WriteLine("--3--" + end);
for (int i = start; i < end; i++)
{
string TempStr = HeadStr;
Sb.Append("{");
for (int j = 0; j < dtDownloadJson.Columns.Count; j++)
{
TempStr = TempStr.Replace(dtDownloadJson.Columns[j] + j.ToString() + "¾", dtDownloadJson.Rows[i][j].ToString());
TempStr = TempStr.Replace(""", '\"');
}
Sb.Append(TempStr + "},");
}
Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
}
}
else
{
}
Sb.Append("],");
if (chk > 1)
{
Sb.Append("\"Table2\": [{\"chk\": " + chk + ", \"currentchk\": " + currentChk + " }]");
}
else
{
Sb.Append("\"Table2\": [{\"chk\": " + chk + ", \"currentchk\": " + currentChk + " }]");
}
}
// Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
Sb.Append("}");
return Sb.ToString();
}
else
{
return "0";
}
}
My problem is removing special charters OR How to allow special characters.?
Please help me anybody...
You shouldn't use a StringBuilder to convert an object to a JSON string. Use the JsonConverter class in JayRock JSON library and it takes care of Serialising/Deserialising Json for you (including escaping)
try to use inbuilt json serialization
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal;
}

Categories

Resources