setEmptyView Android列表视图

时间:2013-08-13 14:02:24

标签: android listview

你好我的应用程序包含listview.listview包含页脚,其中包含下一个按钮,用户按下一个按钮listview适配器刷新并使用通知数据集更改方法填充新问题。但我想在数据为空时设置一条消息从json文件(从服务器传入的动态json文件)中读取。 我使用了setEmptyView方法,但它会隐藏listview,我的页脚也会隐藏,所以用户无法移动到下一组问题。  而且我没有扩展listview。扩展自定义baseadapter请指导我我是android的新手

**

  

更新: -

** 我使用创建textview addview到xml并返回customeview来解决它。我的getview方法如下所示。我可以这样做。此代码可能会影响应用程序性能。因为我正在转向另一种观点,请指导我

@Override
    public int getCount() 
    {
        if(itemDetailarraylist.size()==0)
            return 1;
        else
            return itemDetailarraylist.size();
    }

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {

        if (itemDetailarraylist.size() == 0) {
            TextView message = new TextView(context);
            message.setText("No Data Found on This Section");
            message.setTextSize(20);
            message.setLayoutParams(new LinearLayout.LayoutParams(
                    (int) LinearLayout.LayoutParams.WRAP_CONTENT,
                    (int) LinearLayout.LayoutParams.WRAP_CONTENT));
            convertView = inflater.inflate(R.layout.nodatafound_message,null);
            ((LinearLayout)convertView).setGravity(Gravity.CENTER);
            ((LinearLayout)convertView).addView(message);
            return convertView;
        } else {
            ViewHolder holder;
            if(convertView==null)
            {------}}
}

2 个答案:

答案 0 :(得分:1)

请参阅此链接

How do I set an empty view for ListViews in an activity with multiple ListViews?

<TextView android:id="@+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="60px"
android:text="@string/empty_text"
/>

mListView.setEmptyView(findViewById(R.id.empty_view));

答案 1 :(得分:0)

    Update:-

** i solved it using creating textview addview into xml and return customeview. my getview method shown below.any other way can i do this. can this code may impact on the app performance. beacause i am inflecting another view please guide me

@Override
    public int getCount() 
    {
        if(itemDetailarraylist.size()==0)
            return 1;
        else
            return itemDetailarraylist.size();
    }

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {

        if (itemDetailarraylist.size() == 0) {
            TextView message = new TextView(context);
            message.setText("No Data Found on This Section");
            message.setTextSize(20);
            message.setLayoutParams(new LinearLayout.LayoutParams(
                    (int) LinearLayout.LayoutParams.WRAP_CONTENT,
                    (int) LinearLayout.LayoutParams.WRAP_CONTENT));
            convertView = inflater.inflate(R.layout.nodatafound_message,null);
            ((LinearLayout)convertView).setGravity(Gravity.CENTER);
            ((LinearLayout)convertView).addView(message);
            return convertView;
        } else {
            ViewHolder holder;
            if(convertView==null)
            {------}}
}