简体   繁体   中英

Convert string array to int array android

I'm trying to convert an string array ( listview_array ) to int array , and then compare numbers. Unfortunately, the app is crashing everytime I execute it.

Here is the code:

public class FindStop3 extends Activity implements OnItemClickListener{
    private String stop,line,listview_array[],buschain,dayweek,weekly;
    private int selected,day;   
    private TextView tvLine,tvToday,tvNext;
    private ListView lv;    
    String currentdate = java.text.DateFormat.getDateInstance().format(Calendar.getInstance().getTime());//Get current time
    String currenttime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());//Get current time
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_stop3);
        FindStop3.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);//Lateral transition

        Intent intent = getIntent();//Take data from last activity
        Bundle bundle = intent.getExtras();
        line=bundle.getString("Line");//Takes the previous selected line from the last activity
        stop=bundle.getString("Stop");//Takes the previous selected stop from the last activity
        setTitle("Selected stop: "+stop);//Modifies title

        getDayOfWeek();//Gets day of week
        getBusChain(line,stop,weekly);//Calls method to get the xml chain name        

        tvLine = (TextView) findViewById(R.id.tvLine);
        tvLine.setText("Line from "+line);

        tvNext = (TextView) findViewById(R.id.tvNext);
        tvNext.setText("Next bus arrives at "+currenttime);


        tvToday = (TextView) findViewById(R.id.tvToday);
        tvToday.setText(dayweek+","+currentdate+" schedule:");          

        selected= getResources().getIdentifier(buschain, "array",
                this.getPackageName());//Lets me use a variable as stringArray

        listview_array = getResources().getStringArray(selected);

        lv = (ListView) findViewById(R.id.lvSchedule);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));


        getNextBus();


    }

And here is the method to convert it:

public void getNextBus () {
        //Converts listview_array into an integer array

         int myar[]=new int[listview_array.length];

                  for(int i=0;i<listview_array.length;i++){

                      myar[i]=Integer.parseInt(listview_array[i]);



            }

If the method is not executed, the application works perfectly. The values are taken from an xml file as follows:

<string-array name="Schedule">
        <item>05:40</item>
        <item>06:00</item>
        <item>06:16</item>
        <item>06:28</item>
        <item>06:40</item>
        <item>07:16</item>
        <item>07:29</item> 
        <item>07:43</item>
        <item>07:55</item>
        <item>08:07</item>
        <item>08:22</item>       
    </string-array>

Could anyone gives an idea about what can be the problem?

Thanks.

I think that your problem is when you try to convert the values, because a value like this "05:40" cannot be converted into a int.

Problably, you're getting a .

If you want a better answer please send the log of your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM