列表项中的警报对话框单击侦听器
Alert dialog inside list item click listener
我有一个包含一些值的列表视图。我需要在列表视图的项目点击上生成一个对话框。我的问题是,当单击列表视图对话框中的任何项目时,显示的数量等于列表视图中的项目数。我只想在每个项目上单击一个对话框,显示相应的项目详细信息。
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();
}
}
}
而且我无法在对话框内的编辑文本中输入值。请帮助我。
试试这个
if(posstion==0){
//showYourDialog have value 0
}else if(posstion==1){
//showYourDilog have value 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();
}
}
不要在 onItemClick 中使用 for 循环。移除 for 循环,你可以从方法参数中获取视图对象。
parent.getItemAtPosition(position));
我有一个包含一些值的列表视图。我需要在列表视图的项目点击上生成一个对话框。我的问题是,当单击列表视图对话框中的任何项目时,显示的数量等于列表视图中的项目数。我只想在每个项目上单击一个对话框,显示相应的项目详细信息。
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();
}
}
}
而且我无法在对话框内的编辑文本中输入值。请帮助我。
试试这个
if(posstion==0){
//showYourDialog have value 0
}else if(posstion==1){
//showYourDilog have value 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();
}
}
不要在 onItemClick 中使用 for 循环。移除 for 循环,你可以从方法参数中获取视图对象。
parent.getItemAtPosition(position));