Android 使用SQLite中的数据填充MultiChoice微调器
我正在开发一个将数据保存到SQLite数据库的应用程序。我有一个属性,它位于多选微调器中 我想将其保存到数据库中,但我不知道如何检索它,因此当我单击它时,它将显示在微调器中选定的项目 有什么想法吗?谢谢 顺便说一句,我从这里获得了MultiChoice微调器库: 编辑: 下面是我操作MultiSelectionSpinner类的代码:Android 使用SQLite中的数据填充MultiChoice微调器,android,spinner,Android,Spinner,我正在开发一个将数据保存到SQLite数据库的应用程序。我有一个属性,它位于多选微调器中 我想将其保存到数据库中,但我不知道如何检索它,因此当我单击它时,它将显示在微调器中选定的项目 有什么想法吗?谢谢 顺便说一句,我从这里获得了MultiChoice微调器库: 编辑: 下面是我操作MultiSelectionSpinner类的代码: package com.scbpfsdgis.femobilebetav20; import android.os.Bundle; import andro
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());
}
}
package com.scbpfsdgis.femobilebetav20;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.Toast;
导入com.guna.libmultispinner.MultiSelectionSpinner;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
/**
*威廉于2018年1月27日创作。
*/
公共类FieldDetailActivity扩展AppCompativeActivity实现MultiSelectionSpinner.OnMultipleItemsSelectedListener{
spnMechMeth spnTract旋转器;
字符串限制=空;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u field\u detail);
String[]limits=getResources().getStringArray(R.array.limits);
List limitList=newarraylist(Arrays.asList(limits));
MultiSelectionSpinner MSSLimites=(MultiSelectionSpinner)findViewById(R.id.spnmainim);
MSS限制。设置项(限制列表);
msslimites.setListener(此);
spnMechMeth=findviewbyd(R.id.spnMechMeth);
spnTract=findviewbyd(R.id.spnTract);
System.out.println(“选定字符串:+mssCanals.getSelectedItemsAsString());
}
@凌驾
公共作废已选择的骰子(列表索引){
}
@凌驾
public void selectedStrings(列表字符串){
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();
}
}
package com.guna.libmultispinner;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.DialogInterface.OnMultiChoiceClickListener;
导入android.util.AttributeSet;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.SpinnerAdapter;
导入java.util.array;
导入java.util.LinkedList;
导入java.util.List;
公共类MultiSelectionSpinner扩展微调器实现
OnMultiChoiceClickListener{
MultipleItemsSelectedListener上的公共接口{
作废选定的骰子(列表索引);
作废所选字符串(列表字符串);
}
private OnMultipleItemsSelectedListener侦听器;
字符串[]_items=null;
布尔[]mSelection=null;
布尔值[]mSelectionAtStart=null;
字符串_itemsAtStart=null;
阵列适配器简单_适配器;
字符串标题=null;
公共MultiSelectionSpinner(上下文){
超级(上下文);
简单_适配器=新的阵列适配器(上下文,
android.R.layout.simple\u微调器(项目);
super.setAdapter(简单_适配器);
}
公共MultiSelectionSpinner(上下文、属性集属性){
超级(上下文,attrs);
简单_适配器=新的阵列适配器(上下文,
android.R.layout.simple\u微调器(项目);
super.setAdapter(简单_适配器);
}
public void setListener(OnMultipleItemsSelectedListener侦听器){
this.listener=listener;
}
public void onClick(DialogInterface dialog,int,其中布尔值被选中){
if(mSelection!=null&&which