1

My ListView is goind to show the rank of users, usernames, and their score, smth like: 1. John 25 and I'd like to display the score just like it's shown on the example attached.

enter image description here

Martin Lukas
  • 314
  • 5
  • 17

2 Answers2

1

What you need to use is a CustomAdapter instead of the default adapter. Please refer to this other answer Custom Adapter for List View

Basically you create your own item layout: how you wish each item in the list will be displayed (it could be a TextView for the username and another TextView for the Score).

And then in the getView method of the Adapter set the value of each one.

sebasira
  • 1,739
  • 1
  • 22
  • 41
1

You can achieve this using a custom list adapter and custom layout xml Try this:-

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = findViewById(R.id.list_view);
    listView.setAdapter(new NameRankAdapter(this));
}
}

NameRankAdapter.java

public class NameRankAdapter extends BaseAdapter {
private Context context;

public NameRankAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return 20;
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.list_item, null);
    }
    TextView name = view.findViewById(R.id.name);
    name.setText("Name " + i);
    TextView rank = view.findViewById(R.id.rank);
    rank.setText(String.valueOf(i));
    return view;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:text="name"
    android:id="@+id/name"
    android:textSize="30sp"
    android:textColor="@android:color/black"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="name"
    android:textSize="25sp"
    android:id="@+id/rank"
    android:textColor="@android:color/black"/>
</FrameLayout>
Rahul Samaddar
  • 244
  • 2
  • 8