gpt4 book ai didi

android - Android 上的 ListView 中未显示 Firebase 数据

转载 作者:行者123 更新时间:2023-11-29 01:03:06 26 4
gpt4 key购买 nike

Data Structure Link

Part of user1

我正在尝试在 android 上构建我的 firebase 项目,我必须在其中查看通知列表,但我无法在 ListView 中查看它。如果我创建一个本地数组对象,它会出现在列表中。我通过调试选项检查了它,我可以看到数据进入数组。

主要内容:

public class Notification extends AppCompatActivity {

private static final String TAG = "Notifications";

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mNotificationDatabaseReference;
private ChildEventListener mChildEventListener;
private FirebaseAuth mAuth;
private String userUid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);

mAuth = FirebaseAuth.getInstance();
userUid = mAuth.getCurrentUser().getUid();

mFirebaseDatabase = FirebaseDatabase.getInstance();
mNotificationDatabaseReference = mFirebaseDatabase.getReference().child("USER").child("User1").child("notifications");

final ArrayList<Word> words = new ArrayList<Word>();

// Read from the database
mNotificationDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Word value = postSnapshot.getValue(Word.class);
words.add(value);
// TODO: handle the post
}// This method is called once with the initial value and again
// whenever data at this location is updated.

Log.d(TAG, "Value is: " + words);
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});


WordAdapter adapter = new WordAdapter(this, words);
ListView listView = findViewById(R.id.notification);
listView.setAdapter(adapter);
}
}

适配器:

public class WordAdapter extends ArrayAdapter<Word>{

private static final String LOG_TAG = WordAdapter.class.getSimpleName();

/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*
* @param context The current context. Used to inflate the layout file.
* @param words A List of words objects to display in a list
*/
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}

/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* @param position The position in the list of data that should be displayed in the
* list item view.
* @param convertView The recycled view to populate.
* @param parent The parent ViewGroup that is used for inflation.
* @return The View for the position in the AdapterView.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}

// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWord = getItem(position);

// Find the TextView in the list_item.xml layout with the ID version_name
TextView notificationHeadView = (TextView) listItemView.findViewById(R.id.notification_head);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
notificationHeadView.setText(currentWord.getNotificationHead());

// Find the TextView in the list_item.xml layout with the ID version_number
TextView notificationDateView = (TextView) listItemView.findViewById(R.id.notification_date);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
notificationDateView.setText(currentWord.getNotificationDate());

// Find the TextView in the list_item.xml layout with the ID version_number
TextView notificationBodyView = (TextView) listItemView.findViewById(R.id.notification_body);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
notificationBodyView.setText(currentWord.getNotificationBody());

// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}

}

单词:

public class Word {

private String notificationHead;
private String notificationDate;
private String notificationBody;

public Word(){

}
public Word(String notificationHead, String notificationDate, String notificationBody){
this.notificationHead = notificationHead;
this.notificationDate = notificationDate;
this.notificationBody = notificationBody;
}

public String getNotificationHead(){
return notificationHead;
}
public void setNotificationHead(String notificationHead) {
this.notificationHead = notificationHead;
}

public String getNotificationDate(){
return notificationDate;
}
public void setNotificationDate(String notificationDate) {
this.notificationDate = notificationDate;
}

public String getNotificationBody() {
return notificationBody;
}
public void setNotificationBody(String notificationBody) {
this.notificationBody = notificationBody;
}
}

请帮忙!

最佳答案

好的,试试这个:

1) 将数组声明移出 onCreate() 方法,使其成为 Activity 的成员变量,这样您就不必将其声明为 final:

ArrayList<Word> words = new ArrayList<Word>();

2) 将适配器声明也添加为类的成员变量,并在访问数据库之前实例化适配器:

    adapter = new WordAdapter(this, words);
ListView listView = findViewById(R.id.notification);
listView.setAdapter(adapter);

3) 最后,将所有数据添加到列表后,调用 notifiyDatasetChanged() 适配器刷新列表。

for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Word value = postSnapshot.getValue(Word.class);
words.add(value);
// TODO: handle the post
}
adapter.notifyDatasetChanged();

关于android - Android 上的 ListView 中未显示 Firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544302/

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