Get Resource with ID from String

896 views Asked by At

My program is supposed to have a listview, which when clicked, displays information for the clicked item. My problem is that I have attached the clicked item's name as an extra of the Intent that starts the new Activity. The information I need to display is stored as a string array. I need to use the string that I receive from the Intent to find the string array and bring it into Java. Can someone help me with this? Thanks.

MainActivity.java

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListView;


public class MainActivity extends ActionBarActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listview = (ListView) findViewById(R.id.stationlist);
    listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> l, View v, int position, long id) {
            String[] stations = getResources().getStringArray(R.array.stations);
            String station_name = stations[position] + "_timings";
            Intent intent = new Intent(MainActivity.this, Station_timings.class);
            intent.putExtra("stationwithtime", station_name);
            intent.putExtra("position", position);
            startActivity(intent);
    };
    });
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
 }
 }

Station_timings.class

 import android.content.res.Resources;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.TextView;

public class Station_timings extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_station_timings);
    String station_name = getIntent().getStringExtra("stationwithtime");
    int position = getIntent().getIntExtra("position", 0);
    TextView tv = (TextView) findViewById(R.id.textview);
    Resources res = getResources();
    String[] array = getResources().getStringArray(R.array.stations);
    tv.setText(station_name);
    int arrayid = res.getIdentifier(station_name, "array" , this.getPackageName());
    String[] array2 = res.getStringArray(arrayid);
    ListView listView = new ListView(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,     R.layout.activity_station_timings, array);
    listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.station_timings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 }

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>

<string-array name="stations" style="@style/names_of_stations">
    <item name="Oceanside">Oceanside Station</item>
    <item name="San Clemente">San Clemente Station</item>
    <item name="San Juan Capistrano">San Juan Capistrano Station</item>
    <item name="Irvine">Irvine Station</item>
    <item name="Tustin">Tustin Station</item>
    <item name="Santa Ana">Santa Ana Station</item>
    <item name="Orange">Orange Station</item>
    <item name="Anaheim">Anaheim Station</item>
    <item name="Fullerton">Fullerton Station</item>
    <item name="Buena Park">Buena Park Station</item>
    <item name="Santa Fe Springs/Norwalk">Santa Fe Springs/Norwalk Station</item>
    <item name="Commerce">Commerce Station</item>
    <item name="Los Angeles">Los Angeles Union Station</item>
</string-array>
<string-array name ="oceanside">
    <item name="1"> 5:45 </item>
    <item name="2"> 6:30 </item>
</string-array>
<string-array name ="station_timings">
    <item name="1"> wrong </item>
    <item name="2"> wrong </item>
</string-array>
<string name="title_activity_station_timings">Station_timings</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

 </resources>
1

There are 1 answers

1
David Wasser On BEST ANSWER

In your example, the variable station_name will contain the String "Oceanside Station". You are then calling

res.getIdentifier(station_name, "array" , this.getPackageName());

which will try to get a resource identifier for a string-array with the name "Oceanside Station". You've provided a string-array with the name "oceanside". They don't match.

Another thing you may wish to consider is using a more structured format to store your data, such as a JSON document, an XML document, a CSV file, or similar. You can then read that into your program as actual objects making them much easier to deal with.