简体   繁体   English

在ListView中获取Switch状态

[英]Getting Switch state in ListView

in my Application I want the user to make multiple choices and for this i present some Switch in a ListView and a button to confirm. 在我的应用程序中,我希望用户做出多个选择,为此,我在ListView中显示一些Switch和一个确认按钮。

The problem is, when I inserted the values in db in the Button onClickListener() function, i can retrieve from the switches the text, but not the state of them (in the example, always output true) 问题是,当我在Button onClickListener()函数中的db中插入值时,我可以从开关中检索文本,但不能检索它们的状态(在示例中,始终输出true)

btn.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ListAdapter adapter = list.getAdapter();
            MyDatabase db = new MyDatabase(cntx);
            db.open();
            for(int n=0;n<adapter.getCount();n++)
            {
                View rowview = adapter.getView(n, null, null);
                Switch swtc = (Switch) rowview.findViewById(R.id.switch1);
                String[] args = {(String)swtc.getText()};
                if(swtc.isChecked())
                {
                    Log.i("update", args[0]+" going to be true");
                }
                else
                {
                    Log.i("update", args[0]+" going to be false");

                }
                int a = db.updateAlert(swtc.isChecked(), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
                Toast.makeText(cntx, "affected: "+Integer.toString(a), Toast.LENGTH_LONG).show();
            }
            db.close();

        }
    });

PS: adapter is the arrayadapter I've created for the ListView, cntx is the context of the application, and MyDatabase is my custom class that provide to manage an Sqlite Database. PS:适配器是我为ListView创建的arrayadapter,cntx是应用程序的上下文,MyDatabase是提供用于管理Sqlite数据库的自定义类。 Hope you can help me 希望你能帮我

code of arrayadapter: arrayadapter的代码:

 public class NotificationElementList extends ArrayAdapter<String>{

private final Activity context;

private final List<String> titles;
private final List<String> imgs;
private final List<Boolean> alert;


public NotificationElementList(Activity context, List<String> titles, List<String> imgs, List<Boolean> alert) {

    super(context, R.layout.list_single, titles);
    this.context = context;
    this.titles = titles;
    this.imgs = imgs;
    this.alert = alert;

    }

@Override
public View getView(int position, View view, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();

    View rowView= inflater.inflate(R.layout.notification_list_element, null, true);

    Switch switch_text = (Switch) rowView.findViewById(R.id.switch1);
    switch_text.setChecked(alert.get(position));
    switch_text.setText(titles.get(position));

    return rowView;
    }

} }

As you are not providing the convertView argument to ArrayAdapter.getView method, it will always create a new view hierarchy for you. 由于您没有为ArrayAdapter.getView方法提供convertView参数,因此它将始终为您创建一个新的视图层次结构。 As a result you will always see your switch widgets' state initialized to alert.get(position) . 结果,您将始终看到开关小部件的状态已初始化为alert.get(position)

One of the potential solutions could be to set a OnCheckedChangeListener to Switch widgets and update model whenever switch state changes. 一种可能的解决方案是将OnCheckedChangeListener设置为Switch小部件并在开关状态更改时更新模型。 For example like this 像这样

@Override
public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.notification_list_element, null, true);

    Switch s = (Switch) rowView.findViewById(R.id.switch1);
    s.setChecked(alert.get(position));
    s.setText(titles.get(position));
    s.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            alert.set(position, isChecked);
        }
    });

    return rowView;
}

Doing so will allow you to obtain the state directly from model as opposed to trying to obtain it from switch widget in your button click event handler. 这样做将允许您直接从模型获取状态,而不是尝试从按钮单击事件处理程序中的开关小部件获取状态。 Instead of doing this 而不是这样做

View rowview = adapter.getView(n, null, null);
Switch swtc = (Switch) rowview.findViewById(R.id.switch1);     
db.updateAlert(swtc.isChecked(), MyDatabase.Tab_Cal_Set.Cal+"=?", args);

You would do 你会做

 db.updateAlert(alert.get(n), MyDatabase.Tab_Cal_Set.Cal+"=?", args);

In addition, I suggest you update the adapter to utilize the view holder pattern for better performance. 另外,建议您更新适配器以利用视图持有者模式以获得更好的性能。

You should not call adapter getView method. 您不应调用适配器的getView方法。 It is used internally by ListView. ListView在内部使用它。
Its purpose is say to ListView how to populate its contents. 它的目的是说ListView如何填充其内容。

If the alert list is used to save the Switch checked state you can use it. 如果alert list用于保存“ Switch选中状态,则可以使用它。

btn.setOnClickListener( new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        MyDatabase db = new MyDatabase(cntx);
        db.open();
        for(int n=0;n<alert.size();n++)
        {
            String[] args = {titles.get(n)};
            if(alert.get(n))
            {
                Log.i("update", args[0]+" going to be true");
            }
            else
            {
                Log.i("update", args[0]+" going to be false");

            }
            int a = db.updateAlert(alert.get(n), MyDatabase.Tab_Cal_Set.Cal+"=?", args);
            Toast.makeText(cntx, "affected: "+Integer.toString(a), Toast.LENGTH_LONG).show();
        }
        db.close();

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将开关按钮状态保存在可扩展列表视图中 - save switch button state in expandable listview 滚动时列表视图中的开关状态不稳定 - switch state is not stable in listview when scrolling 在列表视图中滚动时,Android的开关状态发生变化 - Android state of switch changes when scrolling in listview 在列表视图中滚动时切换按钮更改状态 - Switch button changing state when scrolling in listview 滚动Listview时切换状态 - Switch changes state when scrolling Listview 在显示列表视图后,尝试以编程方式在LISTVIEW内设置SWITCH的状态 - Trying to programmatically set the state of a SWITCH inside a LISTVIEW after the listview is displayed 获取列表视图以在各种数组之间切换以显示 - Getting a listview to switch between various arrays for display 如何在ListView的一行内的状态下更改开关状态-React Native - How to change switch status in state inside a row in a ListView - React Native ListView 带开关按钮,setChecked 启动两个开关,保存另一个开关的状态 - ListView with switch buttons, setChecked on start two switches, and save state of another 我正在使用Simple Adapter for listview但是在listview中滚动更改其状态? - I am using Simple Adapter for listview but while scrolling in listview switch changes its state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM