2012-07-05 24 views
0

我有一個簡單的ListActivity使用SimpleCursorAdapter。我允許用戶使用EditText更改其中一個值。我執行簡單的驗證以確保輸入的數字小於100.如果用戶輸入的值未通過驗證,我想將舊值恢復。Android SimpleCursorAdapter getView如何將以前的值返回

我試過幾種不同的方法。我目前的做法是從數據庫中重新找回它,但這不起作用。無論哪一個實際發生了變化,我總是會獲得與ListActivity中最後一項相關的價值。我注意到在LogCatonTextChangedafterTextChanged是在ListActivity中的每一行發射多次,而不僅僅是發生改變的那一行。

下面的代碼:

public class MySimpleCursorAdapter extends SimpleCursorAdapter { 

    Context lcontext; 
    boolean changed; 
    String lastval; 

    private PortfolioData pfdata; 

    public MySimpleCursorAdapter(Context context, int layout, Cursor c, 
     String[] from, int[] to) { 
     super(context, layout, c, from, to); 

     lcontext = context; 
    } 

    @Override 
    public View getView(final int pos, View v, ViewGroup parent) { 

     v = super.getView(pos, v, parent); 
     final EditText et = (EditText) v.findViewById(R.id.classpercentage); 

     final TextView tv = (TextView) v.findViewById(R.id._id); 
     et.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
       Log.d("TEST", "In afterTextChanged s=" + s.toString() + " " 
        + tv.getText() + " POS = " + Integer.toString(pos)); 

       lastval = tv.getText().toString(); 


       if (changed == true) { 
        String enteredValue = s.toString(); 
        if (checkNullValues(enteredValue)) { 
         if (Float.parseFloat(enteredValue.trim()) > 100.0f) { 


          AlertDialog.Builder builder = new AlertDialog.Builder(
           lcontext); 

          builder.setMessage("Percentage Value should be Less than 100"); 

          builder.setPositiveButton("Ok", 
           new DialogInterface.OnClickListener() { 
            public void onClick(
             DialogInterface arg0, int arg1) { 

             String sql = "select c.percentage as PERCENTAGE " + 
             "from asset_classes c WHERE c._id = " + lastval + ";"; 

             pfdata = new PortfolioData(lcontext); 
             SQLiteDatabase db = pfdata.getReadableDatabase(); 

             Cursor cursor = db.rawQuery(sql, null); 

             if (cursor != null) 
             { 
              cursor.moveToFirst(); 

              et.setText(cursor.getString(0)); 
             } 

             cursor.close(); 
             pfdata.close(); 
            } 
           }); 
          // End of the Alert 

          if (changed == true) 
          { 
           builder.show(); 
          } 
         } 
        } 
        changed = false; 
       } 


      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
       // Log.d("TEST", "In beforeTextChanged start=" + 
       // Integer.toString(start) +" count="+ Integer.toString(count) + 
       // " after=" + Integer.toString(after) + " s=" + s + " " + tv); 
      } 

      public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
       Log.d("TEST", "In onTextChanged start=" + 
        Integer.toString(start) + " count=" + Integer.toString(count) 
        + " before=" + Integer.toString(before) + " s=" + s + " " + 
        tv); 
       changed = true; 

      } 
     }); 

     return v; 
    } 
} 

我會很感激這個一個全新的視角。一如既往,先謝謝你。

回答

0

嘗試使用onFocusChangeListener。當它獲得焦點時,將當前文本保存到類的字段中。

喜歡的東西:

String oldText - Would be your old text field.

然後你做:

et.setOnFocusChangeListener(new OnFocusChangeListener()) { 
      @Override 
      public void onFocusChange(View whatever, boolean hasFocus) { 
         if (hasFocus) { 
         //code 
         } else { 
         //code or maybe empty 
         } 
      } 
}

這時如果數量爲> 100,你剛纔得到的oldText值,並把在EditText上。

0

我想你不知道在ListView中回收。在Cursor中有1000行,只有10-20(取決於屏幕大小)行視圖創建。
請勿將數據存儲在視圖中!

在beggining我建議你閱讀http://commonsware.com/Android/excerpt.pdf

+0

你是正確的。我不太瞭解listview的回收和鏈接pdf是非常好的閱讀。這是比我目前擁有的書更好的Android資源。我目前正在修改我發佈的用於回收的部分代碼。我沒有看到的部分是對afterTextChanged,beforeTextChanged或onTextChanged處理程序的引用。 – Clavijo 2012-07-05 22:08:42

+0

TextWatcher只是細節。當你知道這個視圖不是用來存儲數據的時候,你會明白它應該像交易一樣。如果EditText中的數據沒有問題,那麼它將存儲在集合中,否則EditText中的值將被集合中的數據覆蓋。 – pawelzieba 2012-07-06 07:15:09

相關問題