更改签入列表视图时自动取消选中复选框

时间:2014-08-03 08:56:28

标签: android listview checkbox android-listview

我使用textview和复选框自定义列表视图。现在我想要当更改检查复选框时,将检查不同的旧检查(在列表视图中只检查了一个项目)。我没有决心。现在我需要帮助!

抱歉。我说英语不好

在这里!适配器中的代码

public class ListSongCheckDownloadAdapter extends ArrayAdapter<String>
        implements OnCheckedChangeListener {
    private ArrayList<String> listnameSong;
    private Context context;
    public SparseBooleanArray mCheckStates;
    int layoutResourceId;

    public ListSongCheckDownloadAdapter(Context context, int resource,
            int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.layoutResourceId = resource;
        this.listnameSong = new ArrayList<String>(objects);
        mCheckStates = new SparseBooleanArray(listnameSong.size());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder viewholder;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listsongdownload_layout, null);
            viewholder = new ViewHolder();
            viewholder.tvNameSong = (TextView) rowView
                    .findViewById(R.id.nameOfSong);
            viewholder.cbChecked = (CheckBox) rowView
                    .findViewById(R.id.cbCheckSong);
            rowView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) rowView.getTag();
        }
        viewholder.tvNameSong.setText(listnameSong.get(position));
        viewholder.cbChecked.setTag(position);
        viewholder.cbChecked.setChecked(mCheckStates.get(position, false));
        viewholder.cbChecked.setOnCheckedChangeListener(this);
        return rowView;
    }

    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);
    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }

    static class ViewHolder {
        TextView tvNameSong;
        CheckBox cbChecked;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mCheckStates.put((Integer) buttonView.getTag(), isChecked);
    }


}

这是下载活动

public class DownloadAlbumActivity extends Activity implements
        OnItemClickListener {
    private ArrayList<String> nameSongInList;
    private ArrayList<String> linkDownOfAlbum;
    ListSongCheckDownloadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.downloadalbum_layout);
        nameSongInList = new ArrayList<String>();
        linkDownOfAlbum = new ArrayList<String>();
        Intent intent = getIntent();
        nameSongInList = intent.getStringArrayListExtra("namesong");
        linkDownOfAlbum = intent.getStringArrayListExtra("linkdown");
        ImageButton btnChecked = (ImageButton) findViewById(R.id.btnChecked);
        ListView lvAdapter = (ListView) findViewById(R.id.lvSongOfAlbum);
        lvAdapter.setItemsCanFocus(false);
        adapter = new ListSongCheckDownloadAdapter(DownloadAlbumActivity.this,
                R.layout.listsongdownload_layout, R.id.tvNameSong,
                nameSongInList);
        lvAdapter.setAdapter(adapter);
        btnChecked.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String result = "";
                for (int i = 0; i < adapter.mCheckStates.size(); i++) {
                    if (adapter.mCheckStates.get(i) == true) {
                        result = linkDownOfAlbum.get(i).toString();
                    }
                }
                Toast.makeText(DownloadAlbumActivity.this, result,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,
            long arg3) {
        adapter.toggle(position);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以将当前检查项目的ID保存在变量中。然后,当检查另一个项目时(在监听器内),您只需取消选中旧项目并将变量更改为新ID。