跟踪复选框列表中所选项目的顺序
Track order of selected items in checkbox list
我一直在搜索 google,但没有找到答案。我可以跟踪在我的复选框列表视图中选中的项目的顺序吗?我也想一次只允许检查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);
}
}
我想要发生的是当我选择的项目字符串经过下一个 activity 时,我已经将其设置为将项目拆分为单独的字符串,然后设置为单独的文本视图。只有 5 个文本视图,这意味着我不能选择多于或少于 5 个项目。我不知道该怎么做。
此外,在将第一个项目传递给下一个 activity 时,我需要将其放在列表的顶部,因为我需要在我的第一个文本视图中使用它。其他4个名字的顺序无关紧要。
为此,您需要像这样限制数组列表中的数据,使用它会从列表中删除过多的数据
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);
}
}
我一直在搜索 google,但没有找到答案。我可以跟踪在我的复选框列表视图中选中的项目的顺序吗?我也想一次只允许检查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);
}
}
我想要发生的是当我选择的项目字符串经过下一个 activity 时,我已经将其设置为将项目拆分为单独的字符串,然后设置为单独的文本视图。只有 5 个文本视图,这意味着我不能选择多于或少于 5 个项目。我不知道该怎么做。
此外,在将第一个项目传递给下一个 activity 时,我需要将其放在列表的顶部,因为我需要在我的第一个文本视图中使用它。其他4个名字的顺序无关紧要。
为此,您需要像这样限制数组列表中的数据,使用它会从列表中删除过多的数据
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);
}
}