Question

I have ArrayAdapter, where I set array of files:

adapter = new ArrayAdapter<Item>(this,
            R.layout.file_manager, R.id.checkedTextItem,
            fileList) 
            {


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // creates view
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item, null);
            view.setFocusable(false);
            CheckedTextView textView = (CheckedTextView) view
                    .findViewById(R.id.checkedTextItem);

            // put the image on the text view
            textView.setCompoundDrawablesWithIntrinsicBounds(
                    fileList[position].icon, 0, 0, 0);

            textView.setTextColor(Color.WHITE);
            textView.setText(fileList[position].file);
            if(fileList[position].icon == R.drawable.directory_icon)
                textView.setCheckMarkDrawable(null);
            else if(fileList[position].icon == R.drawable.directory_up)
                textView.setCheckMarkDrawable(null);

            // add margin between image and text (support various screen
            // densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            textView.setCompoundDrawablePadding(dp5);

            textView.setFocusable(false);

            return view;

        }
    };

Then I want to change checkable state of CheckedTextView by item clicking. I do it like this:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     setContentView(R.layout.file_manager);
    lv = (ListView)findViewById(R.id.fileManagerList);
    loadFileList();
    file_list = findViewById(R.id.filesList);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv.setFocusable(false);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
            //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString());
            chosenFile = fileList[myItemInt].file;
            File sel = new File(path + "/" + chosenFile);

            if (sel.isDirectory()) {

                firstLvl = false;


                str.add(chosenFile);

                fileList = null;
                path = new File(sel + "");
                //adapter.notifyDataSetChanged();
                loadFileList();
                lv.setAdapter(adapter);


            }
            else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {

                // present directory removed from list
                String s = str.remove(str.size() - 1);

                // path modified to exclude present directory
                path = new File(path.toString().substring(0,
                        path.toString().lastIndexOf(s)));
                fileList = null;

                // if there are no more directories in the list, then
                // its the first level
                if (str.isEmpty()) {
                    firstLvl = true;
                }

                loadFileList();
                lv.setAdapter(adapter);
            }
            else
            {
                View view = myAdapter.getChildAt(myItemInt);
                CheckedTextView textView = (CheckedTextView) view
                        .findViewById(R.id.checkedTextItem);        
                Toast toast = Toast.makeText(getApplicationContext(),textView.isChecked(), Toast.LENGTH_LONG);
                toast.show(); 

                textView.toggle();

                textView.setChecked(true);
            }

    }

});


}

The click event is handling properly. I can get the CheckedTextView item in else statement of onCreate() method - I detect its id and also can get the text of it. But when I try to toggle it or setCheckable - it can't be handling. Nevertheless, when I toggle or setCheckable of all items in my adapter - check state can be changed...What can be the reason of it?

Was it helpful?

Solution

because Android reuses views. When your list of checkable items is scrolled beyond the screen, the new items showing at the bottom are reused from previous ones. You need to keep track of the state of each item (a sparse array works well) and in each view, check the state from the array and set the checkedtextview accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top