gpt4 book ai didi

android-从数据库显示数据到 ListView 并每次刷新

转载 作者:行者123 更新时间:2023-11-30 00:00:53 26 4
gpt4 key购买 nike

我在我的 Fragment 中使用了一个 ListView。我还有一个 EditText 和一个 Button。所以每次我点击我的 Button 时,我都会获取我的 EditText 的内容 每次我在我的列表中添加 friend 时,如何刷新我的 ListView数据库,而不必进入另一个 Fragment 并返回。

这是我的 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) {

}
};
}

还有我的适配器

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>

最佳答案

您已使用 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 复制对这里有用的部分.

In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the addListenerForSingleValueEvent() method to simplify this scenario: it triggers once and then does not trigger again.

希望这能解决您的问题。

关于android-从数据库显示数据到 ListView 并每次刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49959533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com