【问题标题】:How to load and save checkbox values in a listview如何在列表视图中加载和保存复选框值
【发布时间】:2014-04-14 22:42:22
【问题描述】:

我有一个充满文本的列表视图,但每一行都有一个复选框。当onCreate() 被调用时,我希望它从sharedpreferences 加载复选框值并将它们应用于复选框。此外,当调用 onPaused() 时,我希望它保存复选框值。

有人知道怎么做吗???

附:我一直在阅读一些东西,我找到了自定义的adapters,但我不太明白它们的意思。如果有人能解释一下,我也将不胜感激。

【问题讨论】:

  • 你用 onscrolllistener 试过了吗?
  • 不,但听起来像那样,那不是我要找的东西......

标签: android listview checkbox sharedpreferences


【解决方案1】:

在扩展自定义 Adapter 时,您正在精确地实现您所描述的内容。在您的情况下,ArrayAdapter 似乎足以实现它。

这个适配器有一个getView() 方法,它将为你的每一行调用,它基本上处理这些行,实现它们应该如何显示。该方法有 3 个参数,第一个是正在渲染的行的position。第二个是View,模糊地说,代表整行的布局。第三个代表View 的父级。这样,假设您的第二个参数称为convertView,您可以执行以下操作:

CheckBox cb = convertView.findViewById(R.id.myCheckbox);

并使用适当的值设置 call cb.isChecked(...) 以显示该复选框的当前行值。

一些有用的链接:

【讨论】:

  • 好的,关于保存,但加载呢?
  • 对了,getView() 方法是什么时候调用的?
  • 不,这是关于加载的。每次呈现一行时,都会调用此方法,因此当您从 SharedPreferences 中获取值并进行相应设置时。您保存它的方式取决于您的实现,但如果您将其保存在SharedPreferences 中,则加载/存储都非常容易。如果您将值保存在数据库中,它会稍微复杂一些,但也完全可行。甚至可以从远程 Web 服务中获取它们。这在很大程度上取决于您的实施。
  • 当你的ListView 被加载时它会被自动调用。这意味着当您的 ListView 显示时,它将显示 X 行(取决于您的布局的实现),因此将为这些行中的每一行调用 getView(),修改我上面描述的相应三个参数以适应他们每个人。
  • 好的。一个(可能)最后一个问题。在哪里添加我的实现?我应该继承arrayadapter吗?
【解决方案2】:

我不熟悉带有复选框的 android 预实现列表视图。如果有,那么应该有一种方法可以根据位置检查它们。但我建议您使用自定义 ArrayAdapter 并定义您的复选框。在 getView() 中,您只需检查位置并根据位置设置复选框。

编辑:实际上,这是整个过程的示例。

MyAdapter 类:

package com.example.stackover;

import java.util.ArrayList;

import android.app.ListActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {

    ArrayList<Integer> checkBoxList;
    ListActivity mListActivity;

    public MyAdapter(ListActivity mListActivity, ArrayList<Integer> checkBoxList) {
        this.mListActivity = mListActivity;
        this.checkBoxList = checkBoxList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return checkBoxList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return checkBoxList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;
        RowWrapper wrapper = null;
        if (row==null) {
            LayoutInflater inflater = mListActivity.getLayoutInflater();
            row=inflater.inflate(R.layout.listrow, parent, false);
            wrapper=new RowWrapper(row);
            row.setTag(wrapper);
        }
        if(checkBoxList.contains(position)) {
            wrapper.getCb().setSelected(true);
        }
        return(row);
    }

}

RowWrapper 类:

package com.example.stackover;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class RowWrapper {
    View        base;
    TextView    description = null;
    CheckBox    cb    = null;

    public RowWrapper(View base) {
        this.base=base;
    }

    public TextView getDescription() {
        if (description==null) {
            description=(TextView)base.findViewById(R.id.description);
        }
        return(description);
    }
    public TextView getCb() {
        if (cb==null) {
            cb=(CheckBox)base.findViewById(R.id.cb);
        }
        return(cb);
    }

    public View getBase(){
        return this.base;
    }
}

还有你的 listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <CheckBox 
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</LinearLayout>

在您的 ListActivity 或 ListFragment 中,您创建 MyAdapter 并使用构造函数设置适配器,该构造函数从您的共享首选项中获取您的活动和复选框列表。如果你想改变描述文本视图,你可以用另一个数组来保存你的文本,并根据你的位置将它们设置在 getView() 中。

【讨论】:

  • 再三考虑,将您的数组对象设为布尔值。我不知道为什么我认为你需要一个整数类型。最后你的数组应该只表明你的行是否被选中:)
猜你喜欢
  • 2012-09-26
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 2019-10-13
相关资源
最近更新 更多