Android,当用户设置复选框时如何重绘列表?

标签 android listview checkbox redraw

我有一个 ListView ,每行包含两个 TextView 和一个复选框。 我只需要用户设置一个复选框,如果用户设置其他复选框,则应清除前一个复选框并设置第二个复选框。

我可以生成列表,但我不知道为什么 onListItemClick() 方法不起作用?因此,我不知道设置了哪个复选框。

我的下一个问题是,如何在用户单击复选框后重绘列表?

我的代码是:

    package com.Infindo.DRM;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class Infindo_PackageActivity extends ListActivity {

    private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
    private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
    private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
    private ListAdapter listAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infindo_packagepage);

        listAdapter = new ListAdapter(this);
        setListAdapter(listAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.i("List Clicked:", "******");
    }

    public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
    }


    //*********************
    //  RowModel Class
    //*********************
    private class RowModel{
        TextView title;
        TextView description;
        CheckBox checkbox;
    }

    //*********************
    //  ListAdapter Class
    //*********************
    private class ListAdapter extends ArrayAdapter<String>{

        public ListAdapter(Context c) {
            super(c, R.layout.infindo_listformat, titels);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RowModel holder;
            View row = convertView;

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.infindo_listformat, parent, false);

                holder = new RowModel();
                holder.title = (TextView) row.findViewById(R.id.title);
                holder.description = (TextView) row.findViewById(R.id.description); 
                holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);

                row.setTag(holder);

            } else {
                holder = (RowModel) row.getTag(); 
            }

            holder.title.setText(titels[position]);
            holder.description.setText(descriptions[position]);
            String s = flag[position];
            if(s.equalsIgnoreCase("false")) 
                holder.checkbox.setChecked(false);
            else
                holder.checkbox.setChecked(true);

            return(row);
        }
    }
}

更新: 我已经添加了

@Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            Log.i("List Redrawed:", "**notifyDataSetChanged()**");
        }

进入我的列表适配器类并调用它

public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
        listAdapter.notifyDataSetChanged();
    }

我认为重绘的问题已经解决了。但我仍然不知道用户单击了哪个复选框以更改标志数组。

最佳答案

您可以使用 setOnCheckedChangeListener 来了解选中了哪个复选框。您可以查看带有 CheckBox 的 ListView 的完整示例 here .

更新

为了使您的 onListItemClick() 工作,您需要为 ListView 的其他项目编写 android:focusable="false",这是因为其他 View 的焦点ListView 的 onListItemClick() 未按应执行的方式执行。

检查这个答案,为什么 Android custom ListView unable to click on items

关于Android,当用户设置复选框时如何重绘列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320663/

相关文章:

java - 单击项目时 ListView 在纵向模式下崩溃(使用 fragment )

ruby-on-rails - Ruby on Rails - 复选框未保存到数据库?

android - 对用于显示列表的 Textview 和 Listview 感到困惑

android - 数组中的语言翻译

java - ListView OnItemClick 事件

java - 根据屏幕方向在微调器和 ListView 之间切换

android - 联系人与原始联系人有什么区别?

android - 运行时权限 - 为已授予的权限请求权限

ruby-on-rails - Rails 字段(哈希类型)和多个复选框

java - 创建复选框框架并等待选择