android - 如果为空,则更改 ListView 布局

标签 android android-layout layout-xml

<分区>

我使用适配器来设置项目布局并从 JSON 字符串填充 ListView。有时这个 JSON 是空的,ListView 中没有任何行,它完全是空白的。

在这种情况下,我想更改 View 并传递显示“此处没有任何行”的另一个布局。

所以这是我的代码:

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView);
        List list = new LinkedList();

        [...fill list...]

        ListAdapter adapter = new ListAdapter(this, R.layout.list_row_layout, list, R.id.edCod, R.id.edDes);
        listView.setAdapter(adapter);
    }
}

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Adapter> {
    private LayoutInflater layoutInflater;
    private int layout;
    private int textCod;
    private int textDes;

    public FruttaAdapter(Context context, int layout, List<Adapter> objects, int textCod, int textDes) {
        super(context, layout, objects);

        this.layout = layout;
        this.textCod = textCod;
        this.textDes = textDes;
        layoutInflater = LayoutInflater.from(context);
    }

最佳答案

您可以使用listView.setEmptyView(yourEmptyView)

关于android - 如果为空,则更改 ListView 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484505/

相关文章:

android - 如何在移动应用程序中实现推荐 friend ?

java - 如何在一个列表中过滤相同的纬度,日志值?

android - 通过下载 URL 显示来自 firebase cloud firestore 的图像

android - 使用 TextView.setText(Html.fromHtml(string)) 将具有 html 图像标签的字符串转换为图像

android - 如何在android中的getview()方法中多次动态创建xml布局?

magento - 在 Magento 皮肤 Javascript 之前加载外部 Javascript

magento - magento 中前端和后端(admin 或 adminhtml)之间的共享模板

c# - 使用来自 Firebase 数据库表 Xamarin 的数据填充 Spinner

android - CollapsingToolbarLayout 中的多行 TextView 而不是标题

java - 我可以在 android xml 布局或字符串值文件中编写 java 代码吗?