当我在“自定义列表”上单击任意项时,我将制作一个简单的音乐应用程序;当我单击“返回”按钮时,从顶部开始显示其列表视图。
我需要单击或我所在位置的listView位置
//Getting List view.....
listView = (ListView) findViewById(R.id.MyListView);
myroot = FirebaseDatabase.getInstance().getReference("all");
readVariable = new ArrayList<>();
//Ends...............
// save index and top position
int index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
// restore index and position
listView.setSelectionFromTop(index, top);
}
//FireBase Work>>>>>>>>>>>>>>>>>>>
@Override
protected void onStart() {
super.onStart();
myroot.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
readVariable.clear();
for (DataSnapshot artisSnapshot : dataSnapshot.getChildren()) {
ReadVariables read = artisSnapshot.getValue(ReadVariables.class);
readVariable.add(read);
}
CustomList adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this,"Check your network",Toast.LENGTH_LONG).show();
}
});
}
//Ends>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
答案 0 :(得分:0)
将Unhandled promise rejection
和readVariable
定义为全局变量。
adapter
删除
CustomList adapter;
ArrayList<> readVariable = new ArrayList<>();
我们已经将readVariable定义为全局变量并对其进行了初始化。
从readVariable = new ArrayList<>();
onDataChange
并像这样写在这里
CustomList adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
现在在 //Getting List view.....
listView = (ListView) findViewById(R.id.MyListView);
myroot = FirebaseDatabase.getInstance().getReference("all");
adapter = new CustomList(MainActivity.this, readVariable);
listView.setAdapter(adapter);
//Ends...............
中写了setAdapter行,
写下:
onDataChange