我经历了关于这个主题的大量线程,但我无法找到解决问题的方法。
我正在尝试编写一个应用程序,该应用程序将显示存储在SQLlite DB中的GPS坐标列表一旦用户单击其中一个条目,就应该将它们带到另一个显示地图的活动中。
我的列表活动如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.coordinates_activity);
mListView = (ListView) findViewById(R.id.listview);
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.item_layout,
null,
new String[] {LocationsDB.FIELD_ROW_ID, LocationsDB.FIELD_LAT, LocationsDB.FIELD_LNG},
new int[] {R.id.row_id, R.id.row_lat, R.id.row_lgt }, 0);
mListView.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(0, null, this);
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
Intent intentlist = new Intent(getApplicationContext(),fromlist.class);
startActivity(intentlist);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Uri uri = LocationsContentProvider.CONTENT_URI;
return new CursorLoader(this, uri, null, null, null, null);
}
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
mAdapter.swapCursor(arg1);
}
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);
}
}
我已将onItemClick更新为:
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
TextView tv1=(TextView)v.findViewById(R.id.row_lat);
String lon_map=tv1.getText().toString();
Double lon1 = Double.parseDouble(lon_map);
TextView tv2=(TextView)v.findViewById(R.id.row_lgt);
String lgt_map=tv2.getText().toString();
Double lgt2 = Double.parseDouble(lgt_map);
Intent intentlist = new Intent(getApplicationContext(),fromlist.class);
intentlist.putExtra("lon1", lon1);
intentlist.putExtra("lgt2", lgt2);
startActivity(intentlist);
}
第二项活动如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fromlist_activity);
double latitude = getIntent().getDoubleExtra("lon1", 0);
double longitude = getIntent().getDoubleExtra("lgt2", 0);
我仍然无法获得第二个活动中的值
答案 0 :(得分:0)
将您的数据作为额外的意图
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
Intent intentlist = new Intent(getApplicationContext(),fromlist.class);
Bundle b = new Bundle();
b.putDouble("lat", latitude);//latitude
b.putDouble("long", longitude);//longitude
intentlist.putExtra(b);
startActivity(intentlist);
}
});
并在您的formlist
活动中:
@Override
protected void onCreate(bundle b) {
Bundle b = getIntent().getExtras();
latitude = b.getDouble("lat");
longitude = b.getDouble("long");
...
}
答案 1 :(得分:0)
做这样的事情:
(假设您有一个要传递给第二个活动的字符串)。
在您当前的活动中,创建一个新的意图:
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
然后在新的Activity中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
阅读更多信息here。
答案 2 :(得分:0)
在你的第二个活动中试试这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fromlist_activity);
Bundle extras = getIntent().getExtras();
double latitude = extras.getDoubleExtra("lon1", 0);
double longitude = extras.getDoubleExtra("lgt2", 0);