【问题标题】:Checkbox in Custom Adapter does not update自定义适配器中的复选框不更新
【发布时间】:2015-01-25 14:36:00
【问题描述】:

我有一个包含复杂布局项的列表视图。每个项目都有一个复选框和其他内容,例如 TextView 和 ImageView。为了解决复选框的滚动问题,我将标签设置为

holder.checkBox.setTag(this.getItem(position).getId());

这样,当我上下滚动时,复选框的状态会保持不变。但是,当我删除一个项目时,假设它是第一行,然后我调用 API 再次加载列表,第一行(之前是第二行)的复选框仍然被选中。重新加载数据后如何重置每个复选框的状态?

更新

我的视图

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if ( convertView == null ) {
        holder = new ViewHolder();

        convertView = LayoutInflater.from(this.getContext()).inflate(
                R.layout.list_item_conversation, parent, false);

        holder.avatar = (ImageView) convertView.findViewById(R.id.img_conver_ava);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_conversation);
        holder.msg = (TextView) convertView.findViewById(R.id.tv_conver_msg);
        holder.sender = (TextView) convertView.findViewById(R.id.tv_conver_sender);
        holder.timeSent = (TextView) convertView.findViewById(R.id.tv_conver_time_sent);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.checkBox.setTag(this.getItem(position).getId());

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //Save item's id into sharedPreference
        }
    });
    return convertView;
}

我的布局 xml:

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

   <CheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="@dimen/et_forget_pw_margin_top"
       android:id="@+id/cb_conversation"
       android:button="@drawable/rounded_checkbox_conversation"
       android:paddingLeft="@dimen/cb_padding"/>

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

       <ImageView
           android:id="@+id/img_conver_ava"
           android:layout_width="@dimen/img_conver_ava"
           android:layout_height="@dimen/img_conver_ava" />

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           android:padding="@dimen/cb_padding">

           <TextView
               android:id="@+id/tv_conver_sender"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textAppearance="@android:style/TextAppearance.Large"/>

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

           <TextView
               android:id="@+id/tv_conver_time_sent"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textAppearance="@android:style/TextAppearance.Small"/>

       </LinearLayout>

   </LinearLayout>

【问题讨论】:

  • 请从您的 xml 中粘贴您的自定义适配器和复选框
  • 可以通过写notifyDatasetChanged()来刷新listVIew;在您要删除它的适配器中
  • stackoverflow.com/a/27148650/2819233 刷新listView,问题就解决了
  • 我的情况是我允许用户先选择多行,然后调用 API 删除这些项目。之后,我调用另一个 API 来获取数据以使用 notifyDataSetChanged() 再次填写列表。数据已刷新,但复选框的状态未刷新。

标签: android checkbox android-listview


【解决方案1】:

这是我的代码,请理解它,因为这段代码使用了我的变量,但它准确地告诉了你愿意找到的 senarioa:

基本上,下面的代码正在做的是,当单击 chkBox 时,它会将项目添加到收藏夹并在取消选中时删除,所以请在下面找到 getView() n 适配器的代码:

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

    // Planet to display
    IncidentItems item = mResult.get(position);

    TextView graphName;
    TextView colorPanel;
    CheckBox checkBox;
    ImageView icon;

    // Create a new row view
    if (convertView == null) {
        convertView = mInflator.inflate(R.layout.home_list_row, null);

        // Find the child views.
        graphName = (TextView) convertView.findViewById(R.id.textView1);
        checkBox = (CheckBox) convertView.findViewById(R.id.favorite_icon);
        colorPanel = (TextView) convertView
                .findViewById(R.id.tv_color_panel);
        icon = (ImageView) convertView.findViewById(R.id.icon);

        // Optimization: Tag the row with it's child views, so we don't have
        // to
        // call findViewById() later when we reuse the row.
        convertView
                .setTag(new Holder(graphName, checkBox, colorPanel, icon));

        // If CheckBox is toggled, update the planet it is tagged with.
        checkBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                final IncidentItems planet = (IncidentItems) cb.getTag();

                // ////////////////////////////////////////////////////////////

                SharedPreferences mAppSharedPref;
                mAppSharedPref = mContext.getSharedPreferences(
                        "AppSharedPref", Context.MODE_PRIVATE);
                final Editor mEditor;
                mEditor = mAppSharedPref.edit();
                final Gson gson = new Gson();
                Type type = new TypeToken<List<FavoiteGraphInfo>>() {
                }.getType();
                favoiteGraphInfoList = gson.fromJson(
                        mAppSharedPref.getString("favorite", ""), type);
                if (favoiteGraphInfoList == null)
                    favoiteGraphInfoList = new ArrayList<FavoiteGraphInfo>();

                if (cb.isChecked()) {
                // chekbox is chked add to favorite
                                Toast.makeText(mContext,
                                        "added to favorite",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });

                        /* end prompting user */

                    } else {

                    // Add item to favorite when chked
                        Toast.makeText(mContext, "added to favorite",
                                Toast.LENGTH_SHORT).show();

                    }
                } else {

                // remove item when it is uncheaked
                    Toast.makeText(mContext, "Removed from Favorites",
                            Toast.LENGTH_LONG).show();
                }

                // ////////////////////////////////////////////////////////////

                planet.setChecked(cb.isChecked());
            }
        });
    }

    // Reuse existing row view
    else {
        // Because we use a ViewHolder, we avoid having to call
        // findViewById().
        Holder viewHolder = (Holder) convertView.getTag();
        checkBox = viewHolder.getCheckBox();
        graphName = viewHolder.getGraphName();
        colorPanel = viewHolder.getColorPanel();
        icon = viewHolder.getIcon();
    }

    // Tag the CheckBox with the Planet it is displaying, so that we can
    // access the planet in onClick() when the CheckBox is toggled.
    checkBox.setTag(item);

    // Display planet data
    checkBox.setChecked(item.isChecked());
    graphName.setText(item.getGeneric_name());

    // set font to textview

    Typeface font = Typeface.createFromAsset(mContext.getAssets(),
            "Simplified_Rg.ttf");
    graphName.setTypeface(font);

    // set colors

    colorPanel.setBackgroundColor(mContext.getResources().getColor(
            GRID_COLORS[position % 6]));

    // set icons
    final String graphType = item.generic_type;

        icon.setBackgroundResource(R.drawable.pie);


    return convertView;



}

持有人类别:

public class Holder {

    private TextView graphName;
    private TextView colorPanel;
    private CheckBox checkBox;
    private ImageView icon;

    public Holder() {
    }

    public Holder(TextView tvName, CheckBox chkBov, TextView tvColorPanel,
            ImageView icn) {
        this.checkBox = chkBov;
        this.graphName = tvName;
        this.colorPanel = tvColorPanel;
        this.icon = icn;
    }

    public TextView getGraphName() {
        return graphName;
    }

    public void setGraphName(TextView graphName) {
        this.graphName = graphName;
    }

    public TextView getColorPanel() {
        return colorPanel;
    }

    public void setColorPanel(TextView colorPanel) {
        this.colorPanel = colorPanel;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox) {
        this.checkBox = checkBox;
    }

    public ImageView getIcon() {
        return icon;
    }

    public void setIcon(ImageView icon) {
        this.icon = icon;
    }

}

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 2014-03-20
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多