0

我有一個包含一些值的列表視圖。我需要在項目單擊時在列表視圖上生成一個對話框。我的問題是,單擊列表視圖中的任何項目時,對話框顯示等於列表視圖中項目的數量。我只想在每個項目上單擊一個對話框,其中顯示相應的項目詳細信息。列表項單擊偵聽器中的警報對話框

public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    // TODO Auto-generated method stub 


    float due = (float) 0.0; 
if(list != null){ 
     for(int i = 0; i< list.getChildCount();i++){ 
      View vie = list.getChildAt(i); 

     TextView amt = (TextView) vie.findViewById(R.id.amt); 
     TextView alloc = (TextView) vie.findViewById(R.id.alloc); 
     EditText ed = (EditText) vie.findViewById(R.id.edit); 
     String amnt = amt.getText().toString(); 
     String allc = alloc.getText().toString(); 



     // due amount is net amount minus allocation amount 
     due = Float.valueOf(amnt) - Float.valueOf(allc); 


     AlertDialog.Builder alertDial = new AlertDialog.Builder(Collection.this); 
     LayoutInflater inflater=Collection.this.getLayoutInflater(); 
     //this is what I did to added the layout to the alert dialog 
     View layout=inflater.inflate(R.layout.alert_layout,null);  
     alertDial.setView(layout); 
     final TextView dues=(TextView)layout.findViewById(R.id.textViewdue); 
     final EditText received=(EditText)layout.findViewById(R.id.rcvd); 
     dues.setText("Float.toString(due)"); 
     AlertDialog alertDialog = alertDial.create(); 

      // show alert 

      alertDialog.show(); 
    } 
} 
} 

而且我也無法在對話框中的編輯文本中輸入值。請幫幫我。

回答

0

你不需要使用for循環。您點擊的項目爲View view。請嘗試以下代碼

public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    // TODO Auto-generated method stub 


    float due = (float) 0.0; 
if(list != null){ 


     TextView amt = (TextView) view.findViewById(R.id.amt); 
     TextView alloc = (TextView) view.findViewById(R.id.alloc); 
     EditText ed = (EditText) view.findViewById(R.id.edit); 
     String amnt = amt.getText().toString(); 
     String allc = alloc.getText().toString(); 



     // due amount is net amount minus allocation amount 
     due = Float.valueOf(amnt) - Float.valueOf(allc); 


     AlertDialog.Builder alertDial = new AlertDialog.Builder(Collection.this); 
     LayoutInflater inflater=Collection.this.getLayoutInflater(); 
     //this is what I did to added the layout to the alert dialog 
     View layout=inflater.inflate(R.layout.alert_layout,null);  
     alertDial.setView(layout); 
     final TextView dues=(TextView)layout.findViewById(R.id.textViewdue); 
     final EditText received=(EditText)layout.findViewById(R.id.rcvd); 
     dues.setText("Float.toString(due)"); 
     AlertDialog alertDialog = alertDial.create(); 

      // show alert 

      alertDialog.show(); 
    } 
} 
+0

嘿它工作..但爲什麼我不能輸入值來編輯文本?我必須使用編輯文本值更新列表中的一個視圖。請你知道爲什麼嗎? – Bivin

0

試試這個

if(posstion==0){ 
//showYourDialog have value 0 
}else if(posstion==1){ 
//showYourDilog have value 1 
} 

希望!它可以幫助你

0

不要在forItemClick中使用for循環。刪除for循環,你可以從方法參數中獲取視圖對象。

parent.getItemAtPosition(position));