我正在开发一个将数据保存到SQLite数据库的应用程序。我有一个多选择微调器的属性。
Selected items displayed in codes, separated by comma
我想将此保存到数据库中,但我不知道如何检索它,所以当我点击它时,它会显示在微调器中选择的项目。
有什么想法吗?感谢。
顺便说一句,我从这里获得了MultiChoice Spinner库:http://v4all123.blogspot.com/2013/09/spinner-with-multiple-selection-in.html
编辑: 下面是我操作MultiSelectionSpinner类的代码:
package com.scbpfsdgis.femobilebetav20;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.guna.libmultispinner.MultiSelectionSpinner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by William on 1/27/2018.
*/
public class FieldDetailActivity extends AppCompatActivity implements MultiSelectionSpinner.OnMultipleItemsSelectedListener {
Spinner spnMechMeth, spnTract;
String limits = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_field_detail);
String[] limits = getResources().getStringArray(R.array.limitations);
List<String> limitList = new ArrayList<String>(Arrays.asList(limits));
MultiSelectionSpinner mssLimits = (MultiSelectionSpinner) findViewById(R.id.spnMainLim);
mssLimits.setItems(limitList);
mssLimits.setListener(this);
spnMechMeth = findViewById(R.id.spnMechMeth);
spnTract = findViewById(R.id.spnTract);
System.out.println("Selected Strings: " + mssCanals.getSelectedItemsAsString());
}
@Override
public void selectedIndices(List<Integer> indices) {
}
@Override
public void selectedStrings(List<String> strings) {
Toast.makeText(this, strings.toString(), Toast.LENGTH_LONG).show();
System.out.println("Toast " + strings.toString());
}
}
MultiSelectionSpinner.java:
package com.guna.libmultispinner;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class MultiSelectionSpinner extends Spinner implements
OnMultiChoiceClickListener {
public interface OnMultipleItemsSelectedListener{
void selectedIndices(List<Integer> indices);
void selectedStrings(List<String> strings);
}
private OnMultipleItemsSelectedListener listener;
String[] _items = null;
boolean[] mSelection = null;
boolean[] mSelectionAtStart = null;
String _itemsAtStart = null;
ArrayAdapter<String> simple_adapter;
String title = null;
public MultiSelectionSpinner(Context context) {
super(context);
simple_adapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public void setListener(OnMultipleItemsSelectedListener listener){
this.listener = listener;
}
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (mSelection != null && which < mSelection.length) {
mSelection[which] = isChecked;
simple_adapter.clear();
if (buildSelectedItemString().length() == 0) {
simple_adapter.add("- Limitations -");
} else {
simple_adapter.add(buildSelectedItemString());
System.out.println("Selected String: " + buildSelectedItemString().length());
}
} else {
throw new IllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}
@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Choose ...");
builder.setMultiChoiceItems(_items, mSelection, this);
_itemsAtStart = getSelectedItemsAsString();
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length);
listener.selectedIndices(getSelectedIndices());
listener.selectedStrings(getSelectedStrings());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
simple_adapter.clear();
if (_itemsAtStart.toString().trim().length() == 0) {
simple_adapter.add("- Limitations -");
} else {
simple_adapter.add(_itemsAtStart);
}
System.arraycopy(mSelectionAtStart, 0, mSelection, 0, mSelectionAtStart.length);
}
});
builder.show();
return true;
}
public String getBuilderTitle(String att) {
if (att.equalsIgnoreCase("L")) {
return "Choose Limitations...";
} else {
return "Canals...";
}
}
@Override
public void setAdapter(SpinnerAdapter adapter) {
throw new RuntimeException(
"setAdapter is not supported by MultiSelectSpinner.");
}
public void setItems(String[] items) {
_items = items;
mSelection = new boolean[_items.length];
mSelectionAtStart = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
mSelection[0] = true;
mSelectionAtStart[0] = true;
}
public void setItems(List<String> items) {
_items = items.toArray(new String[items.size()]);
mSelection = new boolean[_items.length];
mSelectionAtStart = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
mSelection[0] = false;
}
public void setSelection(String[] selection) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
mSelectionAtStart[i] = false;
}
for (String cell : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(cell)) {
mSelection[j] = true;
mSelectionAtStart[j] = true;
}
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public void setSelection(List<String> selection) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
mSelectionAtStart[i] = false;
}
for (String sel : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(sel)) {
mSelection[j] = true;
mSelectionAtStart[j] = true;
}
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public void setSelection(int index) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
mSelectionAtStart[i] = false;
}
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
mSelectionAtStart[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public void setSelection(int[] selectedIndices) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
mSelectionAtStart[i] = false;
}
for (int index : selectedIndices) {
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
mSelectionAtStart[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public List<String> getSelectedStrings() {
List<String> selection = new LinkedList<>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
//Edit: Get only limit code.
selection.add(_items[i].substring(_items[i].indexOf("(")+1, _items[i].indexOf(")")));
}
}
return selection;
}
public List<Integer> getSelectedIndices() {
List<Integer> selection = new LinkedList<>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(i);
}
}
return selection;
}
private String buildSelectedItemString() {
StringBuilder sb = new StringBuilder();
boolean foundOne = false;
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
//Edit: Get only limit code
sb.append(_items[i].substring(_items[i].indexOf("(")+1, _items[i].indexOf(")")));
}
}
return sb.toString();
}
public String getSelectedItemsAsString() {
StringBuilder sb = new StringBuilder();
boolean foundOne = false;
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
//Edit: Get only limit code
sb.append(_items[i].substring(_items[i].indexOf("(")+1, _items[i].indexOf(")")));
}
}
return sb.toString();
}
}
答案 0 :(得分:-1)
使用LoaderManager回调。 从这里获得一些帮助: