我有一个连接到串行设备的应用程序。当我启动我的应用程序时,我希望隐藏设置,所以我将线性布局设置为不可见并消失了。但是,这意味着未建立连接。存在一些问题,如果视图设置为消失,则无法读取我在微调器中设置的默认值,这是为什么以及如何修复它。
如果设置被隐藏并且我连接,一旦我打开设置,连接就可以正常工作。
因此,如果两种布局都显示如下:http: //i.imgur.com/N5cU0Wg.png
如果设置布局不可见,它可以正常工作:http: //i.imgur.com/hIttP24.png
但如果它消失了,直到我拿出设置它才起作用:http: //i.imgur.com/ViJUgf3.png
这是我在 onCreate 中使用默认 9600 制作的微调器示例
mBaudSpinner = (Spinner) findViewById(R.id.bannerBaudSpinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mBaudSpinner.setAdapter(adapter);
String[] tempArray = SlickUSB2Serial.BAUD_RATES;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mBaudSpinner.setSelection(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal());
编辑:
这是找到连接时发生的一些代码,但在视图消失时没有被调用,我在这里实现了库方法:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//bannerView.setVisibility(View.INVISIBLE);
//bannerView.setVisibility(View.GONE);
// TODO Auto-generated method stub
Log.d(TAG3, "in onItemSelected");
changeSelectedAdapter(mDeviceAdapters.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void changeSelectedAdapter(USB2SerialAdapter adapter) {
Toast.makeText(this, "in changeselectedadapter", Toast.LENGTH_SHORT)
.show();
// if(mSelectedAdapter!=null){
// mDeviceOutputs.set(mDeviceSpinnerAdapter.getPosition(mSelectedAdapter.getDeviceId()+""),mReceiveBox.getText().toString());
Log.d(TAG3, "in change selected adapter");
mSelectedAdapter = adapter;
mBaudSpinner.setSelection(adapter.getBaudRate().ordinal());
mDataSpinner.setSelection(adapter.getDataBit().ordinal());
mParitySpinner.setSelection(adapter.getParityOption().ordinal());
mStopSpinner.setSelection(adapter.getStopBit().ordinal());
updateCurrentSettingsText();
// mReceiveBox.setText(mDeviceOutputs.get(mDeviceSpinner.getSelectedItemPosition()));
Toast.makeText(this,
"Adapter switched toooo: " + adapter.getDeviceId() + "!",
Toast.LENGTH_SHORT).show();
String getInitialPrompt = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\n";
try {
sendOverSerial(getInitialPrompt.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d(TAG, "exception");
}
}
@Override
public void onAdapterConnected(USB2SerialAdapter adapter) {
adapter.setDataListener(this);
mDeviceAdapters.add(adapter);
mDeviceOutputs.add("");
mDeviceSpinnerAdapter.add("" + adapter.getDeviceId());
mDeviceSpinner.setSelection(mDeviceSpinnerAdapter.getCount() - 1);
Toast.makeText(this,
"Adapter: " + adapter.getDeviceId() + " Connected!",
Toast.LENGTH_SHORT).show();
}
这确实被称为:
public void onAdapterConnected(USB2SerialAdapter adapter) {
adapter.setDataListener(this);
mDeviceAdapters.add(adapter);
mDeviceOutputs.add("");
mDeviceSpinnerAdapter.add("" + adapter.getDeviceId());
mDeviceSpinner.setSelection(mDeviceSpinnerAdapter.getCount() - 1);
Toast.makeText(this,
"Adapter: " + adapter.getDeviceId() + " Connected!",
Toast.LENGTH_SHORT).show();
}
我需要调用它,但它不是:
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//bannerView.setVisibility(View.INVISIBLE);
//bannerView.setVisibility(View.GONE);
// TODO Auto-generated method stub
Log.d(TAG3, "in onItemSelected");
changeSelectedAdapter(mDeviceAdapters.get(position));
}
我对 onItemListener 的唯一引用是在 onCreate 中,所以它不是因为微调器“消失”而没有被处理吗?
mDeviceSpinner = (Spinner) findViewById(R.id.bannerDeviceSpinner);
mDeviceSpinnerAdapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
mDeviceSpinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mDeviceSpinner.setAdapter(mDeviceSpinnerAdapter);
mDeviceSpinner.setOnItemSelectedListener(this);
这是 mSelectedadapter 的文档,i.imgur.com/Ezigt7G.png
它在我的应用程序中是这样定义的:
private static USB2SerialAdapter mSelectedAdapter;