I am trying to make leaderboard in android studio using adapter but this worked fine like this:
but I wanted to make the scores display on the right side. So I made this, but there is problem when the scrolling comes in:
Because you can scroll both of them then the name and the score don't match.
How could I make this as there is one scroll only or somehow that name is on the left side and score is in the right side.
// 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>