Mysql在Android应用程序中没有得到完美的结果

时间:2011-09-24 09:32:42

标签: android mysql

This one is Frist Image List of College (Mysql Search)

第一张图片你可以看到大学名单。 当我点击第一次大学时,我得到了完美的结果 当我点击上一次大学时我没有得到Result..plz帮助

这里 1)ListCollege.java//First Activity它给我学院//第一张图片列表

    collegelist=(ListView)findViewById(R.id.collegeList);
    InputStream is = null;

    String result = "";
     //the year data to send
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("searchcollege",search_college));
     nameValuePairs.add(new BasicNameValuePair("searchcity",search_city));

     //http post
     try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://www.career4u.org/android/android_connection.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);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
             }
             is.close();
             result=sb.toString();
             String result1[]=result.split("\\}\\,\\{");
             for(int i=0;i<=result1.length;i++)
             {
                     result1[i]= result1[i].replace("[{", "").replace("institue_name", "").replace("\"", "").replace("}]", "").replace(":", "");
                     collegelist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , result1));  
                     collegelist.setOnItemClickListener(new OnItemClickListener() {
                          public void onItemClick(AdapterView<?> parent, View view,
                              int position, long id) {
                            Intent intent = new Intent(ListCollege.this,viewCollege.class);
                            intent.putExtra("search_college", ((TextView) view).getText());
                            startActivity(intent);
                         }
                        });
             } 

     }catch(Exception e){
             Log.e("log_tag", "Error converting result "+e.toString());
     }
     //parse json data
}

}

2)viewCollege.java//第二个活动它给我的大学信息//第二和第三张图片

    String search_college =getIntent().getExtras().getString("search_college");
    collegeName.setText("    "+search_college);

    InputStream is = null;

    String result = "";
     //the year data to send
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("searchcollege",search_college));


     //http post
     try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://www.career4u.org/android/android_collegeDetail.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);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                     sb.append(line + "\n");
             }
             is.close();
             result=sb.toString();
             **collegeAddress.setText(result);//Display Mysql Result in JSON**
             //String result1[]=result.split("\\}\\,\\{");
             //for(int i=0;i<result1.length;i++)
             //{
              //     result1[i]= result1[i].replace("[{", "").replace("institue_name", "").replace("\"", "").replace("}]", "").replace(":", "");
             //} 

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

}

}

click on First College

enter image description here

1 个答案:

答案 0 :(得分:0)

看看你的代码部分:

     for(int i=0;i<=result1.length;i++)
     {
             result1[i]= result1[i].replace("[{", "").replace("institue_name", "").replace("\"", "").replace("}]", "").replace(":", "");
             collegelist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , result1));  
             collegelist.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                    Intent intent = new Intent(ListCollege.this,viewCollege.class);
                    intent.putExtra("search_college", ((TextView) view).getText());
                    startActivity(intent);
                 }
                });
     } 

循环时,您正在覆盖所有项目的适配器和itemclicklistener

collegelist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , result1));
collegelist.setOnItemClickListener(new OnItemClickListener()...

首先,您需要将结果保存在解析它的result1中。在循环覆盖适配器之后,必须使用arrayData对onclicklistener进行编程,以便知道您单击了哪个项目(带位置)。

也许您可以轻松地使用辅助阵列来完成它。