获取ListView中的选中项目 - Android

时间:2017-01-31 17:43:36

标签: java android listview

我有一个带有一些检查选项的列表视图。我想在单击按钮后将所有检查的项目转移到字符串数组中,以便我可以使用Intent在其他类上使用它。我该怎么做?我已经搜索了一些答案,但没有一个能为我工作。

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
}

}}

2 个答案:

答案 0 :(得分:0)

创建文件 CustomAdapter.class 并将此代码放入:

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;
        }

    }

如何将其放入代码中

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

如何获取核对清单:

classAdapter.getCheckedList()
您的代码中的

示例:

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
        }

答案 1 :(得分:-1)

在onItemClick方法内部为已检查项目/字符串创建数组,并将此数组存储在sharepref中,然后转到所需的屏幕并从共享首选项中检索数组...

由于