changing value of a textview while change in other textview by multiplying
- by sur007
Here I am getting parsed data from a URL and now I am trying to change the value of parse data to users only dynamically on an text view and my code is
package com.mokshya.jsontutorial;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.mokshya.jsontutorialhos.xmltest.R;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends ListActivity {
EditText resultTxt;
public double C_webuserDouble;
public double C_cashDouble;
public double C_transferDouble;
public double S_webuserDouble;
public double S_cashDouble;
public double S_transferDouble;
TextView cashTxtView;
TextView webuserTxtView;
TextView transferTxtView;
TextView S_cashTxtView;
TextView S_webuserTxtView;
TextView S_transferTxtView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
cashTxtView = (TextView) findViewById(R.id.cashTxtView);
webuserTxtView = (TextView) findViewById(R.id.webuserTxtView);
transferTxtView = (TextView) findViewById(R.id.transferTxtView);
S_cashTxtView = (TextView) findViewById(R.id.S_cashTxtView);
S_webuserTxtView = (TextView) findViewById(R.id.S_webuserTxtView);
S_transferTxtView = (TextView) findViewById(R.id.S_transferTxtView);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions
.getJSONfromURL("http://ldsclient.com/ftp/strtojson.php");
try {
JSONArray netfoxlimited = json.getJSONArray("netfoxlimited");
for (inti = 0; i < netfoxlimited.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = netfoxlimited.getJSONObject(i);
map.put("date", e.getString("date"));
map.put("c_web", e.getString("c_web"));
map.put("c_bank", e.getString("c_bank"));
map.put("c_cash", e.getString("c_cash"));
map.put("s_web", e.getString("s_web"));
map.put("s_bank", e.getString("s_bank"));
map.put("s_cash", e.getString("s_cash"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[] { "date", "c_web", "c_bank", "c_cash", "s_web",
"s_bank", "s_cash", }, new int[] { R.id.item_title,
R.id.webuserTxtView, R.id.transferTxtView,
R.id.cashTxtView, R.id.S_webuserTxtView,
R.id.S_transferTxtView, R.id.S_cashTxtView });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
Toast.makeText(Main.this,
"ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_SHORT).show();
}
});
resultTxt = (EditText) findViewById(R.id.editText1);
resultTxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
resultTxt.setText("");
}
});
resultTxt.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text;
text = resultTxt.getText().toString();
if (resultTxt.getText().length() > 5) {
calculateSum(C_webuserDouble, C_cashDouble,
C_transferDouble);
calculateSunrise(S_webuserDouble, S_cashDouble,
S_transferDouble);
} else {
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
}
private void calculateSum(Double webuserDouble, Double cashDouble,
Double transferDouble) {
String Qty;
Qty = resultTxt.getText().toString();
if (Qty.length() > 0) {
double QtyValue = Double.parseDouble(Qty);
double cashResult;
double webuserResult;
double transferResult;
cashResult = cashDouble * QtyValue;
webuserResult = webuserDouble * QtyValue;
transferResult = transferDouble * QtyValue;
DecimalFormat df = new DecimalFormat("#.##");
String cashResultStr = df.format(cashResult);
String webuserResultStr = df.format(webuserResult);
String transferResultStr = df.format(transferResult);
cashTxtView.setText(String.valueOf(cashResultStr));
webuserTxtView.setText(String.valueOf(webuserResultStr));
transferTxtView.setText(String.valueOf(transferResultStr));
// cashTxtView.setFilters(new InputFilter[] {new
// DecimalDigitsInputFilter(2)});
}
if (Qty.length() == 0) {
cashTxtView.setText(String.valueOf(cashDouble));
webuserTxtView.setText(String.valueOf(webuserDouble));
transferTxtView.setText(String.valueOf(transferDouble));
}
}
private void calculateSunrise(Double webuserDouble, Double cashDouble,
Double transferDouble) {
String Qty;
Qty = resultTxt.getText().toString();
if (Qty.length() > 0) {
double QtyValue = Double.parseDouble(Qty);
double cashResult;
double webuserResult;
double transferResult;
cashResult = cashDouble * QtyValue;
webuserResult = webuserDouble * QtyValue;
transferResult = transferDouble * QtyValue;
DecimalFormat df = new DecimalFormat("#.##");
String cashResultStr = df.format(cashResult);
String webuserResultStr = df.format(webuserResult);
String transferResultStr = df.format(transferResult);
S_cashTxtView.setText(String.valueOf(cashResultStr));
S_webuserTxtView.setText(String.valueOf(webuserResultStr));
S_transferTxtView.setText(String.valueOf(transferResultStr));
}
if (Qty.length() == 0) {
S_cashTxtView.setText(String.valueOf(cashDouble));
S_webuserTxtView.setText(String.valueOf(webuserDouble));
S_transferTxtView.setText(String.valueOf(transferDouble));
}
}
}
and I am getting following error on logcat
08-28 15:04:12.839: E/AndroidRuntime(584): Uncaught handler: thread main exiting due to uncaught exception
08-28 15:04:12.848: E/AndroidRuntime(584): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.jsontutorialhos.xmltest/com.mokshya.jsontutorial.Main}: java.lang.NullPointerException
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.os.Looper.loop(Looper.java:123)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-28 15:04:12.848: E/AndroidRuntime(584): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 15:04:12.848: E/AndroidRuntime(584): at java.lang.reflect.Method.invoke(Method.java:521)
08-28 15:04:12.848: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-28 15:04:12.848: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-28 15:04:12.848: E/AndroidRuntime(584): at dalvik.system.NativeStart.main(Native Method)
08-28 15:04:12.848: E/AndroidRuntime(584): Caused by: java.lang.NullPointerException
08-28 15:04:12.848: E/AndroidRuntime(584): at com.mokshya.jsontutorial.Main.onCreate(Main.java:111)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)