android-显示从数据库到列表视图的数据并每次刷新它

时间:2018-04-21 19:34:53

标签: android firebase listview android-fragments

我在ListView内使用了Fragment。我还有EditTextButton。因此,每次点击Button时,我都会获取EditText的内容,每次在数据库中添加好友时都可以刷新ListView而无需进入另一个Fragment public class ContactFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_contact, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView list = getView().findViewById(R.id.contactframe); auth = FirebaseAuth.getInstance(); final User user = new User(); bouton = getView().findViewById(R.id.search_btn); email = getView().findViewById(R.id.search_field); bouton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!email.getText().toString().trim().equals(auth.getCurrentUser().getEmail().toString().trim())) { user.addFriend(auth.getCurrentUser().getUid(), email.getText().toString().trim()); email.setText(""); } else { Toast.makeText(getActivity(), "Action refusé", Toast.LENGTH_LONG).show(); } } }); contactList = new ArrayList<User>(); mDatabase = FirebaseDatabase.getInstance().getReference("User").child(auth.getCurrentUser().getUid().toString()) .child("contact"); mDatabase.addListenerForSingleValueEvent(valueEventListener); adaptor = new ContactAdaptor(getActivity(), contactList); list.setAdapter(adaptor); } ValueEventListener valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { contactList.clear(); if (dataSnapshot.exists()) { for (DataSnapshot snapshot : dataSnapshot.getChildren()) { User ami = snapshot.getValue(User.class); contactList.add(ami); } adaptor.notifyDataSetChanged(); } } @Override public void onCancelled(DatabaseError databaseError) { } }; } 1}}并且复出。

这是我的片段。

public class ContactAdaptor extends ArrayAdapter<User> {
    private LayoutInflater mInflater;
    ArrayList<User> myList;

    public ContactAdaptor(FragmentActivity activity, List<User> myList) {
        super(activity, 0, myList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        User item = (User) getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_contact, parent, false);
        }

        TextView username = (TextView) convertView.findViewById(R.id.text_view_contact_username);
        TextView email = (TextView) convertView.findViewById(R.id.contact_email);
        username.setText(item.username);
        email.setText(item.email);

        return convertView;
    }
}

我的适配器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Editext for Search -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/search_field"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="15dp"
            android:layout_marginTop="33dp"
            android:background="@drawable/search_layout"
            android:ems="10"
            android:hint="Chercher içi"
            android:inputType="textPersonName"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:textColor="#999999"
            android:textSize="16sp" />

        <Button
            android:id="@+id/search_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/search_field"
            android:layout_marginEnd="60dp"
            android:text="Ajouter" />
    </RelativeLayout>

    <ListView
        android:id="@+id/contactframe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_marginRight="16dp"
        android:scrollbarSize="4dp"
        android:scrollbarThumbVertical="@drawable/scrollbar"
        android:scrollbars="vertical"
        tools:context="com.android.pfe.fragment.ContactFragment"></ListView>
</LinearLayout>

布局文件是

 Private Sub txtbasicsalary_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtbasicsalary.KeyPress
    If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." Then
        e.Handled = True
    End If
    If e.KeyChar = "." And txtbasicsalary.Text.IndexOf(".") <> -1 Then
        e.Handled = True
    End If
    If e.KeyChar = "." Then
        e.Handled = False
    End If
    If e.KeyChar = Chr(Keys.Back) Then
        e.Handled = False
    End If
    If Char.IsDigit(e.KeyChar) Then
        If txtbasicsalary.Text.IndexOf(".") <> -1 Then
            If txtbasicsalary.Text.Length >= txtbasicsalary.Text.IndexOf(".") + 3 Then  'replace 2 for greater numbers after decimal point
                e.Handled = True
                txtconveyance.Focus()
            End If
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

您已使用addListenerForSingleValueEvent一次获取数据,并且不打开底层连接以侦听firebase端的任何数据更改。

您只需使用addValueEventListener代替addListenerForSingleValueEvent,如下所示。

mDatabase = FirebaseDatabase.getInstance().getReference("User").child(auth.getCurrentUser().getUid().toString())
            .child("contact");

// This is where you should use the addValueEventListener instead of addListenerForSingleValueEvent
mDatabase.addValueEventListener(valueEventListener);

我正在复制firebase documentation中有用的部分。

  

在某些情况下,您可能希望一次调用回调   立即删除,例如初始化你的UI元素时   不要指望改变。你可以使用addListenerForSingleValueEvent()   简化此方案的方法:它触发一次然后不触发   再次触发。

希望能解决你的问题。