【发布时间】:2017-01-05 16:38:46
【问题描述】:
我正在尝试使用适配器在 android studio 中制作排行榜,但这工作得很好,如下所示:
但我想让分数显示在右侧。所以我做了这个,但是滚动进来时出现问题:
因为你可以滚动它们,所以名称和分数不匹配。
我怎么能做到这一点,因为只有一个卷轴,或者不知何故,名字在左边,分数在右边。
// Get ListView object from xml
final ListView nameView = (ListView) findViewById(R.id.nameView);
final ListView scoreView = (ListView) findViewById(R.id.scoreView);
// Create a new Adapter
final ArrayAdapter<String> nameAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
final ArrayAdapter<String> scoreAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
// Assign adapter to ListView
nameView.setAdapter(nameAdapter);
scoreView.setAdapter(scoreAdapter);
// Ordering with score and adding key values as string to nameList
highscoreRef.orderByChild("score").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
nameList.push(snapshot.child("name").getValue().toString());
scoreList.push(snapshot.child("score").getValue().toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Error sending data.", Toast.LENGTH_LONG).show();
}
});
//Collections.reverse(nameList);
highscoreRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (String name : nameList) {
nameAdapter.add(name);
}
for (String score : scoreList) {
scoreAdapter.add(score);
}
nameList.clear();
scoreList.clear();
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Huippupisteet"
android:id="@+id/highscore_text"
android:layout_gravity="center"
android:textSize="25dp"
android:layout_weight="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/nameView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</ListView>
</LinearLayout>
【问题讨论】:
-
你需要 2 个单独的列表视图吗?
-
嗯,实际上不是。重点是将用户名放在左边,分数放在右边。
标签: android listview firebase firebase-realtime-database