2

私は新しいので、これが非常に単純で、自分で解決する必要があった場合は申し訳ありません。私はそれについて考えるのに数日を費やしました。私は1トンを調査し、ここで他の多くの投稿を検索しましたが、成功しませんでした。

DatePicker、Buttons、EditTextフィールド、およびSpinnersのダイアログがあります。DBに保存されているアイテムからダイアログまで、必要なものすべてを入力できます。しかし、いくつかのEditTextフィールドに数値を入力しようとすると、NumberFormatExceptionがスローされます。値を変数にハードコーディングして保存し、試行で値を取得する試みを非表示にすると、正常に実行されます。値が保存され、ダイアログを再度開くと、適切な領域に値が入力されます。

ここに部分的なコードがあります-

        EditText et = new EditText(getContext()); 
        for(int i=0; i<=etcount; i++){
        placed = -1; //reset for next iteration

        //try to get number from edittext
        et.findViewById(i);
        placed = Integer.parseInt(et.getText().toString());

        awake++;
        /*
        try {
            //et.findViewById(i);
            placed = Integer.parseInt(findViewById(i).toString());
            //placed = Integer.parseInt(et.getText().toString());
        } catch(NumberFormatException nfe) {
           System.out.println("Could not parse " + nfe);
        }*/

すぐにトラブルシューティングしたかったので、tryブロックの原因をコメントアウトしました。

etcount変数は、DBから値を取得する前に、onCreateで-1の値で初期化されます。DBに値が格納されている場合は、1ずつ増加し、コードが呼び出されて、EditTextとSpinnerがレイアウトに動的に追加されます。また、EditTextidはetcountの値に設定されます。これがそのコードです-

//this will be ran when +placement button pressed, or if there are items in db stored
//adds 2 rows to dialog, one for textview to label items, one row for edittext with number
//and spinner with what item it is
private void createTableRow(int numPlaced, int itmPlaced){ 


    etcount++; //used to count how many edittext fields there are so that they can be saved later
    spincount++; //to count how many spinner there are


    tl = (TableLayout)findViewById(R.id.tableLayoutVisit); //the tablelayout name
    //need to create row for textview, then another row for edittext and spinner
    tr1 = new TableRow(this.getContext()); //table row 1, for textview
    tr1.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    TextView tv = new TextView(this.getContext());
    tv.setText("Amount Placed");
    tv.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    tr1.addView(tv); //add textview to tablerow1
    tl.addView(tr1, new TableLayout.LayoutParams( //add row to tablelayout
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


    tr2 = new TableRow(this.getContext()); //tablerow2: edittext and spinner
    tr2.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));        
    EditText et = new EditText(this.getContext());
    et.setId(etcount);
    et.setInputType(InputType.TYPE_CLASS_NUMBER);
    if(numPlaced!=0){ //if being populated from previous visit  
        et.setText(Integer.toString(numPlaced));
        //et.setText("" +numPlaced);
    }

    //need to have listener to read data
    et.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


    Spinner spin = new Spinner(this.getContext());
    spin.setId(spincount);
    spinArray = getContext().getResources().getStringArray(R.array.itemplaced);
    ArrayAdapter adapter = new ArrayAdapter(getContext(),
            android.R.layout.simple_spinner_item, spinArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


    spin.setAdapter(adapter);
    if(itmPlaced!=-1){
        spin.setSelection(itmPlaced); //assign correct value 
    }
    spin.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    tr2.addView(et);
    tr2.addView(spin);


    tl.addView(tr2, new TableLayout.LayoutParams( //add row to tablelayout
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

}

それが新しいアイテムであり、現在DBに保存されていない場合は、(0、-1)を渡します。また、おそらく私がTYPE_CLASS_NUMBERを受け入れるように強制したことに気づいたでしょう。しかし、問題はそこにあるとは思いません。繰り返しますが、値をハードコーディングするとDBに保存され、次に開くと、レイアウトにEditText / Spinner行が動的に作成され、EditTextにDBの値が入力されます。

だから...私が思う最初のセクション、et.findViewById(i)に何か問題があります。または配置=Integer.parseInt(et.getText()。toString());。「etcount」の値は、DBから何も入力されていない場合は-1、DBから何かが入力されるたびに0など(etcount ++)である必要があります。

これが長蛇の列である場合は申し訳ありませんが、具体的にしたかった。これで十分な情報になることを願っています!どんな助けでも素晴らしいでしょう!!! 前もって感謝します。

4

2 に答える 2

0

私の側からのいくつかの提案:1)getTextメソッドが空の文字列""またはnullを与えていないかどうかを確認してみてください。2)EditTextなどを配置します。ローカルインスタンスではなくクラス変数に。例publicclassExampleActivity extends Activity {EditTextet;//代わりにoncreateメソッドに配置します。

} //または、oncreateで定義する場合は、finalにします。

コードによると、forループに問題があるようですfor(int i = 0; i <= etcount; i ++){placed = -1; //次の反復のためにリセット

    //try to get number from edittext  // which edit text you are trying to access. am sure it is  returning null to you.
    et.findViewById(i);
    placed = Integer.parseInt(et.getText().toString());
于 2011-12-23T14:16:05.493 に答える
0

これを試して:

    EditText et = new EditText(this);

    for (int i = 0; i <= etcount; i++) {
        placed = -1;
        et = (EditText) this.findViewById(i);
        placed = Integer.parseInt(et.getText().toString());
        //...
    }

エラーを見つけたようです。setIdメソッドで一意の値を割り当てないというコードの主な問題。あなたの場合、いくつかのeditTextsとスピンは同じIDを持つことができます。これは間違っています。すべての要素には一意の識別子が必要です。これは、コードで最初に修正する必要があるものです。

その後、私のアプローチを使用できます。しかし、私にはこのアプローチに何か問題があるように思われます。美しくはありません)この分野のベストプラクティスを見つけてください。

于 2011-12-23T16:46:55.473 に答える