1

我正在尝试更改一个编辑视图的值,而另一个编辑视图中有更改,即使程序运行值未传递给编辑视图。值从一个编辑视图传递到另一个编辑视图,然后该值乘以解析值。帮助我的主要活动代码是:

public class Main extends ListActivity {
public double C_webuserDouble;
public double C_cashDouble;
public double C_transferDouble;

public double S_webuserDouble;
public double S_cashDouble;
public double S_transferDouble;

EditText resultTxt ;
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);

    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(int i=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);        

        /** C_webuserDouble=json.getDouble("c_web");
            C_transferDouble= json.getDouble("c_bank");
            C_cashDouble=json.getDouble("c_cash");


            S_webuserDouble=json.getDouble("s_web");
            S_transferDouble= json.getDouble("s_bank");
            S_cashDouble=json.getDouble("s_cash");*/

        }       
    }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);
    resultTxt = (EditText) findViewById(R.id.editText1);
    resultTxt.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

            String text;
            text=resultTxt.getText().toString();
            if(resultTxt.getText().length()>0){
calculateSum(C_webuserDouble,C_cashDouble,C_transferDouble);
                    calculateSunrise(S_webuserDouble,S_cashDouble,S_transferDouble);
            }

}

        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // 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));

    }

 }


}

以下是我的列表占位符代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">    

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="No data"/>

</LinearLayout>

所有代码都相同,但在给出用户输入时会生成以下 logcat 错误

08-29 14:53:01.234: E/AndroidRuntime(2206): Uncaught handler: thread main exiting due to uncaught exception
08-29 14:53:01.245: E/AndroidRuntime(2206): java.lang.NullPointerException
08-29 14:53:01.245: E/AndroidRuntime(2206):     at com.mokshya.jsonnetfoxtab.Main.calculateSum(Main.java:142)
08-29 14:53:01.245: E/AndroidRuntime(2206):     at com.mokshya.jsonnetfoxtab.Main.access$0(Main.java:120)
08-29 14:53:01.245: E/AndroidRuntime(2206):     at com.mokshya.jsonnetfoxtab.Main$1.afterTextChanged(Main.java:101)
4

2 回答 2

0

根据您的编程列表占位符代码应该是这样的

<ListView
android:id="@id/listview1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />

<EditText
android:id="@id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data"/>

或者

<ListView
android:id="@id/listview1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />

<AutoCompleteTextView
android:id="@id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data"/>

在编程中

在这里改变

 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});


ListView listView = ( ListView ) findViewById(R.id.listview1);

// Setting the adapter to the listView
 listView.setAdapter(adapter);

 resultTxt = (EditText) findViewById(R.id.editText1);
 /// and so on
于 2012-08-29T07:31:21.607 回答
0

将您的xml更改为

<TextView     
android:id="@+id/editText1     
android:layout_width="fill_parent"     
android:layout_height="wrap_content"     
android:text="No data"/> 
于 2012-08-29T07:23:27.330 回答