首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android读取Firebase CheckBox的行为怪异

Android读取Firebase CheckBox的行为怪异
EN

Stack Overflow用户
提问于 2019-12-21 18:01:27
回答 1查看 122关注 0票数 0

在这个项目中,我有以下几点:

  1. MainActivity
  2. FirebaseHelper
  3. CustomAdapter
  4. Person
  5. one_line_list_item.xml

MainActivity类

代码语言:javascript
运行
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        mListView = (ListView) findViewById(R.id.list_view);
        searchView = (SearchView) findViewById(R.id.search);
        //searchView.setIconifiedByDefault(true);
        searchView.setSubmitButtonEnabled(true);
        //initialize firebase database
        db = FirebaseDatabase.getInstance().getReference();
        helper = new FirebaseHelper(db, this, mListView);

        adapter = new CustomAdapter(context, helper.people);

FirebaseHelper类

代码语言:javascript
运行
复制
public class FirebaseHelper {

    DatabaseReference db;
    Boolean saved;
    ArrayList<Person> people = new ArrayList<>();
    ListView mListView;
    Context c;
    CustomAdapter adapter;

    public FirebaseHelper(DatabaseReference db, Context context, ListView mListView) {
        this.db = db;
        this.c = context;
        this.mListView = mListView;
        this.retrieve();
    }

    public ArrayList<Person> retrieve() {
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                people.clear();
                if (dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        //Now get Person Objects and populate our arraylist.
                        Person person = ds.getValue(Person.class);
                        people.add(person);
                    }
                    adapter = new CustomAdapter(c, people);
                    mListView.setAdapter(adapter);

                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            mListView.smoothScrollToPosition(people.size());
                                                            mListView.smoothScrollToPosition(people.indexOf(0));
                        }
                    });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d("mTAG", databaseError.getMessage());
                Toast.makeText(c, "ERROR " + databaseError.getMessage(), Toast.LENGTH_LONG).show();

            }
        });

        return people;
    }
}

CustomAdapter类

代码语言:javascript
运行
复制
public class CustomAdapter extends BaseAdapter implements Filterable {

    private static final String TAG = CustomAdapter.class.getSimpleName();
    Context c;
    ArrayList<Person> people;
    ArrayList<Person> mDataFiltered;
    ArrayList<Integer> selected;

    public CustomAdapter(Context c, ArrayList<Person> people) {
        this.c = c;
        this.people = people;
        this.mDataFiltered = people;
        selected = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return people.size();
    }

    @Override
    public Object getItem(int position) {
        return people.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecyclerView.ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(c).inflate(R.layout.one_line_list_item, parent, false);
        }

        CheckBox cbId = convertView.findViewById(R.id.cbid);
        TextView nameTextView = convertView.findViewById(R.id.name);
        TextView dateTextView = convertView.findViewById(R.id.date);
        TextView descriptionTextView = convertView.findViewById(R.id.description);

        final Person s = (Person) this.getItem(position);

        Log.i(TAG, "" + s.get_id());

        nameTextView.setText(s.getName());
        descriptionTextView.setText(s.getDetails());
        dateTextView.setText(s.getDay() + " " + s.getMonth() + " " + s.getYear());


        if (cbId != null ) {

            if (selected.contains(s.get_id())) {
                cbId.setChecked(true);
            } else {
                cbId.setChecked(false);
            }

            cbId.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(c, "" + s.get_id(), Toast.LENGTH_SHORT).show();
                }
            });

        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(c, s.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        convertView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                Log.i(TAG, "convertView.getTag --- " + v.getTag());
                int selected_id = s.get_id();
                if (selected.contains(selected_id)) {
                    selected.remove(selected.indexOf(selected_id));
                    v.setBackgroundColor(ContextCompat.getColor(v.getContext(), R.color.default_color));
                    v.setSelected(false);
                    Toast.makeText(v.getContext(), "CustomAdapter REMOVED: " + selected_id + " - " , Toast.LENGTH_SHORT).show();
                } else {
                    selected.add(selected_id);
                    v.setBackgroundColor(ContextCompat.getColor(v.getContext(), R.color.selected_color));
                    v.setSelected(true);
                    Toast.makeText(v.getContext(), "CustomAdapter ADDED: " + selected_id + " - " , Toast.LENGTH_SHORT).show();
                }

                return false;
            }
        });

        return convertView;
    }
}

Person类

代码语言:javascript
运行
复制
public class Person {

    private String name;
    private String day;
    private String month;
    private String year;
    private String details;
    private int _id;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getDay() { return day; }
    public void setDay(String day) { this.day = day; }
    public String getMonth() { return month; }
    public void setMonth(String month) { this.month = month; }
    public String getYear() { return year; }
    public void setYear(String year) { this.year = year; }
    public String getDetails() { return details; }
    public void setDetails(String details) { this.details = details; }
    public int get_id() { return _id; }
    public void set_id(int _id) { this._id = _id; }
    public Person() { }
}

one_line_list_item.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:background="?android:attr/activatedBackgroundIndicator">

    <CheckBox
        android:id="@+id/cbid"
        android:layout_width="wrap_content"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="6dp"
        android:layout_marginLeft="26dp"
        android:layout_marginEnd="6dip"
        android:layout_marginRight="12dp"
        android:text="" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="26dp"
        android:layout_toStartOf="@id/cbid"
        android:layout_alignParentTop="true"
        android:lineHeight="18dp"
        android:maxLines="1"
        android:text="@string/name"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="fill_parent"
        android:layout_height="18dp"
        android:layout_below="@+id/name"
        android:layout_toStartOf="@id/cbid"
        android:lineHeight="12dp"
        android:maxLines="1"
        android:text="@string/date"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="18dp"
        android:layout_below="@id/date"
        android:layout_toStartOf="@id/cbid"
        android:lineHeight="18dp"
        android:maxLines="1"
        android:text="@string/additional_description"
        android:textSize="16sp" />

</RelativeLayout>

对不起,对于这段长代码,我只将每个Class 的必要部分放在一起,以使其尽可能清晰.

问题是:

当单击列表中的任何checkbox时,它似乎也在检查其他隐藏在视图中的内容。

我试着把这个问题隔离开来,但我想不出.

你知道它为什么还要检查其他隐藏在视线之外的物品吗??

它们以8为间隔进行检查,在任何未检查的情况下也不进行检查.

更新:尝试在MainActivity中实现这一功能

代码语言:javascript
运行
复制
mListView.setLongClickable(true);
selected = new ArrayList<>();
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        // int selected_id = s.get_id();
        int selected_id = position;
        if (selected.contains(selected_id)) {
            selected.remove(selected.indexOf(selected_id));
            Log.i(TAG, " -- remove -- " + parent.getChildAt(selected_id));
            parent.getChildAt(selected_id).setBackgroundColor(ContextCompat.getColor(context, R.color.default_color));
            //view.setBackgroundColor(ContextCompat.getColor(context, R.color.default_color));
            //view.setSelected(false);
            Toast.makeText(context, "MainAct REMOVED: " + selected_id + " - " , Toast.LENGTH_SHORT).show();
        } else {
            selected.add(selected_id);
            parent.getChildAt(selected_id).setBackgroundColor(ContextCompat.getColor(context, R.color.selected_color));
            Log.i(TAG, " -- add -- " + parent.getChildAt(selected_id));
            //view.setBackgroundColor(ContextCompat.getColor(context, R.color.selected_color));
            //view.setSelected(true);
            Toast.makeText(context, "MainAct ADDED: " + selected_id + " - " , Toast.LENGTH_SHORT).show();
        }
        return true;
    }
});

还是不去.:-

EN

回答 1

Stack Overflow用户

发布于 2020-01-02 18:04:39

试着实施以下措施:

代码语言:javascript
运行
复制
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ....

    final Person s = (Person) this.getItem(position);

    if (selected.contains(s.get_id())) {
        cbId.setChecked(true);
    } else {
        cbId.setChecked(false);
    }

    ....

    cbId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // update your model (or other business logic) based on isChecked

            int selected_id = s.get_id();
            if (selected.contains(selected_id)) {
                selected.remove(selected_id);
                Toast.makeText(c, "REMOVED: " + selected_id + " - " + isChecked, Toast.LENGTH_SHORT).show();
            } else {
                selected.add(selected_id);
                Toast.makeText(c, "ADDED: " + selected_id + " - " + isChecked, Toast.LENGTH_SHORT).show();
            }
        }
    });

    ....

    convertView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(c, " -- onLongClick -- " + s.get_id() + " - " + s.getName(), Toast.LENGTH_SHORT).show();

            int selected_id = s.get_id();
            if (selected.contains(selected_id)) {
                selected.remove(selected_id);
            } else {
                selected.add(selected_id);
            }

            notifyDataSetChanged();

            return false;
        }
    });

    ....
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59438598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档