2011-07-04 85 views
0

我創建了一個包含5行信息的屏幕。每行有2個值。 (value1value2) 在每行的末尾是一個按鈕。對於每一行我創建了一個xml文件,我正在循環並填充數據庫中的值幫助創建對話框並將信息傳遞給它

我在看的是當按鈕被按下時打開一個對話框,其中兩個editviews填充值從那一行開始

然後當在對話框上按下按鈕時,數據庫中的值被更新。

我對createdialog/preparedialog方法有問題,並將值傳遞給它們。

更新代碼

public void createLayout(LinearLayout pMainlayout, String pMajorValue) { 

     DatabaseTools db = new DatabaseTools(this); 

     majorValue= pMajorValue; 

     nameInfo = db.getNames(pMajorValue); 
     name = returnName(pMajorValue); 

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     for (int i = 0; i < nameInfo.size(); i++) { 
      // Get views 
      View view = (View) inflater.inflate(R.layout.mainlayout, null); 
      TextView nameView = (TextView) sightmarkView.findViewById(R.id.name_title); 
      TextView Value1View = (TextView) sightmarkView.findViewById(R.id.value1); 
      TextView ValueView = (TextView) sightmarkView.findViewById(R.id.value2); 

      Button updateButton = (Button) sightmarkView.findViewById(R.id.updatebutton); 

     // Get info from Database 
      nameValue = nameInfo.get(i).toString(); 
      value1v = db.getMark(majorValue, nameInfo.get(i).toString()); 
       value2v = db.getMark(majorValue, nameInfo.get(i).toString()); 
      // populate info into fields 
      nameView.setText(nameValue); 
      Value1View.setText(String.valueOf(value1)); 
      Value2View.setText(String.valueOf(value2)); 

      updateButton.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View v) { 
      Bundle bundle = new Bundle(); 
          //Code here to add values to a bundle 

          showDialog(SIGHTMARK_DIALOG_ID, bundle); 

         } 
        }); 

      pMainlayout.addView(view); 
     } 

    } 

所有這一切工作正常。顯示值。我一直看着用束將它們傳遞到對話框

@Override 
    protected Dialog onCreateDialog(int id, Bundle bundle) { 
     switch(id) { 
     case DIALOG_ID: 
      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      final View layout = inflater.inflate(R.layout.update, (ViewGroup) findViewById(R.id.update)); 
      final EditText value1Edit = (EditText) layout.findViewById(R.id.value1_current); 
      final EditText value2Edit = (EditText) layout.findViewById(R.id.value2_current); 

     //next pull values from bundle to populate into fields 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setView(layout); 
      // Now configure the AlertDialog 
      builder.setTitle(R.string.sight_update_title); 
      builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        this.removeDialog(DIALOG_ID); 
       } 
      }); 
      builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

        value1New = Float.parseFloat(value1Edit.getText().toString()); 
        value2New = Float.parseFloat(value2Edit.getText().toString()); 

       // method run to update the database with the new values passed to it 
        updateMarks(value1, value2); 

        this.removeDialog(DIALOG_ID); 
       } 
      }); 
      // Create the AlertDialog and return it 
      AlertDialog Dialog = builder.create(); 
      return Dialog; 
     } 
     return null; 
    } 

這部分有問題,沒有正確獲取值或將它們傳遞到更新方法

+0

你嘗試過什麼到目前爲止?您覺得什麼是不工作?在任何人有機會獲得幫助之前,您需要展示一些代碼和佈局文件。 – RivieraKid

+1

可以在一個全局靜態變量,並指定所需的值,當點擊按鈕,然後你可以使用該值來填充對話框中的編輯視圖... – Dinash

+0

我的問題的主要部分是將這兩個值傳遞到createdislog/preparedislog 。如果需要,我可以提供代碼。目前,我已經正確打開了alertdialog,但其中包含空白值。謝謝 –

回答

0

我認爲這個問題是在這裏:

如果您使用showDialog(int),則活動將首次調用此方法並在此後掛起。通過此方法創建的任何對話框 都會自動保存併爲您恢復爲 ,包括它是否顯示。

刪除引用

removeDialog(SIGHTMARK_DIALOG_ID); 
showDialog(SIGHTMARK_DIALOG_ID, bundle); 

documentation