CheckedTextView无法在setOnItemClickListener onCreate方法中切换

时间:2012-08-29 09:30:41

标签: android toggle oncreate onclicklistener checkedtextview

我有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所有项目时 - 检查状态可以改变......它可能是什么原因?

1 个答案:

答案 0 :(得分:1)

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