在对话框弹出窗口中使用适配器中的网格项作为Android中的视图

时间:2016-05-04 19:28:56

标签: android android-alertdialog android-adapter

我想将弹出对话框的视图设置为网格项。 我在适配器中设置的OnClickListener事件中将此项视为视图。

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        grid = inflater.inflate(R.layout.child_card, null);
        final TextView textView = (TextView) grid.findViewById(R.id.childName);
        //ImageView imageView = (ImageView)grid.findViewById(R.id.childImage);
        textView.setText(children.get(position).getName());

        grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundColor(Color.GREEN);
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setView(v);

                final Dialog dialog = builder.create();
                dialog.show();
                final Handler handler  = new Handler();
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                };

                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        handler.removeCallbacks(runnable);
                    }
                });
                handler.postDelayed(runnable, 3000);
            }
        });
    } else {
        grid = (View) convertView;
    }

    return grid;
}

我收到了这些错误: “指定的孩子已经有了父母。你必须先在孩子的父母身上调用removeView()。”

1 个答案:

答案 0 :(得分:0)

观察: - 当convertView已经可用时,不要创建redudant View对象。 - 数据的设置应该在if-else循环之外。 -Click监听器也应该在if-else循环之外。 - 使用ViewHolder模式初始化视图。 - 在child_card布局中为每个textview和imageview设置xml标记。

例如

<TextView
android:id=""
android:layout_width =""
android:layout_height=""
android:tag="1"
/>

同样适用于ImageView。

试试以下代码。

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if(convertView == null) {
        holder = new ViewHolder;
        convertView = inflater.inflate(R.layout.child_card, null);
        holder.textView = (TextView) convertView.findViewById(R.id.childName);
        //holder.imageView = (ImageView)convertView.findViewById(R.id.childImage);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    } 

    // set views here.
    holder.textView.setText(children.get(position).getName());

    // add click Listener here.
    convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundColor(Color.GREEN);
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setView(v);

                final Dialog dialog = builder.create();
                dialog.show();
                final Handler handler  = new Handler();
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                };

                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        handler.removeCallbacks(runnable);
                    }
                });
                handler.postDelayed(runnable, 3000);
            }
        });

        return convertView;
}

class ViewHolder {
    TextView textView;
    ImageView imageView;
}