ListView在Android中,当Firebase数据库更新时,数值重复复制数值。

huangapple go评论66阅读模式
英文:

ListView Value duplicating the value when the firebase database is updated in Android

问题

以下是你提供的代码的翻译部分:

ListOfFaculty

public class ListOfFaculty extends Fragment {

    DatabaseReference ref;
    ListView listview;
    List<String> store;
    List<String> UserID;
    String[] faculty_list;
    String a;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ref = FirebaseDatabase.getInstance().getReference("Faculty");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View root = inflater.inflate(R.layout.fragment_list_of_faculty, container, false);

        listview = root.findViewById(R.id.lv);
        store = new ArrayList<>();
        UserID = new ArrayList<>();

        ref.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postsap : dataSnapshot.getChildren()) {

                    DataSnapshot data = postsap.child("name");
                    if (data.exists()) {
                        a = data.getValue().toString();
                    }
                    store.add(a);
                    String Id = postsap.getKey();
                    UserID.add(Id);
                }

                faculty_list = new String[store.size()];

                for (int i = 0; i < faculty_list.length; i++) {
                    faculty_list[i] = store.get(i);
                }
                if (getActivity() != null) {
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, faculty_list);
                    listview.setAdapter(adapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i = new Intent(getContext(), Faculty.class);
                i.putExtra("User_Id", UserID.get(position));
                startActivity(i);
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                String name = faculty_list[position];
                Toast.makeText(getContext(), "" + UserID.get(position), Toast.LENGTH_SHORT).show();

                DialogFragment dialogFragment = new MyDailogFragment();
                Bundle bundle = new Bundle();
                bundle.putString("Name", name);
              
                bundle.putString("Id", UserID.get(position));
                dialogFragment.setArguments(bundle);
                dialogFragment.show(getChildFragmentManager(), "missiles");

                return true;
            }
        });

        return root;
    }
}

这是我的代码对你提供的Java代码的翻译。如果你有任何问题或需要进一步的帮助,请随时提问。

英文:

I have Listview in a Fragment in which i am setting data using ArrayAdapter, when i am deleting the data with Dialog box the Value is duplicating in the listview. i don't know what to do i was thinkint to refresh my fragment when the delete button is clicked in dialog box but i don't how to do that.I am new to Android please let me know what can i do to solve this.Here is my Code.

ListOfFaculty

public class ListOfFaculty extends Fragment {

    DatabaseReference ref;
    ListView listview;
    List&lt;String&gt; store;
    List&lt;String&gt; UserID;
    String[] faculty_list;
    String a;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ref = FirebaseDatabase.getInstance().getReference(&quot;Faculty&quot;);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View root =  inflater.inflate(R.layout.fragment_list_of_faculty, container, false);

        listview = root.findViewById(R.id.lv);
        store =new ArrayList&lt;&gt;();
        UserID =new ArrayList&lt;&gt;();

        ref.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot postsap : dataSnapshot.getChildren() ) {

                    DataSnapshot data = postsap.child(&quot;name&quot;);
                    if(data.exists()){
                         a =  data.getValue().toString();
                    }
                    store.add(a);
                    String Id = postsap.getKey();
                    UserID.add(Id);
                }

                faculty_list = new String[store.size()];

                for(int i=0;i&lt;faculty_list.length;i++){
                    faculty_list[i] =store.get(i);
                }
                if (getActivity() != null) {
                    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getContext(), android.R.layout.simple_spinner_item, faculty_list);
                    listview.setAdapter(adapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {

                Intent i = new Intent(getContext(),Faculty.class);
                i.putExtra(&quot;User_Id&quot;,UserID.get(position));
                startActivity(i);
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {

                String name = faculty_list[position];
                Toast.makeText(getContext(), &quot;&quot;+UserID.get(position), Toast.LENGTH_SHORT).show();

                DialogFragment dialogFragment =new MyDailogFragment();
                Bundle bundle =new Bundle();
                bundle.putString(&quot;Name&quot;,name);
              
                bundle.putString(&quot;Id&quot;,UserID.get(position));
                dialogFragment.setArguments(bundle);
                dialogFragment.show(getChildFragmentManager(),&quot;missiles&quot;);

                return true;
            }
        });

        return root;
    }
}

This is my Dialog Box Code,I am calling this Dialog box with .setOnItemLongClickListener from my ListofFaculty fragment.

MyDailogFragment

public class MyDailogFragment extends DialogFragment {

    DatabaseReference dailog;
    String Id;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        final String name=  bundle.getString(&quot;Name&quot;,&quot;&quot;);
        Id = bundle.getString(&quot;Id&quot;,&quot;&quot;);

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
        alertDialog.setMessage(&quot;Delete &quot;+name+ &quot; ?&quot;)
                .setPositiveButton(&quot;Delete&quot;, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), &quot;&quot;+Id, Toast.LENGTH_SHORT).show();
                        dailog = FirebaseDatabase.getInstance().getReference(&quot;Faculty&quot;).child(Id);
                        dailog.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                dataSnapshot.getRef().removeValue();
                                Toast.makeText(getActivity(), name+&quot; Deleted Successfully &quot;, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }
                })
                .setNegativeButton(&quot;Cancel&quot;, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), &quot;Nothing&quot;, Toast.LENGTH_SHORT).show();
                    }
                });

        return alertDialog.create();
    }
}

答案1

得分: 0

你在有新值时必须清除旧数据

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        store.clear();
        for(DataSnapshot postsap : dataSnapshot.getChildren() ){

            DataSnapshot data = postsap.child("name");
            if(data.exists()){
                a =  data.getValue().toString();
            }

            store.add(a);
            String Id = postsap.getKey();
            UserID.add(Id);
        }

        faculty_list = new String[store.size()];

        for(int i=0;i<faculty_list.length;i++){
            faculty_list[i] =store.get(i);
        }
        if (getActivity() != null) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, faculty_list);
            listview.setAdapter(adapter);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});
英文:

You have to clear old data when have new value

ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                store.clear();
                for(DataSnapshot postsap : dataSnapshot.getChildren() ){

                    DataSnapshot data = postsap.child(&quot;name&quot;);
                    if(data.exists()){
                        a =  data.getValue().toString();
                    }

                    store.add(a);
                    String Id = postsap.getKey();
                    UserID.add(Id);
                }

                faculty_list = new String[store.size()];

                for(int i=0;i&lt;faculty_list.length;i++){
                    faculty_list[i] =store.get(i);
                }
                if (getActivity() != null) {
                    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getContext(), android.R.layout.simple_spinner_item, faculty_list);
                    listview.setAdapter(adapter);
                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

huangapple
  • 本文由 发表于 2020年9月9日 15:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63806621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定