Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:10:10+00:00 2026-06-17T21:10:10+00:00

i have a layout with listview and under it there is linearlayout with two

  • 0

i have a layout with listview and under it there is linearlayout with two buttons horizantally, i want when no checkbox is checked the two buttons hide, i try to make it but my code make the two buttons hide and a black instead of them appears , and when a checkbos is checked the two buttons don’t appear, why ?

class RestaurantAdapter extends BaseAdapter {
    private ArrayList<Boolean> status = new ArrayList<Boolean>();
    private static LayoutInflater inflater = null;
    private ArrayList<HashMap<String, String>> data;
    Activity activity;
    LinearLayout layout;
public RestaurantAdapter(Activity activity,
        ArrayList<HashMap<String, String>> data, LinearLayout layout) {
    this.layout = layout;
    this.activity = activity;
    this.data = data;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < data.size(); i++) {
        status.add(false);
    }
}
public int getCount() {
    return data.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
                null);
    TextView name = (TextView) vi
            .findViewById(R.id.restaurant_multi_select_title);
    ImageView image = (ImageView) vi
            .findViewById(R.id.restaurant_multi_select_list_item_image);
    CheckBox cb = (CheckBox) vi
            .findViewById(R.id.restaurant_multi_select_checkBox);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                status.set(position, true);
            } else {
                status.set(position, false);
            }
        }
    });
    cb.setChecked(status.get(position));
    boolean allFalse = true;
    int j = 0;
    while (j < status.size() && allFalse) {
        if (status.get(j))
            allFalse = false;
        j++;
    }
    if (allFalse)
        layout.setVisibility(LinearLayout.INVISIBLE);
    else
        layout.setVisibility(LinearLayout.VISIBLE);

    HashMap<String, String> restaurant = data.get(position);
    name.setText(restaurant.get("name"));

    image.setImageResource(Integer.parseInt(restaurant.get("image")));
    return vi;
}

}
check the allFalse variable

in the main activity

layout = (LinearLayout) findViewById(R.id.llButtons);

        lvRestaurants = (ListView) findViewById(R.id.lvRestaurants);

        ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < restaurants.length; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("image", R.drawable.mcdonalds + "");
            hm.put("name", restaurants[i]);
            alist.add(hm);
        }
        RestaurantAdapter ra = new RestaurantAdapter(this, alist, layout);
        lvRestaurants.setAdapter(ra);

this is the xml

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

    <ListView
        android:id="@+id/lvRestaurants"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="0dip" >
    </ListView>

    <LinearLayout
        android:id="@+id/llButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <Button
            android:id="@+id/bDone"
            android:layout_width="153dp"
            android:layout_height="match_parent"
            android:text="Done" />

        <Button
            android:id="@+id/bCancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Cancel" />
    </LinearLayout>

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:10:11+00:00Added an answer on June 17, 2026 at 9:10 pm

    Use setVisibility(View.GONE) to make the buttons not only be hidden, but stop occupying space in the layout, if that is what you mean by “and a black appears instead of them”.

    As for them not re-appearing when they are checked, you need to either add a listener on your status object or call ra.notifyDataSetChanged() to have the listview redrawn (which will result in the onDraw() logic being executed).

    Edit: It might be easiest for you to declare your onCheckedChangeListener outside the adapter and use that. Use an integer to record the number of checked boxes.

    private int checkedBoxs = 0;
    private final CheckBox.OnCheckedChangeListener listener = 
        new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    status.set(position, true);
                    checkedBoxs++;
                    if (layout.getVisibility() == View.GONE)
                        layout.setVisibility(View.VISIBLE);
                } else {
                    status.set(position, false);
                    checkedBoxs--;
                    if (checkedBoxs == 0)
                        layout.setVisibility(View.GONE);
                }
            }
        };
    

    And then do cb.setOnCheckedChangeListener(listener) in your onDraw(). Eliminate all other allFalse logic from your onDraw()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listView with custom objects defined by the xml-layout below. I want
I want to divide layout for two parts in first I will have only
I have a layout defined with some buttons, and a ListView widget. I also
I have a LinearLayout layout with a ListView in it. I've made the android:background
I have an xml layout which I use for each row in my listview:
enter code here I have a ListView that have a different Layout XML for
I have a ListView with a custom layout for the rows (list items) and
I have a listView, where each row has a button in the row layout.
i have a problem with a listView that is shown deep under many layers
I have layout like this At the top i have listView ,below that i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.