当 listView 为空时,setEmptyView 不显示图像

Posted

技术标签:

【中文标题】当 listView 为空时,setEmptyView 不显示图像【英文标题】:setEmptyView is not displaying image when listView is empty 【发布时间】:2019-11-11 18:38:20 【问题描述】:

我正在尝试在 listView 为空时显示图像,但不幸的是,当列表视图为空时,只要列表为空,它就不会显示该图像。

我尝试将 setEmptyView 移动到 oncreate 中,在此之前我尝试检查光标是否为空,如果为空则调用 setemptyview 方法

围栏活动 在这个活动中,我尝试使用 setEmptyView 方法。

public class FenceActivity extends AppCompatActivity 
    List<Fence> fenceList;
    SQLiteDatabase sqLiteDatabase;
    ListView listViewFences;
    FenceAdapter fenceAdapter;
    DataBaseHelper dataBaseHelper;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savedfences);

        listViewFences = findViewById(R.id.fencesListView);
        fenceList = new ArrayList<>();

        showFencesFromDatabase();
    

    public void showFencesFromDatabase() 

        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();
        if (cursor.moveToFirst()) 
            do 
                fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
             while (cursor.moveToNext());
        
        cursor.close();
        fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
        listViewFences.setAdapter(fenceAdapter);

        listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    

    public void reloadFencesFromDatabase() 
        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();
        fenceList.clear();
        if (cursor.moveToFirst()) 
            do 
                fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
             while (cursor.moveToNext());
        
        cursor.close();
        fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
        listViewFences.setAdapter(fenceAdapter);
        listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    

Savedences.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_ android:layout_>

    <ListView
        android:id="@+id/fencesListView"
        android:layout_
        android:layout_ />

    <ImageView
        android:id="@+id/emptyElement"
        android:layout_
        android:layout_
        android:dividerHeight="1dp"
        android:visibility="gone"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

适配器类

public class FenceAdapter extends ArrayAdapter<Fence> 

    Context context;
    int listLayoutRes;
    List<Fence> fenceList;
    DataBaseHelper dataBaseHelper;
    FenceActivity fenceActivity;

    public FenceAdapter(Context context, int listLayoutRes, List<Fence> fenceList) 
        super(context, listLayoutRes, fenceList);
        this.context = context;
        this.listLayoutRes = listLayoutRes;
        this.fenceList = fenceList;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        if (convertView == null) 
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_layout_fences, null);
        


         final Fence fence = fenceList.get(position);

        TextView textViewSno = convertView.findViewById(R.id.textViewSnoLabel);
        TextView textViewLat = convertView.findViewById(R.id.textViewLatitudeValue);
        TextView textViewLon = convertView.findViewById(R.id.textViewLongitudeValue);
        TextView textViewRadi = convertView.findViewById(R.id.textViewRadiusValue);

        textViewSno.setText(Integer.toString(fence.getSno()));
        textViewLat.setText(String.valueOf(fence.getLat()));
        textViewLon.setText(String.valueOf(fence.getLon()));
        textViewRadi.setText(Integer.toString(fence.getRadius()));

        Button buttonDel = convertView.findViewById(R.id.buttonDeleteFence);

        buttonDel.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View view) 
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Are you sure");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) 
                        dataBaseHelper = new DataBaseHelper(context);
                        fenceActivity = (FenceActivity)context;
                        dataBaseHelper.deleteDataById(fence);
                        fenceActivity.reloadFencesFromDatabase();
                 
                );
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) 
                    
                );
                AlertDialog dialog = builder.create();
                dialog.show();
            
        );
        return convertView;
    

当列表为空时,我可以进行哪些更改以显示该图像。

【问题讨论】:

设置适配器后,检查列表的大小,即fenceList。如果 size 小于零,设置 fenceList.setVisibility(View.GONE);和 emptyElement.setVisibility(View.VISIBLE); 【参考方案1】:
public class FenceActivity extends AppCompatActivity 
    List<Fence> fenceList;
    SQLiteDatabase sqLiteDatabase;
    ListView listViewFences;
    FenceAdapter fenceAdapter;
    DataBaseHelper dataBaseHelper;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savedfences);

        listViewFences = findViewById(R.id.fencesListView);
        fenceList = new ArrayList<>();

        showFencesFromDatabase();
    

    public void showFencesFromDatabase() 

        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();
        if (cursor.moveToFirst()) 
            do 
                fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
             while (cursor.moveToNext());
        
        cursor.close();
        fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
        listViewFences.setAdapter(fenceAdapter);

        listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    

    public void reloadFencesFromDatabase() 
        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();
        fenceList.clear();
        if (cursor.moveToFirst()) 
            do 
                fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
             while (cursor.moveToNext());
        
        cursor.close();
        fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
        listViewFences.setAdapter(fenceAdapter);
        listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    

添加这个而不是listViewFences.setEmptyView(findViewById(R.id.emptyElement));

listViewFences.setVisibility(fenceList.size>0?View.VISIBLE:View.GONE);
emptyElement.setVisibility(fenceList.size>0?View.GONE:View.VISIBLE);

还要在活动中定义emptyElement view

【讨论】:

在 xml 中的 ImageView 中添加 src 并尝试【参考方案2】:

在设置适配器之前尝试设置空视图

    fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
    listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    listViewFences.setAdapter(fenceAdapter);

【讨论】:

【参考方案3】:

在您的代码中进行此更改 -

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savedfences);

        listViewFences = findViewById(R.id.fencesListView);
        fenceList = new ArrayList<>();

        fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
        listViewFences.setAdapter(fenceAdapter);

        showFencesFromDatabase();
    

   public void showFencesFromDatabase() 

        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();
        if (cursor.moveToFirst()) 
            do 
                fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
             while (cursor.moveToNext());
        
        cursor.close();
        notifyDataSetChanged();

        if(fenceList.size() > 0)
            findViewById(R.id.emptyElement.setVisibility(View.GONE);
            fenceList.setVisibility(View.VISIBLE); 
        else
            findViewById(R.id.emptyElement.setVisibility(View.VISIBLE);
            fenceList.setVisibility(View.GONE); 
        

    

【讨论】:

can't use function notifyDataSetChanged() studio 报错。

以上是关于当 listView 为空时,setEmptyView 不显示图像的主要内容,如果未能解决你的问题,请参考以下文章

WPF - ListView 为空时不显示上下文菜单

有没有办法在 xaml 中隐藏 ListView?

当我的列表为空时,如何显示图像或文本?

ListView如何优化

空ListView显示另一个活动

当 textArea 为空时,按钮不会被禁用