开发者

Remember selected ListView Items

开发者 https://www.devze.com 2023-03-10 15:51 出处:网络
I\'m new to android and I have following problem I have a ListView. The ListView is filled with data from an ArrayList. I added a CheckBox to every row.

I'm new to android and I have following problem

I have a ListView. The ListView is filled with data from an ArrayList. I added a CheckBox to every row.

After clicking on a "Resume-Button" I want to write 开发者_如何学运维the selected items to a SQLliteDB (the db works fine - I tested it with static data).

My question is now: How can I get the checked items from the list?

Thx everyone for help! Best regards kyp


You can use this ListView method getCheckedItemPositions() to get all the checked positions. Make sure that the child view in your ListView actually implements the Checkable interface otherwise the checkmark information will not be passed along to the ListView.


You can go through the list, using a for loop and getItemAtPosition(int). For each row you just check if the item is checked, isChecked() for a checkbox. Without seeing the code that is as specific as I can get. If this is not clear enough then post some code about how/what items are stored in the listView

Hope this helps!


But i figured it out myself. I only wanted to select the row by clicking on the checkBox view.

I modified my custom Array Adapter

Here is my code:

public class ContactAdapter extends ArrayAdapter<Contact> implements OnClickListener{

private static HashMap<Integer, Boolean> teamItems = new HashMap<Integer, Boolean>();
int count = 0;

private List<Contact> items;
private int mode;

CheckBox add_to_team;
static int counter = 3;

public ContactAdapter(Context context, int textViewResourceId, List<Contact> objects, int mode) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.mode = mode;
    this.items = objects;
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        //get a reference to the LayoutInflator
        LayoutInflater li = (LayoutInflater)getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //and inflate our XML into the View
        v = li.inflate(R.layout.list_contacts_item, null);
    }

    String race = items.get(position).race;
    String nick = items.get(position).nick;
    String name = items.get(position).name;
    final int pos = position;

    if(v!= null) {
        // Do the work for the other views

        // MODE 1 = SELECTION-MODE  
        if(mode == 1) {

            add_to_team.setVisibility(0);

            try {
                if(count!=0) {
                    boolean b = teamItems.get(pos);
                    if(b==false) {
                        add_to_team.setChecked(false);
                    }
                    else {
                        add_to_team.setChecked(true);
                    }
                }
            } catch (NullPointerException e) {

            }

            add_to_team.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    teamItems.put(pos, arg1);
                    count++;
                }
            });
        } else {
            add_to_team.setVisibility(8);
        }
    }
    return v;
}   

}

I added a HashMap in which I save the selected items. I can call the select state from my activity, by calling the HashMap... And it works great!

0

精彩评论

暂无评论...
验证码 换一张
取 消