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

  • SEARCH
  • Home
  • 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 8235137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:42:11+00:00 2026-06-07T18:42:11+00:00

I am trying to create a ListView containing directory name. The ListView contains a

  • 0

I am trying to create a ListView containing directory name. The ListView contains a TextView to display the Folder name and a checkbox.
The List is created successfully but I’m facing the problem with state of the checkbox when I scroll the list up/down. The states of the checkboxes changes randomly.
To save the state of the checkboxes I’m using a HashMap<String, Boolean>, String continig the name of the directory and boolean – the checkbox state.

Below is the code (Sorry for the length):
MainActivity.java

public class MainActivity extends ListActivity implements OnClickListener {

    Button done, cancel, selectAll;

    ArrayList<String> m_list;
    HashMap<String, Boolean> m_checkedMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_checkedMap = new HashMap<String, Boolean>();

        done = (Button) findViewById(R.id.bDone);
        cancel = (Button) findViewById(R.id.bCancel);
        selectAll = (Button) findViewById(R.id.bSelAll);
        done.setOnClickListener(this);
        cancel.setOnClickListener(this);

        new AsyncHandler(this).execute();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(this, m_list.get(position), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.bDone:
            //Update CheckedMap in DB
            Toast.makeText(this, m_checkedMap.values().toString(),
                    Toast.LENGTH_LONG).show();
            break;
        case R.id.bCancel:
            this.finish();
            break;
        }
    }

    public class AsyncHandler extends AsyncTask {

        Context context;
        public AsyncHandler(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            getList();
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            // super.onPostExecute(result);
            setListAdapter(new ElementAdapter(context, m_list, m_checkedMap));
        }

        private void getList() {
            m_list = new ArrayList<String>();
            File root = new File("/");
            File[] files = root.listFiles();
            for (File f : files) {
                if (f.isDirectory() && !f.isHidden()) {
                    m_list.add(f.getName());
                    m_checkedMap.put(f.getName(), false);
                }
            }
            Collections.sort(m_list);
        }
    }
}

Adapter class

public class ElementAdapter extends ArrayAdapter {

    LinearLayout rowLayout;
    ArrayList<String> items = new ArrayList<String>();
    TextView tvElement;
    HashMap<String, Boolean> checkedMap;
    CheckBox cBox; 
    Context context;

    public ElementAdapter(Context c, ArrayList<String> elements,
            HashMap<String, Boolean> m_checkedMap) {
        super(c, R.layout.row, elements);
        this.items = elements;
        this.checkedMap = m_checkedMap;
        this.context = c;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.row, null);
        }

        rowLayout = (LinearLayout) view.findViewById(R.id.row);
        tvElement = (TextView) view.findViewById(R.id.tvElement);
        tvElement.setText(items.get(position));
        cBox = (CheckBox) view.findViewById(R.id.checkBox1);
        cBox.setOnClickListener(new OnClickListener() {
            public void onClick(View paramView) {
                if (cBox.isChecked()) {
                    //checkedMap.put(tvElement.getText().toString(), false);
                    checkedMap.put(items.get(position), false);
                } else if (!cBox.isChecked()) {
                    //checkedMap.put(tvElement.getText().toString(), true);
                    checkedMap.put(items.get(position), true);
                }
            }
        });
        cBox.setChecked(checkedMap.get(items.get(position)));
        return view;
    }
}
  • 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-07T18:42:13+00:00Added an answer on June 7, 2026 at 6:42 pm

    I had created a blog post for the same reason of scrolling issue of CheckBox with ListView,
    ListView with CheckBox Scrolling Issue

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

Sidebar

Related Questions

I am trying to create a listview that receives a flagged list of items
I am trying to create a ListView with CheckBox's...Th ListView should allow the user
Hello I am trying to create a custom expandable listview, but the documentation on
I am trying to display ListView . I have created one activity , in
I am trying to create a simple Icon+Text ListView but it does not work.
I'm trying to create a layout for a ListView, with a checkbox to the
I'm trying to create a listview within a customdialog. listview contains a custom row
Im trying to create a Spinners within a ListView, but i dont know how
I am trying to create an expandable listview similar to the expandable list shown
Hello i am trying to create aplication witch shows me sms in ListView but

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.