2012-09-17 39 views
-1

可能重复:
Android ListView with Simple AdapterAndroid的ListActivity

这是我的第一个活动。

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);} 
}); 

从这个活动我打电话给我的第二个活动与结果字符串。 这是我的第二个活动。

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

检查您是否得到任何值在_brName_字符串中还是没有。 –

+0

@http://stackoverflow.com/users/1626878/sahilmahajanmj - 是的......值得注意的是...... – Dasaya

回答

0

试试这个:

String brName = this.getIntent().getExtras().getString("key"); 
0

在你的第二个活动,你应该能够使用

String brName = getIntent().getStringExtra("key"); 
0
Bundle extras = getIntent().getExtras(); 
    String keyval = extras.getString("key");