0

我有 ArrayAdapter,我在其中设置了文件数组:

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;

        }
    };

然后我想通过单击项目来更改 CheckedTextView 的可检查状态。我这样做:

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

    }

});


}

单击事件正在正确处理。我可以在 onCreate() 方法的 else 语句中获取 CheckedTextView 项 - 我检测到它的 id 并且还可以获得它的文本。但是当我尝试切换它或 setCheckable 时 - 它无法处理。然而,当我切换或 setCheckable 我的适配器中的所有项目时 - 可以更改检查状态......这可能是什么原因?

4

1 回答 1

1

因为Android重用了视图。当您的可检查项目列表滚动到屏幕之外时,底部显示的新项目将从以前的项目中重复使用。您需要跟踪每个项目的状态(稀疏数组效果很好),并在每个视图中检查数组的状态并相应地设置 checkedtextview。

于 2012-08-29T10:33:00.083 回答