Skip to content

Instantly share code, notes, and snippets.

@tatd3v
Forked from dleonett/ListActivity.java
Created May 1, 2020 04:53
Show Gist options
  • Save tatd3v/e7cb0e91f29d9c333201b1b4e93cac2f to your computer and use it in GitHub Desktop.
Save tatd3v/e7cb0e91f29d9c333201b1b4e93cac2f to your computer and use it in GitHub Desktop.
public class ListActivity extends AppCompatActivity {
PatientController pC;
ListView pList;
PatientCursorAdapter pca;
Patient pat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
pList = findViewById(R.id.infoListLV);
pC = new PatientController(getApplicationContext());
Cursor c = pC.allPatients();
pca = new PatientCursorAdapter(this, c, 0);
pList.setAdapter(pca);
pca.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_activity_toolbar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.edit:
List<String> selectedIdsList = pca.getSelectedIdsList();
// recorrer la lista y hacer lo que necesites hacer con esos IDs
return true;
case R.id.delete:
List<String> selectedIdsList = pca.getSelectedIdsList();
// recorrer la lista y hacer lo que necesites hacer con esos IDs
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class TodoCursorAdapter extends CursorAdapter {
List<String> selectedIdsList = new ArrayList<>();
public TodoCursorAdapter(Context context, Cursor cursor) {
// ...
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// ...
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Checkbox checkbox = (Checkbox) view.findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener{
@Override
public onChecked(View view, boolean checked) {
String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
if (checked) {
selectedIdsList.add(id);
} else {
selectedIdsList.remove(id);
}
}
});
}
public List<String> getSelectedIdsList() {
return selectedIdsList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment