这是我的第一个活动。
public class CheckAvailability extends Activity{
Button but1,but2;
EditText brName;
TextView txt1;
String text;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.availability);
brName =(EditText)findViewById(R.id.editText1);
but1 = (Button)findViewById(R.id.button5);
but2 = (Button)findViewById(R.id.button6);
txt1 = (TextView)findViewById(R.id.textView3);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String result = null;
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
//ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
postParameters.add(new BasicNameValuePair("branchname", brName.getText().toString()));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/hello/AvailabilityResponse");
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
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());
}
Intent intent = new Intent(CheckAvailability.this , ListAtmActivity.class);
intent.putExtra("key", result);
startActivity(intent);}
});
从这个Activity我用结果字符串调用我的第二个活动。 这是我的第二项活动。
public class ListAtmActivity extends Activity{
private static String url ="http://10.0.2.2:8080/hello/AvailabilityResponse";
private static final String TAG_ID = "id";
private static final String ATM_NO = "atmbrno";
private static final String ATM_PLACE = "atmbrname";
static InputStream is = null;
static String json = "";
static JSONArray jObj = null;
JSONArray contacts=null;
public class ListViewA extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view2);
ListView lv= (ListView)findViewById(R.id.listview);
String[] from = new String[] {"col_1", "col_2"};
int[] to = new int[] { R.id.item2, R.id.item3};
String brName=getIntent().getExtras().getString("key");
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
try {
contacts = new JSONArray(brName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
for(int i = 0; i < contacts.length(); i++){
JSONObject json_data = contacts.getJSONObject(i);
System.out.print(contacts.length());
String atm_id = json_data.getString(ATM_NO);
String atm_name = json_data.getString(ATM_PLACE);
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1", atm_id);
map.put("col_2", atm_name);
fillMaps.add(map);
}
}
catch(JSONException e) {
e.printStackTrace();
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}}
但是我无法将任何输出放到我的界面上......只有空白屏幕显示。这有什么问题?
答案 0 :(得分:0)
试试这个:
String brName = this.getIntent().getExtras().getString("key");
答案 1 :(得分:0)
在您的第二项活动中,您应该可以使用
String brName = getIntent().getStringExtra("key");
答案 2 :(得分:0)
Bundle extras = getIntent().getExtras();
String keyval = extras.getString("key");