不能一次更新FireBase数据库的孩子

时间:2017-04-22 18:43:35

标签: java android listview firebase

我有一个按钮,当你点击它使EditText可编辑时,第二个按钮将新字符串发送到数据库,数据库将更新的字符串返回到列表视图。

我第一次编辑时工作正常但是如果我想再次编辑它会更新数据库中的值,但是在应用程序中它会一直从新字符串切换到循环中的旧字符串。

this is my FireBase Database structure:(我希望能够更新" _name"例如多次更新)

这是我的适配器:(注意" editTremp"" submitText"按钮)

    public class personalZoneAdapter extends ArrayAdapter<TrempData>  {

    static class ViewHolderItem{
        private TextView  msg , extra,phone ,date_time ,uid ;
        private EditText name ;
        private ImageButton deleteTremp , editTremp , phoneBtn , submitText ;
        private ImageView sideview;
    }


    private int layoutResource;
    private FirebaseAuth firebaseAuth;
    private DatabaseReference mDatabase;

    public personalZoneAdapter(Context context, int layoutResource, ArrayList<TrempData> list) {
        super(context, layoutResource, list);
        this.layoutResource = layoutResource;
        firebaseAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolderItem viewHolder;

        if (view == null) {
            final LayoutInflater Inflater = LayoutInflater.from(getContext());
            view = Inflater.inflate(layoutResource, null);

            viewHolder = new ViewHolderItem();
            viewHolder.name = (EditText) view.findViewById(R.id.user_name);
            viewHolder.phone = (TextView) view.findViewById(R.id.user_phone);
            viewHolder.date_time = (TextView) view.findViewById(R.id.user_date_time);
            viewHolder.msg = (TextView) view.findViewById(R.id.user_msg);
            viewHolder.extra = (TextView) view.findViewById(R.id.user_extra);
            viewHolder.uid = (TextView) view.findViewById(R.id.user_uid);
            viewHolder.deleteTremp = (ImageButton)view.findViewById(R.id.remove);
            viewHolder.editTremp = (ImageButton)view.findViewById(R.id.edit);
            viewHolder.phoneBtn = (ImageButton)view.findViewById(R.id.phone_btn);
            viewHolder.submitText = (ImageButton)view.findViewById(R.id.submitText);
            viewHolder.sideview = (ImageView)view.findViewById(R.id.user_image);

            view.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        final TrempData data = getItem(position);

        //data.setPos(position);
        //int[] androidColors = getContext().getResources().getIntArray(R.array.androidcolors);
        //int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];

        if (data != null) {

            if (viewHolder.name != null & viewHolder.phone != null & viewHolder.msg != null & viewHolder.date_time != null & viewHolder.uid != null) {
                viewHolder.name.setText(data.get_name());
                viewHolder.name.setTag(data._name);
                //name.setTextColor(randomAndroidColor);
                viewHolder.phone.setText(data.get_phone());
                viewHolder.name.setTag(data._phone);
                viewHolder.date_time.setText(data.get_timestamp());
                viewHolder.name.setTag(data._timestamp);
                viewHolder.msg.setText(data.get_from() + "--> " + data.get_to() + ", " + data.get_date() + ", " + data.get_time());
                viewHolder.uid.setText(data.get_uid());
                viewHolder.name.setTag(data._uid);

                viewHolder.editTremp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        viewHolder.submitText.setVisibility(View.VISIBLE);
                        viewHolder.name.setBackground(new ColorDrawable(WHITE));
                        viewHolder.name.setEnabled(true);
                        viewHolder.name.setCursorVisible(true);
                        viewHolder.name.setFocusableInTouchMode(true);
                        viewHolder.name.setInputType(InputType.TYPE_CLASS_TEXT);
                        viewHolder.name.requestFocus();

                    }
                });

                viewHolder.submitText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDatabase.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                    TrempData tremp = getItem(position);
                                    if (ds.getKey().toString().equals(tremp.get_key())) {
                                        viewHolder.name.setEnabled(false);
                                        String new_name = viewHolder.name.getText().toString();
                                        viewHolder.name.setBackground(new ColorDrawable(TRANSPARENT));
                                        viewHolder.submitText.setVisibility(View.GONE);
                                        data.set_name(new_name);
                                        mDatabase.child(ds.getKey().toString()).child("_name").setValue(new_name);
                                        notifyDataSetChanged();
                                        //Toast.makeText(getContext(),"הטרמפ עודכן בהצלחה" + position,Toast.LENGTH_SHORT).show();
                                        break;
                                    }
                                }
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                    }
                });


                viewHolder.phoneBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.get_phone()));
                        getContext().startActivity(i);
                    }
                });

                viewHolder.deleteTremp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        mDatabase.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                    TrempData tremp = getItem(position);
                                    if (ds.getKey().toString().equals(tremp.get_key())) {
                                        mDatabase.child(ds.getKey().toString()).removeValue();
                                        notifyDataSetChanged();
                                        //Toast.makeText(getContext(),"הטרמפ נמחק בהצלחה",Toast.LENGTH_SHORT).show();
                                        break;
                                    }
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                Toast.makeText(getContext(),"ישנה בעיה. אנא נסה שוב",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });


                if(data.get_extras().length() == 0) viewHolder.extra.setVisibility(View.GONE);
                viewHolder.extra.setText("הערות:" + " " + data.get_extras());

                if(firebaseAuth.getCurrentUser().getUid().equals(viewHolder.uid.getText())){
                    viewHolder.deleteTremp.setVisibility(View.VISIBLE);
                    viewHolder.editTremp.setVisibility(View.VISIBLE);
                    viewHolder.phoneBtn.setVisibility(View.GONE);
                    viewHolder.sideview.setColorFilter(Color.rgb(255,164,30));
                }
                else{

                    viewHolder.submitText.setVisibility(View.GONE);
                    viewHolder.deleteTremp.setVisibility(View.GONE);
                    viewHolder.editTremp.setVisibility(View.GONE);
                    viewHolder.phoneBtn.setVisibility(View.VISIBLE);
                    viewHolder. sideview.setColorFilter(Color.rgb(176,176,176));
                }
            }
        }
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

&#39; submitText&#39;设置了一些关键内容。按钮:

viewHolder.submitText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDatabase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                TrempData tremp = getItem(position);
                                if (ds.getKey().toString().equals(tremp.get_key())) {
                                    viewHolder.name.setEnabled(false);
                                    String new_name = viewHolder.name.getText().toString();
                                    viewHolder.name.setBackground(new ColorDrawable(TRANSPARENT));
                                    viewHolder.submitText.setVisibility(View.GONE);
                                    data.set_name(new_name);
                                    mDatabase.child(ds.getKey().toString()).child("_name").setValue(new_name);
                                    notifyDataSetChanged();
                                    //Toast.makeText(getContext(),"הטרמפ עודכן בהצלחה" + position,Toast.LENGTH_SHORT).show();
                                    break;
                                }
                            }
                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }
            });

首先,每次单击按钮时,都会添加一个新的侦听器。以前的听众没有被删除,你现在有两个。理想情况下,您可以在设置阶段的某个位置添加一个侦听器,按钮只会触发更新,侦听器会对此做出反应。

第二件事是这个监听器被添加到mDatabasemDatabase中包含的任何条目的任何更改都将触发此监听器。在听众中还有一行mDatabase......setValue(new_name);。这是奇怪的循环行为的一个方法;听众本身正在改变它正在监听......和循环的变化。

所以你需要做的是重新思考一下流程并分离出不属于听众的东西。在安装过程中添加一次侦听器,并准备好对新数据做出反应并更新UI。该按钮只需要将新数据发送到数据库。