非公开List无法包外访问,CheckListAdapter选中项获取求助
1. Fixing "List is not public; cannot be accessed from outside package" Error
Your arrEmps ArrayList uses a default package-private access modifier, which blocks access from classes outside its package. Instead of making it public (which breaks encapsulation), here are cleaner, safer approaches:
Option 1: Add a Public Getter Method
Add a method in your CheckListAdapter to safely expose the list (or just the data you need):
public ArrayList<CheckList_Items> getCheckListItems() { return arrEmps; }
Then access it from outside the package like this:
CheckListAdapter adapter = (CheckListAdapter) listView.getAdapter(); ArrayList<CheckList_Items> items = adapter.getCheckListItems();
Option 2: Add a Method to Get Selected Items Directly
If you only need selected items, avoid exposing the entire list by adding a method that filters and returns just the selected entries (we’ll cover how to track selection next).
2. How to Get Selected Items from ListView
ListView doesn’t automatically track selected items unless you configure its choice mode. Here’s how to handle both single and multiple selection scenarios:
Step 1: Configure ListView Choice Mode
In your Activity/Fragment where you set up the ListView, add:
// For single selection (only one item can be selected at a time) listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // For multiple selection (select any number of items) listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Step 2: Track Selection with a Custom Model (More Control)
For custom selection UI or finer control, add an isSelected field to your CheckList_Items model:
public class CheckList_Items { // Your existing fields (e.g., name, description) private boolean isSelected; // Getter and setter for selection state public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } }
Update your Adapter’s getView method to handle clicks and update the selection state:
@Override public View getView(int position, View convertView, ViewGroup parent) { // Your existing view setup logic (ViewHolder inflation, etc.) // Toggle selection on item click holder.itemView.setOnClickListener(v -> { CheckList_Items currentItem = arrEmps.get(position); currentItem.setSelected(!currentItem.isSelected()); notifyDataSetChanged(); // Refresh the list to update selection UI }); // Update UI to reflect selection state (e.g., highlight selected items) holder.itemView.setSelected(arrEmps.get(position).isSelected()); return convertView; }
Step 3: Retrieve Selected Items
Add a method in your Adapter to fetch all selected items:
public ArrayList<CheckList_Items> getSelectedItems() { ArrayList<CheckList_Items> selectedItems = new ArrayList<>(); for (CheckList_Items item : arrEmps) { if (item.isSelected()) { selectedItems.add(item); } } return selectedItems; }
Then call this method from your Activity/Fragment whenever you need the selected items:
ArrayList<CheckList_Items> selected = adapter.getSelectedItems();
内容的提问来源于stack exchange,提问作者kara

