2012-02-27 64 views
0

我有一個列表視圖,每行有一個文本字段和edittext字段。我讓他們都在屏幕上戰鬥。當我通過接聽電話,回去等方式恢復活動時,edittext字段中的輸入與最初輸入的內容不匹配。我想知道如何設置onresume或保存的即時狀態來防止這種情況,並確保正確的輸入位於正確的edittext字段中。Listview中的Edittext與錯誤的輸入onresume

這是我正在使用的代碼。

public class editview extends ListActivity { 
    private dbadapter mydbhelper; 
    private PopupWindow pw; 
    public static int editCount; 
    public static ListView listView; 
    public ItemAdapter adapter; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mydbhelper = new dbadapter(this); 
     mydbhelper.open(); 


     View footer = getLayoutInflater().inflate(R.layout.footer_layout, null); 
     ListView listView = getListView(); 
     listView.addFooterView(footer); 
     showResults(); 
     } 

    //Populate view 
    private void showResults(){ 
     Cursor cursor = mydbhelper.getUserWord(); 
     startManagingCursor(cursor); 
     String[] from = new String[] {dbadapter.KEY_USERWORD}; 
     int[] to = new int[] {R.id.textType}; 
     adapter = new ItemAdapter(this, R.layout.edit_row, cursor, 
         from, to); 
      adapter.notifyDataSetChanged(); 
      this.setListAdapter(adapter); 
      editCount = adapter.getCount(); 
      adapter.notifyDataSetChanged(); 
    } 


      //footer button 
      public void onClick(View footer){ 
        final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50); 
        editClickSound.start(); 
        if (ItemAdapter.inputValues.containsValue("")){ 
         Toast.makeText(this, "Please fill in all fields", 1000).show(); 
          }else{ 
          startActivity(new Intent("wanted.pro.madlibs.OUTPUT")); 
           }; 

       } 
... 

      } 
      @Override 
      protected void onResume() { 
       super.onResume(); 
      } 

      @Override 
      protected void onPause() { 

       super.onPause(); 

      } 


     } 
//custom cursor adapter 
class ItemAdapter extends SimpleCursorAdapter { 

    private LayoutInflater mInflater; 
    private Cursor cursor; 
    static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>(); 
    static String oldText; 


    public ItemAdapter(Context context, int layout, Cursor cursor, String[] from, 
      int[] to) { 
     super(context, layout, cursor, from, to); 
     this.cursor = cursor; 
     mInflater = LayoutInflater.from(context); 

    } 


    static class ViewHolder implements TextWatcher { 
     protected TextView text; 
     protected EditText edittext; 
     protected int position; 

     public void afterTextChanged(Editable editable) { 
      Log.e(String.valueOf(position), "Position in array"); 
      inputValues.put(position, editable.toString()); 

     } 
     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 

     } 

     } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 


     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.edit_row, null); 


      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.textType); 
      holder.edittext = (EditText) convertView.findViewById(R.id.editText); 
      holder.edittext.addTextChangedListener(holder); 
      holder.position = position; 
      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 

     } 
     cursor.moveToPosition(position); 
     int label_index = cursor.getColumnIndex("userword"); 
     String label = cursor.getString(label_index); 

     holder.text.setText(label); 
     oldText = inputValues.get(position); 
     holder.edittext.setText(oldText == null ? "" : oldText); 

     return convertView; 

    } 
} 
+0

ListView中的EditText ..是的,我會說忘記它。說真的,這是在android上的越野車。 – 2012-02-27 00:24:55

+0

我使用了一個listview,因爲我需要的edittext的數量是動態的。根據用戶在以前的活動中選擇的內容,我需要4-10的任何地方。 – maebe 2012-02-27 00:29:30

回答

3

首先,ListViews中的EditTexts是一個令人頭疼的問題,以防萬一你開始遇到問題。其次,你似乎並沒有在任何時候保存inputValue字符串。至少你應該序列化onSaveInstanceState()中的值並將它們讀回onCreate()。您不應該將它們存儲在適配器中。你應該有一個適當的「模型」(一個帶有標籤和輸入值的對象)支持適配器。 SimpleCursorAdapter並不適合並行修改數據。