2

I wanted to have two button below the listView in Activity A. But now, the button is shown even no listView in Activity A.

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>

Output enter image description here

I get this even no listView in A

So I have two problem now

  1. How to hide the two button if no listView in A ?
  2. App crashed when I try to return value from B to A. It shows error on this line MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();

    LogCat error

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 edited

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);
        }

    }

The button still not appear although searchArrayList.size > 0. If I change the code to

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

buttons appear even it is more than one list.

Tony
  • 2,515
  • 14
  • 38
  • 71

2 Answers2

1

I think that you need to include

under_list_view_button

to

"activity_a"

Add this: <include layout="@layout/under_list_view_button"/> under the ListView in activity_a

TuanDN
  • 7
  • 3
1

Here is my solution
In your 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);
               }
    }

In your adapter

    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(); 
            ...
            }

Hope this help

Linh
  • 57,942
  • 23
  • 262
  • 279
  • sorry for late respone, I have updated my code. please check it – Linh Dec 02 '15 at 01:36
  • the `results` in `addOrRemoveFooter()` still cannot be resolved. Should I use `searchArrayList` instead of `results` ? – Tony Dec 02 '15 at 01:42
  • Between , in your activity A `FrameLayout footerLayout = public void onCreate(Bundle savedInstanceState) {`, this line correct ? – Tony Dec 02 '15 at 01:46
  • can see my post again ? **MyCustomBaseAdapter edited** part – Tony Dec 02 '15 at 01:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96748/discussion-between-phan-vn-linh-and-tony). – Linh Dec 02 '15 at 02:04
  • Do you know [why](http://stackoverflow.com/questions/34173513/button-shows-in-emulator-but-not-in-real-device?noredirect=1#comment56093761_34173513) ? – Tony Dec 09 '15 at 08:03
  • Bro are you free? I facing the problem but it has stuck for so long..can you please help ? You no need do for me, but I seriously need to hear the advice from you. Please bro – Tony Jan 06 '16 at 01:50
  • what is your problem now? – Linh Jan 06 '16 at 01:51
  • I can't post question here :( I retrieved the value from MySQL and load them into listView. And now I want to edit, I get error – Tony Jan 06 '16 at 01:55
  • I think you need to post a question ;), many people can help you – Linh Jan 06 '16 at 02:03
  • I can't post question...reach limit – Tony Jan 06 '16 at 02:06
  • maybe you can create another account for post question ^^ – Linh Jan 06 '16 at 02:13
  • can I notify you later ? and upvotes some of my question ? – Tony Jan 06 '16 at 02:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99869/discussion-between-phan-vn-linh-and-tony). – Linh Jan 06 '16 at 02:14
  • Bro will you online all the time ? After lunch I will post the question – Tony Jan 06 '16 at 04:40
  • http://stackoverflow.com/questions/34627017/latest-value-did-not-return-to-listview – Tony Jan 06 '16 at 06:32
  • ok. I see your problem but sorry I have no idea to fix it :9, hope someone can help you – Linh Jan 06 '16 at 06:38
  • i have something to discuss with you – Linh Jan 06 '16 at 08:50