单击Row事件,其中该行内的复选框为子元素

时间:2017-06-17 10:39:42

标签: android listview

我可以保证我已经搜索并尝试了代码片段来解决我的问题,但似乎没有按照我的要求工作。 我有一个包含组件的列表视图,自定义行被“膨胀”到列表视图父级中。 2 TextViews和一个复选框。复选框确实做出了适当的反应,但我需要显示一个Toast(现在很简单),并设置选中该特定行的复选框。

这是我的onCreate方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_song_selector);

    //Create the model 
    songSelectorModel = new SongSelectorModel();

    songSelectorModel.setCatalog(SongSelectorModel.getDummyCatalogData());
    listViewSongCatalog = (ListView) findViewById(R.id.lvCatalog);


    listViewSongCatalog.setAdapter(new CatalogListViewAdapter(songSelectorModel.getCatalog()));


    Button btnViewPlayList = (Button) findViewById(R.id.btnViewPlayList);
    btnViewPlayList.setOnClickListener(new ButtonViewPlayListOnClickHandler());

}

这是我自定义适配器的getView方法:

public View getView(int position, View convertView, ViewGroup parent) {
        final ViewItem rowViewComponents;

        if (convertView == null) {
            convertView = SongSelectorActivity.this.getLayoutInflater().inflate(R.layout.song_list_item, null);
            rowViewComponents = new ViewItem();
            //TODO 02
            //Set the components of rowViewComponent from the "song_list_item" layout
            rowViewComponents.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox01);
            rowViewComponents.textViewSongTitle = (TextView) convertView.findViewById(R.id.tvSongTitle);
            rowViewComponents.textViewSongLength = (TextView) convertView.findViewById(R.id.tvSongLength);

            listViewSongCatalog.setOnItemClickListener(new MyOnItemClickListener());

            //Register the handlers for the buttons on the rowView
            rowViewComponents.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int position;
                    View rowView;
                    if (isChecked) {

                        rowView = (View) buttonView;
                        position = listViewSongCatalog.getPositionForView(rowView);
                        Song selectedSong = songSelectorModel.getCatalog().get(position);
                        selectedSong.setSelected(true);
                        songSelectorModel.getPlayList().add(selectedSong);
                    } else {
                        rowView = (View) buttonView;
                        position = listViewSongCatalog.getPositionForView(rowView);
                        Song selectedSong = songSelectorModel.getCatalog().get(position);
                        selectedSong.setSelected(true);
                        songSelectorModel.getPlayList().remove(selectedSong);

                    }
                }
            });


            convertView.setTag(rowViewComponents);
        } else {
            rowViewComponents = (ViewItem) convertView.getTag();
        }


        // Get the song at the selected position from the songCatalog and 
        Song songForThisRowPosition = songCatalog.get(position);

        rowViewComponents.textViewSongTitle.setText(songForThisRowPosition.getTitle());
        rowViewComponents.textViewSongLength.setText(songForThisRowPosition.getLength() + " secs");



        return convertView;
    }

最后是事件处理程序,非常基本,只是一个Toast来检查它是否正常工作但是没有任何反应。

public class MyOnItemClickListener implements AdapterView.OnItemClickListener {


    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(SongSelectorActivity.this,"Clicked on a Row!",Toast.LENGTH_SHORT).show();
    }

}

我尝试过不同的片段,我必须设置listViewSongCatalgo.setDescendantFocusability - 所有这三种可能性,但没有一个能给我带来理想的效果。 在Stackoverflow以外的网站上提供相同的答案,仍然没有结果。 我很感激有关如何继续的一些见解。

亲切的问候

0 个答案:

没有答案