Happy new year !
I'm starting in Android and I'd like to display a Custom listview in a dialog but when I click on the button that generates the dialog my listView doesn't appear. Do you have an idea of the problem? I have checked that the list of items I am sending is complete.I don't have any error when I debug. Here are some bits of the code:
Function call by the button in StatActivity
private void rank(){
List<Runner> sortedList = db.getRunnersOrderedByRating(runnerList);
AlertDialog.Builder builder = new AlertDialog.Builder(StatActivity.this);
View view = getLayoutInflater().inflate(R.layout.dialog_rank, null);
ListView listView = (ListView) view.findViewById(R.id.listView_rank_participant);
RankAdapter adapter = new RankAdapter(this, sortedList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),
"Rang "+i, Toast.LENGTH_SHORT);
}
});
builder.setView(R.layout.dialog_rank);
AlertDialog d_rank = builder.create();
d_rank.show();
}
Custom Adapter
public class RankAdapter extends ArrayAdapter<Runner> {
private static class ViewHolder {
TextView name;
TextView rank;
}
public RankAdapter(Context context, List<Runner> p) {
super(context, R.layout.item_rank, p);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Runner p = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
RankAdapter.ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new RankAdapter.ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_rank, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tv_np);
viewHolder.rank = (TextView) convertView.findViewById(R.id.tv_rank);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (RankAdapter.ViewHolder) convertView.getTag();
}
// Populate the data from the data object via the viewHolder object
// into the template view.
viewHolder.name.setText(p.getLastName()+" "+p.getFirstName());
viewHolder.rank.setText(String.valueOf(position));
// Return the completed view to render on screen
return convertView;
}
}
Dialog xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="458dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="56dp"
android:text="Classement individuelle :"
android:textSize="40sp" />
<ListView
android:id="@+id/listView_rank_participant"
android:layout_width="1243dp"
android:layout_height="693dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="3dp" />
</LinearLayout>
</RelativeLayout>
Item xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="34dp"
android:layout_marginTop="16dp"
android:text="Rang : "
android:textSize="40dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="46dp" />
<TextView
android:id="@+id/tv_rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="167dp"
android:layout_marginTop="19dp"
android:text="TextView"
android:textSize="40dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="181dp" />
<TextView
android:id="@+id/tv_np"
android:layout_width="621dp"
android:layout_height="52dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="16dp"
android:layout_marginEnd="300dp"
android:text="Name"
android:textSize="40dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="391dp" />
</RelativeLayout>
Thank you in advance for your answer!
Not sure whether you find the solution or not.
In your rank()
, you are setting view in the builder at the end after you had done your business logic on listview.
Replace your code of rank()
with:
private void rank(){
List<Runner> sortedList = db.getRunnersOrderedByRating(runnerList);
AlertDialog d_rank = new AlertDialog.Builder(StatActivity.this).create();
View view = getLayoutInflater().inflate(R.layout.dialog_rank, null);
d_rank.setView(view);
ListView listView = (ListView) view.findViewById(R.id.listView_rank_participant);
RankAdapter adapter = new RankAdapter(this, sortedList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),
"Rang "+i, Toast.LENGTH_SHORT);
}
});
d_rank.show();
}
Hope it will help and not too late!!