-1

可能重复:
带有简单适配器的 Android ListView

这是我的第一个活动。

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

    }

}}

但我无法在我的界面中输出任何内容……只有空白屏幕显示。这有什么问题?

4

3 回答 3

0

尝试这个:

String brName = this.getIntent().getExtras().getString("key");
于 2012-09-17T06:46:19.063 回答
0

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

String brName = getIntent().getStringExtra("key");
于 2012-09-17T06:58:51.937 回答
0
Bundle extras = getIntent().getExtras();
    String keyval = extras.getString("key");
于 2012-09-17T07:06:42.370 回答