2011-04-04 69 views
0

我试图从一个活动传递一些数据到一个新的,但每次我推一个列出的项目它显示传递的意图,但不传递信息。弹出的吐司显示正在尝试在新活动之前显示的正确数据。在活动不工作之间传递数据

任何人都可以请帮忙。

TimsList.java

public class TimsList extends ListActivity { 
int ct_id; 
String[] ct_number = null; 
String[] ct_address = null; 
String[] ct_phone = null; 
String[] ct_fax = null; 
String[] ct_email = null; 
String[] ct_city = null; 
String[] ct_province = null; 
String[] ct_country = null; 
String[] ct_pcode = null; 
String[] ct_long = null; 
String[] ct_lat = null; 

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

    String result = null; 
    InputStream is = null; 
    StringBuilder sb=null; 
    //http post 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.bob.com/app_main_list.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()); 
    } 
    //convert response to string 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     sb = new StringBuilder(); 
     sb.append(reader.readLine() + "\n"); 
     String line="0"; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result"+e.toString()); 
    } 
    //paring data 

    JSONArray jArray; 
    try{ 
     jArray = new JSONArray(result); 
     JSONObject json_data=null; 
     ct_number=new String[jArray.length()]; 
     ct_address=new String[jArray.length()]; 
     ct_phone=new String[jArray.length()]; 
     ct_fax=new String[jArray.length()]; 
     ct_email=new String[jArray.length()]; 
     ct_city=new String[jArray.length()]; 
     ct_province=new String[jArray.length()]; 
     ct_country=new String[jArray.length()]; 
     ct_pcode=new String[jArray.length()]; 
     ct_long=new String[jArray.length()]; 
     ct_lat=new String[jArray.length()]; 
     for(int i=0;i<jArray.length();i++){ 
      json_data = jArray.getJSONObject(i); 
      ct_id=json_data.getInt("location_id"); 
      ct_number[i]=json_data.getString("store_number"); 
      ct_address[i]=json_data.getString("store_address"); 
      ct_phone[i]=json_data.getString("store_phone"); 
      ct_fax[i]=json_data.getString("store_fax"); 
      ct_email[i]=json_data.getString("store_email"); 
      ct_city[i]=json_data.getString("store_city"); 
      ct_province[i]=json_data.getString("store_province"); 
      ct_country[i]=json_data.getString("store_country"); 
      ct_pcode[i]=json_data.getString("store_pcode"); 
      ct_long[i]=json_data.getString("store_long"); 
      ct_lat[i]=json_data.getString("store_lat"); 
     } 
    } 
    catch(JSONException e1){ 
     Toast.makeText(getBaseContext(), "No Addresses Found" ,Toast.LENGTH_LONG).show(); 
    } catch (ParseException e1) { 
     e1.printStackTrace(); 
    } 
    setListAdapter(new 
      ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ct_address)); 
    ListView lv; 
    lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> timslist, View view, 
       int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ct_phone[position]+" was Clicked", Toast.LENGTH_SHORT).show(); 

      Intent i = new Intent(getApplicationContext(), TimsListMore.class); 
      //i.putExtra("ct_id_pass", "ct_id"); 
      //i.putExtra("ct_number_pass", "ct_number"); 
      //i.putExtra("ct_address_pass", "ct_address"); 
      i.putExtra("ct_phone_pass", "ct_phone"); 
      //i.putExtra("ct_fax_pass", "ct_fax"); 
      //i.putExtra("ct_email_pass", "ct_email"); 
      //i.putExtra("ct_city_pass", "ct_city"); 
      //i.putExtra("ct_province_pass", "ct_province"); 
      //i.putExtra("ct_country_pass", "ct_country"); 
      //i.putExtra("ct_pcode_pass", "ct_pcode"); 
      //i.putExtra("ct_long_pass", "ct_long"); 
      //i.putExtra("ct_lat_pass", "ct_lat"); 
      startActivity(i); 
     } 
    }); 
    } 
} 

TimsListMore.java

public class TimsListMore extends Activity { 

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

     Intent i = getIntent(); 
     Bundle b = i.getExtras(); 
     String string1 = b.getString("ct_phone_pass"); 
     //String string2 = b.getString("ct_id_pass"); 

     TextView tv1 = (TextView) findViewById(R.id.textview); 
     tv1.setText("" + string1 + ""); 
} 

}

+0

您在extras中添加字符串:i.putExtra(“ct_phone_pass”,“ct_phone”);它不显示“ct_phone”?检查TimsListMore类中的Bundle对象是否为null,以及在Toast中显示结果。 – 2011-04-04 07:09:32

+0

TimsListMore.java确实显示ct_phone,但我需要的值不是文本。 – PsychoDogg 2011-04-05 05:36:57

+0

试试这个i.putExtra(“ct_phone_pass”,ct_phone);而不是i.putExtra(“ct_phone_pass”,“ct_phone”); ... 而在TimsListMore Activity中, - > String [] my_ct_phone = b.getStringArray(“ct_phone_pass”); – 2011-04-05 05:47:41

回答

0

通过改变

i.putExtra("ct_phone_pass", "ct_phone"); 

固定
i.putExtra("ct_phone_pass", ct_phone[position]);