自定义列表视图未出现在对话框中

Custom Listview doesn't appear in Dialog

新年快乐!

我从 Android 开始,我想在对话框中显示自定义列表视图,但是当我单击生成对话框的按钮时,我的 listView 没有出现。你知道这个问题吗? 我已经检查过我发送的项目列表 complete.I 在我调试时没有任何错误。 以下是部分代码:

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();
    }

自定义适配器

    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;
    }
   }

对话框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>

项目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>

Screen of the view

预先感谢您的回答!

不确定您是否找到了解决方案。

在您的 rank() 中,您在列表视图上完成业务逻辑后最后在构建器中设置视图。 将 rank() 的代码替换为:

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();
}

希望对你有所帮助,不会太迟!!