我有一个 Android
应用程序,它在运行时根据数据动态添加 TableViews、
TableRows
和 TextViews
在 Firebase
存储库中找到。更具体地说,为在 repo 中找到的每个“投票”添加一个 TableView
到静态 LinearLayout
,为在投票中找到的每个“选举”添加一个 TableRow
被添加到 Tablelayout
中,对于选举中的每个“Nominee”,一个包含两个 TextViews
的 TableRow
被添加到TableLayout
也是如此。因此,我可能会在运行时添加多个表,每个表包含多个选举的行,并且每个选举都有多个被提名人的数据行。这是我的静态布局,可以很好地衡量....
<?xml version="1.0"?>
<ScrollView android:layout_height="android:layout_width="match_parent" android:id="@+id/scrollView1" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/linearLayout" android:orientation="vertical">
</LinearLayout>
</ScrollView>
..这是我的 Firebase
存储库的表示...
-POLLS
NUMPOLLS - 5
(pollskey) - NAME - Poll1
NUMELECTIONS - 2
ELECTIONS
(electionskey) - NAME - Election1
NUMNOMINATIONS - 2
NUMVOTERS - 2
NUMBERTOELECT - 1
VOTERS - (votesrkey) - NAME - Charles
NUMBER - (678) 333-4444
.
.
.
(voterskey) - ...
NOMINATIONS - (nominationskey) - NAME - Richard Nixon
NUMBEROFVOTES - 2
.
.
.
(nominationskey) - ...
.
.
.
(electionskey) - ...
.
.
.
(pollskey) - ...
所以我的问题确实有两个线索。首先是面向Android的...
- 当 repo 中的数据发生变化时,遍历动态添加的
Android
View 以更改TextView
数据的最佳方法是什么,我不知道有多少这样的TableView
,或者TableRows
在编译的时候会有吗?
现在我正在做这样的事情,但是进展很慢我觉得必须有更好的方法......
private void appendCandidatesAndVotes(DataSnapshot election, TableLayout tbl) {
Random randGen = new Random();
DataSnapshot nominees = election.child("Nominations");
for (DataSnapshot nominee : nominees.getChildren()) {
// Create row for candidate name and number of votes
TableRow rowNameAndVotes = new TableRow(this);
// Generating a random row ID here allows us to pass this to
// the valueEventListener and quickly locate this row in
// the case of a data change (someone has cast a vote) so we
// can update
int uniqueRowId = randGen.nextInt(1000);
rowNameAndVotes.setId(uniqueRowId);
rowNameAndVotes.setBackgroundColor(Color.WHITE);
rowNameAndVotes.setLayoutParams(new TableRow.LayoutParams (
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// Create candidate name view
TextView viewCandidateName = new TextView(this);
viewCandidateName.setId(2);
viewCandidateName.setText(nominee.child("Name").getValue(String.class));
viewCandidateName.setTextColor(Color.BLACK);
viewCandidateName.setPadding(5, 5, 5, 5);
rowNameAndVotes.addView(viewCandidateName);
// Create number of votes view
TextView viewNumVotes = new TextView(this);
viewNumVotes.setId(3);
viewNumVotes.setText(nominee.child("NumberOfVotes").getValue(String.class));
viewNumVotes.setTextColor(Color.BLACK);
viewNumVotes.setPadding(3, 5, 5, 5);
rowNameAndVotes.addView(viewNumVotes);
// Add row to table
tbl.addView(rowNameAndVotes);
// Lets get a Firebase reference for this nominee and
// attach a listener to alert us to all future changes of
// the values therein, so we can update vote counts dynamically
Firebase nomineeRef = nominee.getRef();
nomineeRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot nomineeSnapshot) {
String nomineeName = nomineeSnapshot.child("Name").getValue(String.class);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
int layoutChildCount = layout.getChildCount();
for (int i = 0; i < layoutChildCount; i++) {
View layoutChildView = layout.getChildAt(i);
// If its a Table, loop through all row
if (layoutChildView instanceof TableLayout) {
TableLayout tbl = (TableLayout)layoutChildView;
int tableChildCount = tbl.getChildCount();
for (int j = 0; j < tableChildCount; j++) {
View tblChildView = tbl.getChildAt(i);
if (tblChildView instanceof TableRow) {
TableRow row = (TableRow)tblChildView;
int rowChildren = row.getChildCount();
for (int k = 0; k < rowChildren; k++) {
TextView tv = (TextView)(row.getChildAt(k));
String rowCandidateName = tv.getText().toString();
if (rowCandidateName) ...
}
}
}
}
}
}
我认为这是布局的方式,我唯一可以直接引用的布局是基础 LinearLayout
。我可以使用 setId()
为所有 TableRows
提供唯一的 ID,但据我所知,我无法将任何数据传递给 ValueEventListener
,我也不能引用封闭范围内的任何变量(我希望我错了,这会让事情变得更容易)。我知道我可以在 ValueEventListener
中引用静态最终变量,但我不知道这对我有何帮助,因为我不知道在运行时我将处理多少表或行.
因此,简而言之,我如何将对 ValueEventListener
的特定调用与其数据相关联的 TableRow
链接起来?
现在是问题 2,它更具体一点 Firebase
....
- 似乎
ValueEventListener
在加载 Activity 时被调用一次,然后在基础数据发生任何更改时再次调用。但我只希望它要求更改基础数据,而不是加载。我使用了错误的 EventListener 吗?或者有什么方法可以避免这种行为?
最佳答案
对于 Firebase 特定问题 - 这是设计使然。 Firebase 不鼓励区分初始数据和数据更新,因为在实时场景中,您永远无法真正“ catch ”最新数据。
如果您不想在启动时检索所有数据,那么我会使用查询和限制来限制您可以接收的数据。例如,如果您只想接收最新的 100 个民意调查,您可以这样做:
Firebase ref = new Firebase(url);
Query q = ref.limit(100);
q.addChildEventListener(...);
并且子事件监听器将在每次轮询时被调用一次。当添加新的民意调查时,它将再次被调用(并且对于 100 列表中最旧的民意调查将有一个 child 移除事件)。
希望对您有所帮助!
关于java - 如何在运行时更改动态添加的 Android View 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979531/