我在一个活动中有4个片段,即数据库片段,当前包含ListView,它将查询Firebase以从父节点“资产”获取子代。另外,ListView具有一个自定义适配器,该适配器使其具有标题和副标题。
这是数据库结构
这是主要活动:
/var/www/xyz_pythonanywhere_com_wsgi.py
这是数据库片段:
private var toolbar: ActionBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar = supportActionBar
toolbar?.title = resources.getString(R.string.navigation_database)
loadFragment(DatabaseFragment())
setNavigation()
}
private fun setNavigation(){
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigation)
bottomNavigationView.setOnNavigationItemSelectedListener { item ->
var fragment: Fragment
when (item.itemId) {
R.id.navigation_database -> {
toolbar?.title = resources.getString(R.string.navigation_database)
fragment = DatabaseFragment()
loadFragment(fragment)
}
R.id.navigation_track -> {
toolbar?.title = resources.getString(R.string.navigation_tracking)
fragment = TrackingFragment()
loadFragment(fragment)
}
R.id.navigation_activities -> {
toolbar?.title = resources.getString(R.string.navigation_activities)
fragment = ActivitiesFragment()
loadFragment(fragment)
}
R.id.navigation_references -> {
toolbar?.title = resources.getString(R.string.navigation_references)
fragment = ReferencesFragment()
loadFragment(fragment)
}
}
true
}
}
private fun loadFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.childLayout, fragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
这是自定义适配器:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_database, container, false)
val titleList = ArrayList<String>()
val subtitleList = ArrayList<String>()
val databaseReference = FirebaseDatabase.getInstance().getReference("Assets")
databaseReference.addValueEventListener(object : ValueEventListener{
override fun onCancelled(databaseError: DatabaseError) {
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
for(childSnapshot: DataSnapshot in dataSnapshot.children){
titleList.add(childSnapshot.key.toString())
subtitleList.add("subtitle")
}
}
})
val listView = rootView.findViewById(R.id.databaseListView) as ListView
val adapter = DatabaseListAdapter(activity as MainActivity, titleList, subtitleList)
listView.adapter = adapter
return rootView
}
Firebase数据库规则:
public Context context;
public ArrayList<String> titleList;
public ArrayList<String> subtitleList;
public DatabaseListAdapter(Context context, ArrayList<String> titleList, ArrayList<String> subtitleList){
this.context = context;
this.titleList = titleList;
this.subtitleList = subtitleList;
}
@Override
public int getCount() {
return titleList.size();
}
@Override
public Object getItem(int position) {
return titleList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rootView = LayoutInflater.from(context).inflate(R.layout.layout_listview_database, parent, false);
TextView titleView = rootView.findViewById(R.id.titleView);
TextView subtitleView = rootView.findViewById(R.id.subtitleView);
titleView.setText(titleList.get(position));
subtitleView.setText(subtitleList.get(position));
return rootView;
}
答案 0 :(得分:0)
在override fun onDataChange(dataSnapshot: DataSnapshot)
中,将所有内容添加到列表后,您需要致电adapter.notifyDataSetChanged()
。
您可以保留对适配器的全局var
引用,也可以按ID查找回收者视图并获取其适配器。
您可能还想先清除列表,然后再向其中添加内容。