问题
Using Android (Eclipse) I am trying to read a .csv file and dynamically populate a Spinner using Scanner. Have tried many methods with the same result. Emulator shows Spinner but only the last comma and price are populated. Also need to store all three variables in array and retrive them when Spinner selection is made in order to populate EditText fields. Any assistance would be greatly appreciated...
Data file records:
4,Aluminum Cans,.55 5,Vehicle with cat convertor,9.00 18,Brass ( Irony Red/Yellow ),.20 1001,Deduction Customer # Look-Up,-2.00
Java:
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");
}
});
}
}
回答1:
If you want to display each row of your CSV on one row in the spinner, try this:
List<String> list = new ArrayList<String>();
while (scanner.hasNext()) {
list.add(scanner.next());
}
array_spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
array_spinner.setAdapter(adapter);
Though has poor readability for the ordinary user, perhaps you only want to display the description and price:
List<String> list = new ArrayList<String>();
while (scanner.hasNext()) {
String data = scanner.next();
String [] values = data.split(",");
list.add(values[1] + " $" + values[2]);
}
Your posted method only showed the last CSV line, because you were creating a new ArrayAdapter for each line in the CSV and overriding the previous adapter that you had created.
By storing the Scanner's results in a List and moving the adapter's definition out of the Scanner loop, you should be able to see results like you had hope.
Addition from comments
An OutOfBounds exception with length=2, index=2 suggests that at least one row in your real CSV only has two values (one comma). To help you find that row try this:
if(values.length != 3)
Log.v("Example", "Malformed row: " + data);
else
list.add(values[1] + " $" + values[2]);
Also, I neglected to mention that you are not setting a drop down layout for your Spinner. This will not cause the app to crash, but this creates the standard user interface:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
来源:https://stackoverflow.com/questions/11367790/populate-spinner-from-csv-file-using-scanner