我的程序应该有一个列表视图,点击后会显示所点击项目的信息。我的问题是我已将所点击的项目的名称附加为启动新活动的Intent的额外名称。我需要显示的信息存储为字符串数组。我需要使用从Intent收到的字符串来查找字符串数组并将其带入Java。有人可以帮我弄这个吗? 感谢。
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>