早安,
我正在尝试根据选票计算孩子的数量。
这是我的Firebase结构。
和
我成功地将候选人从候选人表恢复到列表视图,但是我无法恢复他们在投票表中获得的票数。
这是我用于列表视图的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/count_ssc_president_last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="40sp"
android:textColor="@color/colorBlack"/>
<TextView
android:id="@+id/count_ssc_president_first_middle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"/>
<TextView
android:id="@+id/count_ssc_president_partylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/count_ssc_president_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:textSize="20sp"
android:textColor="@color/colorBlack"/>
</LinearLayout>
这是我的Main java类的xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Voter_SSC_President">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="14dp"
android:text="@string/choose_ssc_president"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<ListView
android:id="@+id/listCountSSCPresident"
android:layout_width="309dp"
android:layout_height="338dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="34dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="277dp"
android:layout_height="111dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_ssc_logo" />
</android.support.constraint.ConstraintLayout>
这是我的适配器
private Activity context;
private List<SSC_Presidents> CountSSC_PresidentsList;
DatabaseReference userReference;
long num;
private LayoutInflater inflater;
public CountSSCPresLists(Activity context, List<SSC_Presidents> CountSSC_PresidentsList )
{
super(context, R.layout.listview_count_ssc_president, CountSSC_PresidentsList);
this.context = context;
this.CountSSC_PresidentsList = CountSSC_PresidentsList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.listview_count_ssc_president, null, false);
TextView textViewLast_name = (TextView) listViewItem.findViewById(R.id.count_ssc_president_last_name);
TextView textViewFirst_middle_name = (TextView) listViewItem.findViewById(R.id.count_ssc_president_first_middle);
TextView textViewPartylist = (TextView) listViewItem.findViewById(R.id.count_ssc_president_partylist);
TextView textViewCount = (TextView) listViewItem.findViewById(R.id.count_ssc_president_count);
SSC_Presidents sscPresidents = CountSSC_PresidentsList.get(position);
/*
userReference = FirebaseDatabase.getInstance().getReference("vote");
userReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
num = dataSnapshot.getChildrenCount();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
textViewCount.setText((int) num);*/
textViewLast_name.setText(sscPresidents.getLast_name());
textViewFirst_middle_name.setText(sscPresidents.getFirst_name()+", "+sscPresidents.getMiddle_name());
textViewPartylist.setText(sscPresidents.getPartylist_id());
return listViewItem;
区块注释是我尝试根据候选人ID生成投票号
这是我的主要Java类
String stud_no;
DatabaseReference databaseReferenceCountSSCPres;
ListView listViewCountSSCPresident;
List<SSC_Presidents> sscCountPresList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin__count__ssc__president);
databaseReferenceCountSSCPres = FirebaseDatabase.getInstance().getReference("candidate");
stud_no = LoginScreen.TBPstud_no1();
listViewCountSSCPresident = findViewById(R.id.listCountSSCPresident);
sscCountPresList = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
Query CountSSCPresident = databaseReferenceCountSSCPres.orderByChild("council_id_position_id_school_year").equalTo("SSC_PRES_201819");
CountSSCPresident.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
sscCountPresList.clear();
for (DataSnapshot CountSSCPresSnapshot : dataSnapshot.getChildren())
{
SSC_Presidents CountSSCPres = CountSSCPresSnapshot.getValue(SSC_Presidents.class);
sscCountPresList.add(CountSSCPres);
}
CountSSCPresLists adapter = new CountSSCPresLists(Admin_Count_SSC_President.this, sscCountPresList);
listViewCountSSCPresident.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是结果
我想将结果放入结果的TextView中。
谢谢。
PS。当我删除适配器上的块注释时,结果屏幕将崩溃。
编辑:
该建议有效,但仅适用于最后一个候选人。 我现在也使用查询。
userReference = FirebaseDatabase.getInstance().getReference("vote");
Query CountSSCPresident = userReference.orderByChild("candidate_id").equalTo(sscPresidents.getCandidate_id());
CountSSCPresident.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
num = dataSnapshot.getChildrenCount();
textViewCount.setText(String.valueOf((int)num));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 0 :(得分:0)
4 elements
▿ 0 : 2 elements
▿ key : AnyHashable("aps")
- value : "aps"
▿ value : 2 elements
▿ 0 : 2 elements
- key : alert
▿ value : 2 elements
▿ 0 : 2 elements
- key : title
- value : New Message
▿ 1 : 2 elements
- key : body
- value : You have a new message from John
▿ 1 : 2 elements
- key : sound
- value : default
▿ 1 : 2 elements
▿ key : AnyHashable("gcm.message_id")
- value : "gcm.message_id"
- value : 0:
▿ 2 : 2 elements
▿ key : AnyHashable("google.c.a.e")
- value : "google.c.a.e"
- value : 1
▿ 3 : 2 elements
▿ key : AnyHashable("page")
- value : "page"
- value : message
您需要将num强制转换为字符串。试试这个:
textViewCount.setText((int) num);
编辑:也将其放在侦听器内部:
textViewCount.setText(String.valueOf((int)num));