android - 如何动态添加ListView下面的按钮

标签 android listview button android-listview dynamically-generated

我想在 Activity A 中的 listView 下面有两个按钮。但是现在,即使 Activity A 中没有 listView,也会显示该按钮。

under_list_view_button.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="181dp"
        android:layout_height="wrap_content"
        android:id="@+id/addClaims"
        android:layout_marginLeft="15px"
        android:drawableRight="@mipmap/claims"
        android:text="Add Claims"/>

    <Button
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:id="@+id/btnSave"
        android:drawableRight="@mipmap/submit"
        android:layout_marginLeft="450px"
        android:text="Submit" />


</FrameLayout>

Activity A

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.work_details);
            listview = (ListView) findViewById(R.id.listView);
            btnAddClaims=(Button)findViewById(R.id.addClaims);
            btnSubmit=(Button)findViewById(R.id.btnSave);
            FrameLayout footerLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.under_list_view_button, null);
            btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
            btnAddClaims=(Button)footerLayout.findViewById(R.id.addClaims);
            listview.addFooterView(footerLayout);
            objMyCustomBaseAdapter=new    MyCustomBaseAdapter(getApplicationContext(),results);
             listview.setAdapter(objMyCustomBaseAdapter);
           }

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addDetails:
                mClickedPosition=-1;
                Intent intent = new Intent(getApplication(), B.class);  // go to B class
                startActivityForResult(intent, PROJECT_REQUEST_CODE);
                return true;
                    }
               }
        return super.onOptionsItemSelected(item);

    }

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
            ReceiveProject = data.getStringExtra("Project");
            ReceiveDescription = data.getStringExtra("Description");
            if(mClickedPosition==-1) {  // add returned value to new list
                MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();
                objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription); 
            }
            else
            {
                objMyCustomBaseAdapter=     // update list (MyCustomBaseAdapter)listview.getAdapter();
             objMyCustomBaseAdapter.changeItem(mClickedPosition,ReceiveProject,ReceiveDescription);

            }
        }

MyCustomBaseAdapter

public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView 


        private static ArrayList<SearchResults> searchArrayList;

        private LayoutInflater mInflater;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
            searchArrayList = results;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();
        }



        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

       public void addNewItem(String P,String D)
      {
        SearchResults obj=new SearchResults();
        obj.setProject(" Project/Service/Training : "+P);
        obj.setDescription(" Work Description : " + D);
        searchArrayList.add(obj);
        this. notifyDataSetChanged();
    }

    public void changeItem(int m,String P)
    {
        SearchResults obj=new SearchResults();
        obj.setProject(" Project/Service/Training : "+P);
        obj.setDescription(" Work Description : " + D);
        searchArrayList.set(m,obj);
        this. notifyDataSetChanged();
    }

activity_a

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background_work_details">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true"
        android:orientation="vertical" >

        <AbsoluteLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="397dp"
        android:id="@+id/listView"
        android:layout_x="3dp"
        android:layout_y="102dp" />


</AbsoluteLayout>
</ScrollView>
</LinearLayout>

输出 enter image description here

即使 A 中没有 listView,我也能得到这个

所以我现在有两个问题

  1. 如果A中没有listView,如何隐藏两个按钮?
  2. 当我尝试从 B 返回值到 A 时,应用程序崩溃了。此行显示错误 MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();

    LogCat 错误

Process: com.example.project.myapplication, PID: 6686 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.WorkDetailsTable}: java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.example.project.myapplication.Adapter.MyCustomBaseAdapter at android.app.ActivityThread.deliverResults(ActivityThread.java:3681)

MyCustomBaseAdapter 已编辑

public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView


        private static ArrayList<SearchResults> searchArrayList;

        FrameLayout footerLayout;
        private LayoutInflater mInflater;
        ListView listview;
      //  AbsoluteLayout footer;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results,ListView listview,FrameLayout footerLayout) {
            searchArrayList = results;
            this.listview=listview;
            this.footerLayout=footerLayout;
            mInflater = LayoutInflater.from(context);
            //this.footer=footer;
            addOrRemoveFooter();
        }

    public void addOrRemoveFooter(){
        if(searchArrayList.size()==0 && listview.getFooterViewsCount()==0){
            listview.removeFooterView(footerLayout);
        }
        else
        {

            listview.addFooterView(footerLayout);
        }

    }

尽管searchArrayList.size > 0,按钮仍然没有出现。如果我将代码更改为

 if(searchArrayList.size()==0 && listview.getFooterViewsCount()==0){
                listview.addFooterView(footerLayout);
            }

即使有多个列表,按钮也会出现。

最佳答案

这是我的解决方案
在您的 Activity A

public class A extends Activity{
            FrameLayout footerLayout;
            public void onCreate(Bundle savedInstanceState) {
                ...
                footerLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.under_list_view_button, null);
                btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
                btnAddClaims=(Button)footerLayout.findViewById(R.id.addClaims);

                objMyCustomBaseAdapter = new    MyCustomBaseAdapter(getApplicationContext(),results,listview, footerLayout);
                 listview.setAdapter(objMyCustomBaseAdapter);
               }
    }

在你的适配器中

    public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView 


            private static ArrayList<SearchResults> searchArrayList;
            ListView listview;
            FrameLayout footerLayout 
            private LayoutInflater mInflater;

            public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results, ListView listview, FrameLayout footerLayout) {
                searchArrayList = results;
                this.listview = listview;
                this.footerLayout = footerLayout;
                addOrRemoveFooter(); 
            }

            public void addOrRemoveFooter(){
                if(searchArrayList.size() == 0 && listView.getFooterViewsCount() > 0){
                     listview.removeFooterView(footerLayout);
                }else if(listView.getFooterViewsCount() == 0 && searchArrayList.size() > 0){
                     listview.addFooterView(footerLayout);
                }
            } 
            public void addNewItem(String P,String D){
            addOrRemoveFooter(); 
            ...
            }

            public void changeItem(int m,String P){
            addOrRemoveFooter(); 
            ...
            }
            public void removeItem(int position){
            addOrRemoveFooter(); 
            ...
            }

希望这有帮助

关于android - 如何动态添加ListView下面的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34011732/

相关文章:

android - 摄像机变焦

java - Libgdx 添​​加一个与游戏屏幕分离的 UI 按钮

java - Android afterTextChanged获取EditText标签

ios - 识别用于在代码中编辑的 Storyboard uibutton

html - 单击时按钮图像略微向上移动(在 Internet Explorer 中)

android - Nearby Messages API 在发布时返回状态代码 2806

java - 如何在具有不同数据的 Android 中创建 Speed-O-Meter 图表或半月形图表

Android ListView 复选框选择

jquery - 将样式应用于 kendoui ListView 所选元素

javascript - 使用 Jquery 单击隐藏的文件上传按钮?