2012-04-19 12 views
1

データを読み込んだ後、新しいインテントを開いてデータを表示するときに、2つの可能な選択肢を持つメニューを作成します。しかし、私はそれがcourseName値を送信しないことがわかります。問題は、onCreateOptionsMenuメソッドとonOptionsItemSelectedメソッドで、メインのonCreateメソッドからコース値を取得しないためです。私を助けて、それを修正する方法を教えてください。Androidのインテントは値を送信しません

あなたの意思に値を入れている
String courseName = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.single_course_layout); 

    Intent in = getIntent(); 
    courseName = in.getStringExtra("fullname"); 

    TextView label = (TextView)findViewById(R.id.label); 
    label.setText(courseName); 
    String summary = null; 
    TextView summaryContent = (TextView)findViewById(R.id.summary); 


    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("cname",""+courseName)); 
    InputStream is = null; 
    String result = null; 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/Bdarbas/getCourseSummary.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 
    try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
        System.out.println(line); 
      } 
      is.close(); 

      result=sb.toString(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

try{ 
    JSONArray jArray = new JSONArray(result); 
    for(int ii=0;ii<jArray.length();ii++){ 
      JSONObject json_data = jArray.getJSONObject(ii); 
      summary = json_data.getString("summary"); 
    } 
    summaryContent.setText(summary); 
} catch(JSONException e){ 
    Log.e("log_tag", "Error parsing data "+e.toString()); 
} 

}; 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
}; 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.users:  

      Intent in = new Intent(getApplicationContext(), AllCourseUsers.class); 
      in.putExtra(courseName, "courseName"); 
      startActivity(in); 
      break; 

     case R.id.data:  Toast.makeText(this, "You will see data list!", Toast.LENGTH_LONG).show(); 
          break; 
     } 
    return true; 
} 

答えて

2

後方

in.putExtra(courseName, "courseName"); 

は、私はば完全に失敗した

in.putExtra(key, value); 

in.putExtra("courseName", courseName); 
+0

でなければなりません。どうもありがとう。 – LTnewbie

関連する問題