我有一个包含一些值的列表视图。我需要在项目点击的列表视图上生成一个对话框。我的问题是,当单击列表视图对话框中的任何项目时,显示的内容等于列表视图中的项目数。我只想在每个项目上单击一个对话框,其中显示相应的项目详细信息。
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 :(得分:1)
您不需要使用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();
}
}
答案 1 :(得分:0)
试试这个
if(posstion==0){
//showYourDialog have value 0
}else if(posstion==1){
//showYourDilog have value 1
}
希望!它可以帮助你
答案 2 :(得分:0)
不要在onItemClick中使用for循环。 remove for循环,你可以从方法参数中获取视图对象。
parent.getItemAtPosition(position));