【问题标题】:Android: Calling a dialog from a button imbedded in a listAndroid:从嵌入在列表中的按钮调用对话框
【发布时间】:2010-12-10 12:38:23
【问题描述】:

我有一个自定义列表,其中包含两个 TextViews 和两个 Buttons。我想要一个按钮来更改一个TextView 中显示的数据,我希望另一个button 膨胀Dialog 来解释该行的目的。所以我需要动态更新Dialog 的内容。据我所知,在列表中添加可点击按钮的唯一方法是在为行充气时在适配器中添加setOnClickListener,但我无法在activity 之外创建dialog(我' m 获得强制关闭)。这是我的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;
}

【问题讨论】:

    标签: android list button dialog


    【解决方案1】:

    包含适配器的 Activity 无法处理它自己的生命周期 - Activity.onPause() 当包含对象(例如此适配器)请求使用 Dialog 覆盖 Activity 时。

    因此,要处理它,请将所有AlertDialog 代码移动到新方法内的活动中,该方法将通过按下按钮来调用。

    您可以通过在按钮 xml 的定义中使用前面提到的方法名称填充属性 android:onClick 的参数来做到这一点。

    喜欢这里: http://androidforbeginners.blogspot.com/2010/03/clicking-buttons-in-listview-row.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多