I have the following in the project:
MainActivity
FirebaseHelper
CustomAdapter
Person
one_line_list_item.xml
MainActivity Class
@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 Class
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 Class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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 ) {
cbId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(c, "" + s.get_id(), Toast.LENGTH_SHORT).show();
}
});
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) && !isChecked) {
selected.remove(selected.indexOf(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();
}
}
});
}
//ONITECLICK
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) {
Toast.makeText(c, " -- onLongClick -- " + s.get_id() + " - " + s.getName(), Toast.LENGTH_SHORT).show();
return false;
}
});
return convertView;
}
Person Class
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>
Sorry for this long code, I put only the necessary parts of each Class
to make it as clear as possible...
The issue is:
When clicking any checkbox
on the list, it seems to be checking also others that are hidden from the view.
I tried to isolate the issue as possible but can't figure this out...
Any ideas why it would be checking also other items that are hidden from view??
They are checked in intervals of 8 and also unchecked when any of the is unchecked....
Aucun commentaire:
Enregistrer un commentaire