无法从数据库中获取数据并有意传递
Unable to take data from DB and pass with intent
我的第一个 activity
中有来自 MySQL 的请求数据
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
String url = "http://myhost/app/get.php?id=" + name_id;
JSONArray data;
try {
resultServer = getJSONUrl(url);
data = new JSONArray(resultServer);
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("id", (String)c.getString("id"));
map.put("name", (String)c.getString("name"));
map.put("description", (String)c.getString("description"));
MyArrList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后像这样
将此信息传递给另一个activity
Intent newActivity = new Intent(act1.this,act2.class);
newActivity.putExtra("Name", Name);
newActivity.putExtra("tableDescription", description);
startActivity(newActivity);
finish();
接下来 activity 我得到这样的信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
String textDescription, name;
TextView txtView;
txtView = (TextView) findViewById(R.id.text);
txtView = (TextView) findViewById(R.id.name);
Intent intent= getIntent();
textDescription = getIntent().getStringExtra("description");
((TextView)findViewById(R.id.text)).setText(textDescription+"");
name = getIntent().getStringExtra("Name");
((TextView)findViewById(R.id.name)).setText(name+"");
}
所以在第二个 activity Name
那里,但 textDescription
显示为空,我确定此列中的数据库中有文本。
你的问题出在
newActivity.putExtra("tableDescription", description);
textDescription = getIntent().getStringExtra("description");
应该是
textDescription = getIntent().getStringExtra("tableDescription");
一个简单的错误!
将其传递给 newActivity 时,intent 中的参数名称与您用来获取描述的名称不同:
路过时:
newActivity.putExtra("**tableDescription**", description);
并且在检索用户时使用 :
getIntent().getStringExtra("**description**")
两行的键名必须相同!
我的第一个 activity
中有来自 MySQL 的请求数据protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
String url = "http://myhost/app/get.php?id=" + name_id;
JSONArray data;
try {
resultServer = getJSONUrl(url);
data = new JSONArray(resultServer);
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("id", (String)c.getString("id"));
map.put("name", (String)c.getString("name"));
map.put("description", (String)c.getString("description"));
MyArrList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后像这样
将此信息传递给另一个activityIntent newActivity = new Intent(act1.this,act2.class);
newActivity.putExtra("Name", Name);
newActivity.putExtra("tableDescription", description);
startActivity(newActivity);
finish();
接下来 activity 我得到这样的信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
String textDescription, name;
TextView txtView;
txtView = (TextView) findViewById(R.id.text);
txtView = (TextView) findViewById(R.id.name);
Intent intent= getIntent();
textDescription = getIntent().getStringExtra("description");
((TextView)findViewById(R.id.text)).setText(textDescription+"");
name = getIntent().getStringExtra("Name");
((TextView)findViewById(R.id.name)).setText(name+"");
}
所以在第二个 activity Name
那里,但 textDescription
显示为空,我确定此列中的数据库中有文本。
你的问题出在
newActivity.putExtra("tableDescription", description);
textDescription = getIntent().getStringExtra("description");
应该是
textDescription = getIntent().getStringExtra("tableDescription");
一个简单的错误!
将其传递给 newActivity 时,intent 中的参数名称与您用来获取描述的名称不同:
路过时:
newActivity.putExtra("**tableDescription**", description);
并且在检索用户时使用 :
getIntent().getStringExtra("**description**")
两行的键名必须相同!