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

Posted

技术标签:

【中文标题】自定义列表视图未出现在对话框中【英文标题】:Custom Listview doesn't appear in Dialog 【发布时间】:2021-04-10 10:32:33 【问题描述】:

新年快乐!

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

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_
    android:layout_>
    <LinearLayout
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_marginStart="0dp"
        android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
    android:layout_>

    <TextView
        android:id="@+id/textView"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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

提前感谢您的回答!

【问题讨论】:

【参考方案1】:

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

在您的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();

希望它会有所帮助,不要为时已晚!

【讨论】:

以上是关于自定义列表视图未出现在对话框中的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Blend 中自定义动画?

单击列表视图中自定义单元格中按钮的侦听器

如何获取 AlertDialog 中自定义列表视图的单击项的值?

对话框中的列表视图/适配器未出现单选按钮布局

在情节提要中自定义表格视图单元格时出现 NSInternalInconsistencyException

如何固定表格视图中自定义单元格文本字段的标签值?