我在使用可搜索微调器时遇到了麻烦。我已按照教程(this one)进行操作,以使可搜索的微调器在我的应用中正常工作。目前,我有一个普通的微调框,其中填充了一个csv文件中的项目,您可以通过按已实现的FAB按钮来复制这些微调框。但是,当我尝试在代码中将“ Spinner”小部件更改为“ SearchableSpinner”时,当我复制一个微调器时,我的应用程序崩溃。我在代码中加了星号,以显示在哪里有我的微调器代码。
Create.java代码:
public class create extends AppCompatActivity {
private LinearLayout mLinearLayout;
private ArrayList<Spinner> mSpinners;
Button buttontest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSpinners = new ArrayList<>();
mLinearLayout = findViewById(R.id.my_linearLayout);
//mLinearLayout.addView(makeSpinner()); // First spinner
FloatingActionButton floatingActionButton = //this code is for the FAB, which adds more slots
(FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Handle the click.
SearchableSpinner spinner = (SearchableSpinner) makeSpinner();
mLinearLayout.addView(spinner); //Add another spinner
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)spinner.getLayoutParams();
layoutParams.setMargins( 5, 130, 10, 0);
layoutParams.height = 80;//pixels
layoutParams.width = 240;//pixels
spinner.setLayoutParams(layoutParams);
//Add a new button
AppCompatButton newButton = makeButton();
mLinearLayout.addView(newButton); // Add another button
//Add a new checkbox
CheckBox newCheckbox = makeCheckbox();
mLinearLayout.addView(newCheckbox);
EditText newEdittext = makeEdittext();
mLinearLayout.addView(newEdittext);
}
});
}
//DUPLICATING ITEMS WHEN + IS PRESSED
private CheckBox makeCheckbox() {
//Create new Checkbox
CheckBox checkbox = new CheckBox(this);
// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
checkbox.setLayoutParams(layoutParams);
return checkbox;
}
private EditText makeEdittext() {
//Create new eddittext
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_NUMBER); //sets it to a numeric edittext instead of textboard
// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
return editText;
}
private AppCompatButton makeButton() {
//Create new Button
AppCompatButton button = new AppCompatButton(this);
// code for deleting button //
buttontest = (Button)findViewById(R.id.buttontest);
buttontest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttontest.setVisibility(View.GONE);
makeCheckbox().setVisibility(View.GONE);
}
}); //end
// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
button.setBackgroundColor(Color.parseColor("#ffffff"));
return button;
}
**private Spinner makeSpinner() {**
//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
List<String> itemList = csvFile.read();
//Create new spinner
**SearchableSpinner spinner = (SearchableSpinner) new Spinner(this, Spinner.MODE_DROPDOWN);
// Setup layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
spinner.setLayoutParams(layoutParams);
MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);
spinner.setAdapter(adapter);
//Add it to your list of spinners so you can retrieve their data when you click the getSpinner button
mSpinners.add(spinner);
return spinner;
}
//csv file code
private class CSVFile {
InputStream inputStream;
public CSVFile(InputStream inputStream) {
this.inputStream = inputStream;
}
public List<String> read() {
List<String> resultList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
resultList.add(row[1]);
}
} catch (IOException e) {
Log.e("Main", e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Main", e.getMessage());
}
}
return resultList;
}
}
}
Logcat:
08-09 17:10:42.500 18803-18803 / com.app.supermarketaislefinder E / Android运行时:致命异常:主要 java.lang.ClassCastException:android.widget.Spinner无法转换为com.toptoche.searchablespinnerlibrary.SearchableSpinner 在com.app.supermarketaislefinder.create.makeSpinner(create.java:171) 在com.app.supermarketaislefinder.create.access $ 000(create.java:37) 在com.app.supermarketaislefinder.create $ 1.onClick(create.java:74) 在android.view.View.performClick(View.java:4162) 在android.view.View $ PerformClick.run(View.java:17082) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4867) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 在dalvik.system.NativeStart.main(本机方法)
答案 0 :(得分:1)
您的mSpinners
ArrayList
属于Spinner
小部件,而不是SearchableSpinner
widget
的列表
使用此
private ArrayList<SearchableSpinner> mSpinners;
代替这个
private ArrayList<Spinner> mSpinners;
编辑
您需要在widget
方法中返回SearchableSpinner
的{{1}}
makeSpinner()
答案 1 :(得分:0)
在引用SearchableSpinner
示例时,您需要具有如下的SearchableSpinner
列表。 SearchableSpinner是自定义的Spinner 。
private ArrayList<SearchableSpinner> mSpinners;
删除下面的代码行,然后正确初始化SearchableSpinner
,请参见SearchableSpinner
// SearchableSpinner spinner = (SearchableSpinner) new Spinner(this, Spinner.MODE_DROPDOWN);
SearchableSpinner spinner = new SearchableSpinner(this);
mSpinners.add(spinner);