使用扫描仪从CSV文件填充微调器 [英] Populate Spinner from CSV File using Scanner

查看:66
本文介绍了使用扫描仪从CSV文件填充微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Android(Eclipse),我正在尝试动态读取.csv文件 使用扫描仪填充微调框.尝试了许多具有相同结果的方法. Emulator显示Spinner,但仅填充最后一个逗号和价格.还需要 将所有三个变量存储在数组中,并在选择微调器"时检索它们 为了填充EditText字段.任何帮助将不胜感激...

数据文件记录:

4,铝罐,.55 5,带猫转换器的车辆,9.00 18,黄铜(铁红色/黄色),. 20 1001,扣除客户#查找,-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");
            }
        });
    }
}  

解决方案

如果要在微调器的一行上显示CSV的每一行,请尝试以下操作:

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); 

尽管对于普通用户而言可读性较差,但也许您只想显示说明和价格:

List<String> list = new ArrayList<String>();
while (scanner.hasNext()) {
    String data = scanner.next();
    String [] values = data.split(",");
    list.add(values[1] + "  $" + values[2]);
}       

您发布的方法仅显示最后一个CSV行,因为您正在为CSV中的每一行创建一个新的ArrayAdapter并覆盖您先前创建的适配器.

通过将扫描程序"的结果存储在列表"中,并将适配器的定义移出扫描程序"循环,您应该能够看到希望的结果.

添加评论

长度为2,索引为2的OutOfBounds异常表明,实际CSV中至少有一行只有两个值(一个逗号).为了帮助您找到该行,请尝试以下操作:

if(values.length != 3)
    Log.v("Example", "Malformed row: " + data);
else
    list.add(values[1] + "  $" + values[2]);

此外,我忽略了您没有为Spinner设置下拉布局的问题.这不会导致应用程序崩溃,但是会创建标准的用户界面:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

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");
            }
        });
    }
}  

解决方案

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);

这篇关于使用扫描仪从CSV文件填充微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆