在这个项目中,我有以下几点:
MainActivityFirebaseHelperCustomAdapterPersonone_line_list_item.xmlMainActivity类
@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类
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类
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类
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
<?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中实现这一功能
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;
}
});还是不去.:-
发布于 2020-01-02 18:04:39
试着实施以下措施:
@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;
}
});
....
}https://stackoverflow.com/questions/59438598
复制相似问题