简体   繁体   中英

Get the checked items in a ListView — Android

I have a listview with some check options. I want to transfer all the checked items after clicking a button into a String Array so I can use Intent to use it on other class. How can I do it? I already searched for some answers but none of them worked for me.`

public class MyClasses extends AppCompatActivity implements View.OnClickListener {
public String[] Section1 = {"Bonilla, Abbie", "Hernando, Roland Joseph", "Ko, Kritofer", "Manaig, Kathleen",
        "Olalia, Jerome", "Rosario, Kyle", "Sevilla, Karen", "Tancioco, Eron", "Villena, Mark"};
public String[] Section2 = {"Chavez, Stephanie", "Espana, Bren Alfred", "Faro, Ede", "Gonzales, Venice",
        "Magora, Joshua James", "Roman, Jairah", "Ramirez, Stephanie", "Tiboli, Jamalul", "Torrazo, Nicole"};
public String[] Section3 = {"Arbonida, Caye Anne", "De Guzman, Patricia", "Escandor, Jennifer", "Marzan, Rann",
        "Menorca, Paula", "Payofelin, Marlo", "Pimentel, Iris Coleen", "Queen, Elizabeth", "Unggoy, Monkey"};
public String[] Use = {};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_classes);
    int section = getIntent().getIntExtra("position", 20);
    ShowStudents(section);
    Button submit = (Button) findViewById(R.id.btn_Submit);
    submit.setOnClickListener(this);

}


public void ShowStudents(int pos) {

    if (pos == 0) Use = Section1;
    else if (pos == 1) Use = Section2;
    else if (pos == 2) Use = Section3;
    ListAdapter ClassAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, Use);
    final ListView classlist = (ListView) findViewById(R.id.list_Students);
    classlist.setAdapter(ClassAdapter);

    classlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckedTextView item = (CheckedTextView) view;
            Toast.makeText(MyClasses.this, Use[position] + "IS PRESENT ", Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_Submit:
        //transfer checked items
}

}}

create file CustomAdapter.class and put this code inside :

public class CustomAdapter extends ArrayAdapter {
    public CustomAdapter(Context context, int loyautId, ArrayList<String> list, String[] toCheck) {
            super(context, loyautId, list);
                if(toCheck!=null){
                this.toCheck =new ArrayList<String>(Arrays.asList(toCheck));
                }
            }

        @NonNull
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            String item = getItem(position).toString();
            final CheckedTextView check = (CheckedTextView) convertView.findViewById(android.R.id.text1);

            check.setText(getItem(position).toString());    

            if (toChcec!=null && toCheck.contains(item)) {
                check.setChecked(true);
            }
            check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (check.isChecked()) {
                        checkedList.remove(getItem(position));
                    } else {
                        checkedList.add(getItem(position).toString());
                    }
                }
            });
            return convertView;
        }

        public ArrayList<String> getCheckedList(){
            return checkedList;
        }

    }

how to place it in code:

CustomAdapter classAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_checked, Use,option if you wont check item);

how get checked list:

classAdapter.getCheckedList()

examples in your code:

public class MyClasses extends AppCompatActivity implements View.OnClickListener {
        public String[] Section1 = {"Bonilla, Abbie", "Hernando, Roland Joseph", "Ko, Kritofer", "Manaig, Kathleen",
                "Olalia, Jerome", "Rosario, Kyle", "Sevilla, Karen", "Tancioco, Eron", "Villena, Mark"};
        public String[] Section2 = {"Chavez, Stephanie", "Espana, Bren Alfred", "Faro, Ede", "Gonzales, Venice",
                "Magora, Joshua James", "Roman, Jairah", "Ramirez, Stephanie", "Tiboli, Jamalul", "Torrazo, Nicole"};
        public String[] Section3 = {"Arbonida, Caye Anne", "De Guzman, Patricia", "Escandor, Jennifer", "Marzan, Rann",
                "Menorca, Paula", "Payofelin, Marlo", "Pimentel, Iris Coleen", "Queen, Elizabeth", "Unggoy, Monkey"};
        public String[] Use = {};

    private ArrayList<String> myCheckedList; private **CustomAdapter myAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_classes);
            int section = getIntent().getIntExtra("position", 20);
            ShowStudents(section);
            Button submit = (Button) findViewById(R.id.btn_Submit);
            submit.setOnClickListener(this);
        //YEEEE i HAVE CHECED LIST TO SUBMIT !!!
        myCheckedList = myAdapter.getCheckedList();
        }


        public void ShowStudents(int pos) {

            if (pos == 0) Use = Section1;
            else if (pos == 1) Use = Section2;
            else if (pos == 2) Use = Section3;
            myAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_checked, Use,null);
            final ListView classlist = (ListView) findViewById(R.id.list_Students);
            classlist.setAdapter(ClassAdapter);

            classlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CheckedTextView item = (CheckedTextView) view;
                    Toast.makeText(MyClasses.this, Use[position] + "IS PRESENT ", Toast.LENGTH_SHORT).show();
                 }
            });

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_Submit:
                //transfer checked items
        }

Inside onItemClick method make the array for checked items/string and store this array in sharepref and go to the required screen on which you want and retrieve the array from shared preferences...

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM