如何获取切换按钮列表视图中点击的位置? [英] How to get the position of toggle button clicked within listview?

查看:151
本文介绍了如何获取切换按钮列表视图中点击的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含报警次ListView和一个切换按钮来关闭/开启了报警。当我点击的具体列表视图切换按钮,我想设置/取消特定的报警。但是,我怎么得到我点击基于视图列表视图的位置?

I have a listview that contains alarm times and a toggle button to turn off/on that alarm. When I click the toggle buttons for the specific listview, I would like to set/cancel that specific alarm. But how do I get the position of the listview I clicked on based on the view?

当我点击我的切换按钮开启/关闭它击中这个code:

When I click my toggle button on/off it hits this code:

public void enableAlarm(View view) {

    // set/cancel alarm manager pending intent
}

我保存在数据库中每一个特定的ListView项一些信息,但我需要的列表视图位置。我怎样才能从视图中的位置?

I have some information stored for each particular listview item in a Database, but I need the listview position. How can I get the position from the view?

推荐答案

我建议你创建一个自定义的 ArrayAdapter 类和重写 getView() 方法。从这个方法,您可以访问从中切换设置列表项的位置,并能创造出独特的 OnCheckedChangeListener 每个切换按钮,通过它的位置列表

I suggest you create a custom ArrayAdapter class and override the getView() method. From that method, you have access to the position of the list item from which the toggle was set, and can create a unique OnCheckedChangeListener for each toggle button, passing it the position of the list.

我假设你已经拥有的任何假设每个ListView项将举行一个XML布局文件,因为你提到的报警和切换按钮。

I'm assuming you already have an XML layout file for whatever is supposed to be held in each ListView item, since you mention an alarm and toggle buttons.

更新:
为了得到报警时间,并将其发送到 enableAlarm()方法,你需要将其保存为最后 getView()这样你就可以在 onCheckedChangeListener 访问它。再看code我得到的切换按钮后添加。

Update: In order to get the alarm time and send it to the enableAlarm() method, you need to save it as a final variable within getView() so you can access it within the onCheckedChangeListener. Look at the code I added after getting the ToggleButton.

@override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = getContext().getLayoutInflater();
        v = inflater.inflate(R.layout.alarm_item, parent, false);
    }
    ToggleButton toggle = (ToggleButton) v.findViewById(R.id.alarmToggle);

    //get the alarm time
    TextView timeView = (TextView) v.findViewById(R.id.theAlarmTextView);
    final String alarmTime = timeView.getText();

    toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                //modify your enableAlarm method to take in the time as a String
                enableAlarm(buttonView, alarmTime);
            }
        }
    });
    return v;
}

这篇关于如何获取切换按钮列表视图中点击的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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