Android的listActivity onListItemClick使用复选框 [英] Android listActivity onListItemClick with CheckBox

查看:325
本文介绍了Android的listActivity onListItemClick使用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何访问和更改复选框模式在listactivity任何项目。我有一个复选框和一个TextView一个XML模板文件,这些定义一行。这里的我试图到目前为止什么:

my question is how to access and change the checkBox mode for any item in a listactivity. I have an XML template file with a checkbox and a textview, and these define a row. Here's what I'm trying so far:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
        Toast.makeText(this, "You selected: " + Integer.toString(position), Toast.LENGTH_LONG).show();

        CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); 
        if (checkbox.isChecked() == false) {
            checkbox.setChecked(true); 
        } else {
            checkbox.setChecked(false); 
        }

}

显然,虽然使用R.id.checkbox只是切换的第一个复选框(实际上,它确实我看着我的屏幕上的列表中的任何部分的第一个复选框)。我不知道什么函数用来得到任何行的复选框虽然。敬酒工作正常BTW,所以至少要正确地注册的位置。

Obviously though using R.id.checkbox only toggles the first checkbox (actually, it does the first checkbox of whatever part of the list I'm looking at on my screen). I'm not sure what function to use to get the checkbox of any row though. The Toast works fine btw, so at least it registers position properly.

感谢您的帮助。

编辑 - 现在我试图子类SimpleCursorAdapter更好地控制我想要的行为。下面是该子类:

EDIT - I'm now trying to subclass the SimpleCursorAdapter to better control the behaviour I want. Here is that subclass:

public class musicPlaylist extends SimpleCursorAdapter {

private Cursor c;
private Context context;
private ArrayList<String> checkList = new ArrayList<String>();
private final static int SELECTED = 1;
private final static int NOT_SELECTED = 0;

public musicPlaylist(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.c = c;
    this.context = context;

}

public View getView(int pos, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.song_item, null);
    }

    this.c.moveToPosition(pos);     
    int columnIndex = this.c.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
    String song = this.c.getString(columnIndex);

    TextView sTitle = (TextView) v.findViewById(R.id.text1);
    sTitle.setText(song);
    v.setId(NOT_SELECTED);
    v.setTag(song); 
    v.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            if (v.getId() == NOT_SELECTED) {
                v.setId(SELECTED);
                Toast.makeText(context, "Test: " + v.getId(), Toast.LENGTH_SHORT).show();  
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));

            } else {
                v.setId(NOT_SELECTED);
                Toast.makeText(context, "Test: " + v.getId(), Toast.LENGTH_SHORT).show();  
                v.setBackgroundColor(Color.parseColor("#000000"));
            }

        }
    });

    return v; 
}

}

和参考,这里是我制作ListActivity的XML:

And for reference, here is the XML of the ListActivity I'm making:

<?xml version="1.0" encoding="utf-8"?>

 <ListView android:id="@android:id/list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"
           android:fastScrollEnabled="true"
           android:cacheColorHint="#00000000"
           />

 <TextView android:id="@android:id/empty"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:text="No data"/>

当前的行为:从SD卡上的歌曲列表被制作成一个漂亮的滚动列表。我从getView()的的onClick得到一定程度适当回应:我第一次单击一个项目,它祝酒词,它的标签是1,其背景变为白色,而我第二次举杯同一个项目,我得到 0和背景变黑,这是因为预期。

Current behaviour: the list of songs from the SD card is made into a nice scrollable list. I do get somewhat proper responses from getView()'s onClick: The first time I click an item, it Toasts that its tag is "1" and its background goes white, while the second time I toast the same item, I get "0" and the background goes black, which is as expected.

问题是如果我选择项目1(使其白字),然后向下滚动,我会注意到,第11项,item21,item31,...等也有白色的背景。但是,当我点击他们,他们的ID属性变为1,这意味着他们已经在技术上从来没有被点击!所以基本上,当滚动刷新,以10下一张表,它复制了前10的配色方案...?

The problem is if I select item1 (making its background white) and then scroll down, I'll notice that item11, item21, item31, ... , etc ALSO have white backgrounds. But when I click on them, their ID attribute goes to "1", meaning they've technically never been clicked before! So basically when the scroll "refreshes" to the next list of 10, it copies the color scheme of the first 10...?

希望我解释清楚了。

推荐答案

我觉得这有点更深层次的问题,而不是需要正面回答。

I think this is bit deeper question and not direct answer is needed.

那你想达到什么目的?你真的想使选定的只是你在屏幕上看到的复选框?注意这可能是pretty随机 - 列表视图只适用于那些在屏幕上看到的复选框项目的看法和他们每当该项目外屏幕滚动其他项目重用。

What do you want to achieve? Do you really want to make selected ONLY the checkboxes that you see on screen? Mind that this might be pretty random - list view only holds item views for the checkboxes that are visible on screen and they are reused for other items whenever the item is scrolled outside the screen.

我说,几乎可以肯定,你需要改变所有的复选框的状态在列表中(即使是那些不可见)或其中的某个子集(如部分)。从而真正转化为正确的方法应该做的:

I'd say that almost for sure you need to change state of all the checkboxes in your list (even those not visible) or some subset of them (like section). Which really translates into the proper way it should be done:


  • 适当修改的数据模型
    标志着相应的数据模型元素中选择相应的标志
    (有些布尔值,你每个项目的存储)

  • 您的适配器上调用notifyDataSetChanged()。

其结果是,列表视图将重新所有这一切都显示在屏幕上的意见。假设你的getView()中的适配器正确写的,它会读取正确的模型和更新的项目适当选中状态。
通过notifyDataSetChanged - 如果你有10个项目显示在屏幕上,你将有10倍getView()呼吁可见每个项目。

As a result, list view will recreate all the views which are visible on screen. Assuming that your "getView()" in adapter is written correctly, it will read the right model and update checked state on the item appropriately. By notifyDataSetChanged - if you have 10 items visible on screen you will have 10 times getView() called for every item visible.

这篇关于Android的listActivity onListItemClick使用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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