使用 Android (Eclipse) 我正在尝试读取 .csv 文件并使用 Scanner 动态填充 Spinner。尝试了许多方法,结果相同。模拟器显示 Spinner,但仅填充最后一个逗号和价格。还需要将所有三个变量存储在数组中,并在选择 Spinner 时检索它们以填充 EditText 字段。任何帮助将不胜感激...
数据文件记录:
4,铝罐,.55 5,带猫转换器的车辆,9.00 18,黄铜(讽刺红/黄),.20 1001,扣 客户#查找,-2.00
爪哇:
public class BRprogramActivity extends Activity {
private static final String TAG = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
Button addButton;
Button editButton;
Button sendButton;
//
Spinner array_spinner;
//
// activate soft Keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//
// .csv comma separated values file
//
try {
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.brdata));
//
while (scanner.hasNext()) {
String data = (scanner.next());
String [] values = data.split(",");
item = values[0];
description = values[1];
price = values[2];
//
array_spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item,values);
array_spinner.setAdapter(adapter);
}
scanner.close();
} catch (Exception e) {
Log.e(TAG, "Exception: "+Log.getStackTraceString(e));
}
//
addButton = (Button) findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.v("test", "ADD button clicked");
}
});
//
editButton = (Button) findViewById(R.id.editbutton);
editButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.v("test", "EDIT button clicked");
}
});
//
sendButton = (Button) findViewById(R.id.sendbutton);
sendButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.v("test", "SEND button clicked");
}
});
}
}