无法从数据库中获取数据并通过意图传递

时间:2015-01-12 08:19:34

标签: android mysql android-intent

我在第一个活动中有来自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();
        }

然后将此信息传递给其他此类活动

Intent newActivity = new Intent(act1.this,act2.class);

       newActivity.putExtra("Name", Name);
       newActivity.putExtra("tableDescription", description);
       startActivity(newActivity);
       finish();

在接下来的活动中,我会收到这样的信息

    @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+"");
} 

所以在第二个活动中Name存在,但textDescription显示为null,我确信此列中的数据库中有文本。

2 个答案:

答案 0 :(得分:2)

你的问题在于

 newActivity.putExtra("tableDescription", description);

 textDescription = getIntent().getStringExtra("description");

应该是

textDescription = getIntent().getStringExtra("tableDescription");

答案 1 :(得分:1)

一个简单的错误!

在将其传递给newActivity时,intent中的参数名称与您用于获取描述的参数名称不同:

通过时:

newActivity.putExtra("**tableDescription**", description);

并在使用以下方式检索用户时使用:

getIntent().getStringExtra("**description**")

两行中的键名应相同!