Android:从嵌入列表的按钮调用对话框
我有一个自定义列表,其中包含两个Android:从嵌入列表的按钮调用对话框,android,list,button,dialog,Android,List,Button,Dialog,我有一个自定义列表,其中包含两个文本视图和两个按钮。我想要一个按钮来更改一个文本视图中显示的数据,我想要另一个按钮来打开一个对话框,解释该行的用途。因此,我需要动态更新对话框内容。据我所知,在列表中插入可单击按钮的唯一方法是在为行充气时在适配器中设置setOnClickListener,但我无法在活动之外创建对话框(我正在强制关闭)。这是我的getView()调用。有什么建议吗 public View getView(final int position, View convertView, V
文本视图
和两个按钮
。我想要一个按钮来更改一个文本视图中显示的数据,我想要另一个按钮来打开一个对话框,解释该行的用途。因此,我需要动态更新对话框
内容。据我所知,在列表中插入可单击按钮的唯一方法是在为行充气时在适配器中设置setOnClickListener
,但我无法在活动
之外创建对话框
(我正在强制关闭)。这是我的getView()
调用。有什么建议吗
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView=mInflater.inflate(R.layout.text_list_item,null);
holder=new ViewHolder();
holder.clear=(Button)convertView.findViewById(R.id.btnClr);
holder.label=(TextView) convertView.findViewById(R.id.textListItemLabel);
holder.value=(TextView) convertView.findViewById(R.id.textListItemValue);
holder.info=(Button)convertView.findViewById(R.id.btnInfo);
holder.group= (RadioGroup)convertView.findViewById(R.id.radiogroup);
holder.r1=(RadioButton)convertView.findViewById(R.id.radio1);
holder.r2=(RadioButton)convertView.findViewById(R.id.radio2);
holder.t1=(ToggleButton)convertView.findViewById(R.id.toggle1);
holder.clear.setOnClickListener(new OnClickListener(){
private int pos= position;
@Override
public void onClick(View v){
holder.value.setText(String.valueOf(pos));
notifyDataSetChanged();//I know there's a problem on here, and I'm working on that... but at least it reacts to the button press.
}
});
holder.info.setOnClickListener(new OnClickListener(){
private int pos= position;
@Override
public void onClick(View v){
Button button = (Button) v;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to display?");
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create();//Ok to here...
builder.show();//Crash
String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
包含适配器的活动无法处理其自身的生命周期-Activity.onPause()
当包含的对象请求此适配器使用对话框覆盖活动时
因此,要处理它,请将所有的AlertDialog
代码移动到一个新方法中的活动,该方法将通过按下按钮来调用
您可以通过使用前面提到的方法名称填充按钮xml定义中属性android:onClick
的参数来实现这一点
就像这里: