Android的ListView的多个选择和自定义适配器 [英] Android ListView with multiple select and custom adapter

查看:200
本文介绍了Android的ListView的多个选择和自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 与自定义适配器。该的ListView 允许多选,但不知何故,当一个项目被选中它不承认。

我已经做了适配器项目延长辨认,但仍 getCheckedItemPositions()返回falses的数组。

我想有一些基本的东西,我做错了,但我一直无法到目前为止发现的多选列表视图的例子,其中适配器是不使用默认布局多重选择的ArrayAdapter。

任何帮助将是非常美联社preciated。

code是如下:

主要类:

 的ListView =(ListView控件)findViewById(R.id.cardlist);

TCA =新TextCardAdapter(mInflater);
listView.setAdapter(TCA);
 

适配器:

 公共类TextCardAdapter扩展了BaseAdapter {
私人诠释计数= 0;
私人列表< CheckableCard> cardList =新的ArrayList< CheckableCard>();
私人LayoutInflater mInflater;

公共TextCardAdapter(LayoutInflater充气){
    this.mInflater =充气;
}

@覆盖
公众诠释getCount将(){
    返回计数;
}

@覆盖
公共对象的getItem(INT位置){
    返回cardList.get(位置);
}

@覆盖
众长getItemId(INT位置){
    返回的位置;
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    最后ViewHolder持有人;
    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.textlayout_row,空,假);
        convertView.setClickable(真正的);

        持有人=新ViewHolder();
        holder.text =(TextView中)convertView.findViewById(R.id.card_name);
        holder.checkbox =(复选框)convertView.findViewById(R.id.checkbox);
        convertView.setTag(保持器);
    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }

    最后CheckableCard卡= cardList.get(位置);

    holder.text.setText(card.card.toString());
    holder.checkbox.setChecked(card.isChecked());
    holder.checkbox.setOnClickListener(card.checkListener);
    convertView.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            card.checkListener.onClick(五);
            holder.checkbox.setChecked(card.isChecked());
        }
    });

    返回convertView;
}

公共无效添加(卡卡){
    cardList.add(新CheckableCard(卡));
    算上++;
    notifyDataSetChanged();
}

公共无效的addAll(集合<卡> cardColl){
    对于(卡C:cardColl)
        加(C);
}

公共无效的removeAll(){
    计数= 0;
    cardList.clear();
    notifyDataSetChanged();
}

市民卡getCard(INT位置){
    CheckableCard CC =(CheckableCard)的getItem(位置);
    如果(CC == NULL)返回NULL;
    返回cc.card;
}

公共类CheckableCard实现可检查{
    私人布尔查= FALSE;
    公众最终卡牌;
    公众最终OnClickListener checkListener;

    公共CheckableCard(卡卡){
        this.card =卡;
        checkListener =新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                切换();
            }
        };
    }

    @覆盖
    公共布尔器isChecked(){
        返回检查;
    }

    @覆盖
    公共无效setChecked(布尔检查){
        this.checked =检查;
    }

    @覆盖
    公共无效切换(){
        !检查=检查;
    }

}

静态类ViewHolder {
    TextView的文字;
    复选框复选框;
}
}
 

解决方案

只是试一试:

  1. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

  2. holder.checkbox.setFocusable(假);

  3. 我认为这是ListView的分项来看shoudld实现了可检查的接口,这意味着转换后视图(convertView)应具有可检查的所有方法。

I have a ListView with a custom adapter. The ListView allows multiple select, but somehow it's not recognising when an item is selected.

I've already made the adapter items extend Checkable, but still the getCheckedItemPositions()returns an array of falses.

I guess there's something fundamental I'm doing wrong, but I have been unable so far to find examples of multiple select ListViews where the adapter was not an ArrayAdapter using the default layout for multiple selects.

Any help would be much appreciated.

Code is below:

Main class:

listView = (ListView) findViewById(R.id.cardlist);

tca = new TextCardAdapter(mInflater);
listView.setAdapter(tca);

Adapter:

public class TextCardAdapter extends BaseAdapter {
private int count = 0;
private List<CheckableCard> cardList = new ArrayList<CheckableCard>();
private LayoutInflater mInflater;

public TextCardAdapter(LayoutInflater inflater) {
    this.mInflater = inflater;
}

@Override
public int getCount() {
    return count;
}

@Override
public Object getItem(int position) {
    return cardList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.textlayout_row, null, false);
        convertView.setClickable(true);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.card_name);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final CheckableCard card = cardList.get(position);

    holder.text.setText(card.card.toString());
    holder.checkbox.setChecked(card.isChecked());
    holder.checkbox.setOnClickListener(card.checkListener);
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            card.checkListener.onClick(v);
            holder.checkbox.setChecked(card.isChecked());
        }
    });

    return convertView;
}

public void add(Card card) {
    cardList.add(new CheckableCard(card));
    count++;
    notifyDataSetChanged();
}

public void addAll(Collection<Card> cardColl) {
    for (Card c : cardColl)
        add(c);
}

public void removeAll() {
    count = 0;
    cardList.clear();
    notifyDataSetChanged();
}

public Card getCard(int position) {
    CheckableCard cc = (CheckableCard) getItem(position);
    if (cc == null) return null;
    return cc.card;
}

public class CheckableCard implements Checkable {
    private boolean checked = false;
    public final Card card;
    public final OnClickListener checkListener;

    public CheckableCard(Card card) {
        this.card = card;
        checkListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                toggle();
            }
        };
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    @Override
    public void toggle() {
        checked = !checked;
    }

}

static class ViewHolder {
    TextView text;
    CheckBox checkbox;
}
}

解决方案

Just have a try:

  1. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

  2. holder.checkbox.setFocusable(false);

  3. I think that is the listView's sub item view shoudld implements the Checkable interface, that means the converted View(convertView) should have all the method of Checkable.

这篇关于Android的ListView的多个选择和自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆