跟踪复选框列表

时间:2016-06-22 07:06:00

标签: android checkboxlist android-checkbox

我一直在搜索谷歌,但没有找到答案。我可以在复选框列表视图中跟踪选中项目的顺序吗?我也想一次只允许检查5个项目。

public class ToonList扩展了ListFragment {

private ArrayAdapter<String> adapter;
int numberOfCheckboxesChecked = 0;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);

    ArrayList<String> toon_list = new ArrayList<String>();
    toon_list.add("name1");toon_list.add("name2");toon_list.add("name3");//...



     adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_multiple_choice, toon_list);
    setListAdapter(adapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    getListView().setLongClickable(true);




getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    String toon = (String) parent.getItemAtPosition(position);
    Intent myIntent = new Intent(getActivity(), ToonDetails.class);
    myIntent.putExtra("toon",toon);
    getActivity().startActivity(myIntent);
    return true;
}
});

}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.share).setVisible(true);
    menu.findItem(R.id.log_in).setVisible(true);
    super.onPrepareOptionsMenu(menu);

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.share) {



        SparseBooleanArray checked = getListView().getCheckedItemPositions();
        ArrayList<String> selectedItems = new ArrayList<String>();
        for (int i = 0; i < checked.size(); i++) {

            // Item position in adapter
            int position = checked.keyAt(i);
            // Add sport if it is checked i.e.) == TRUE!
            if (checked.valueAt(i))
                selectedItems.add(adapter.getItem(position));
        }

        String[] outputStrArr = new String[selectedItems.size()];

        for (int i = 0; i < selectedItems.size(); i++) {
            outputStrArr[i] = selectedItems.get(i);
        }

        Intent intent = new Intent(getActivity(),
                                   ResultActivity.class);

        // Create a bundle object
        Bundle b = new Bundle();
        b.putStringArray("selectedItems", outputStrArr);

        // Add the bundle to the intent.
        intent.putExtras(b);

        // start the ResultActivity
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我想要发生的是当我的所选项目串到达下一个活动时,我已将其设置为将项目拆分为单个字符串,然后设置为单独的文本视图。只有5个文本视图,这意味着我不能选择多于或少于5个项目。我无法想象如何做到这一点。

此外,当我将其传递给下一个活动时,我需要点击第一个项目位于我的列表顶部,因为我需要在第一个textview中使用它。其他4个名字的顺序无关紧要。

1 个答案:

答案 0 :(得分:0)

为此您需要将数据限制在数组列表中,这样使用它会从列表中删除过多的数据

public class LimitedSizeList<K> extends ArrayList<K> {

    private int maxSize;

    public LimitedSizeList(int size){
        this.maxSize = size;
    }

    public boolean add(K k){
        boolean r = super.add(k);
        if (size() > maxSize){
            removeRange(0, size() - maxSize - 1);
        }
        return r;
    }

    public K getYongest() {
        return get(size() - 1);
    }

    public K getOldest() {
        return get(0);
    }
}