简体   繁体   English

Mysql在Android应用程序中未获得理想的结果

[英]Mysql didn't get perfect result in Android Application

这是大学的第一张图片列表(Mysql Search)

First Image u can see List of college. 您可以看到大学列表的第一张图片。 When i clicked first college i got perfect result when i clicked last college i didn't get Result..plz help 当我单击第一所大学时,我获得了完美的结果。当我单击最后一所大学时,我没有得到Result..plz帮助

here 1)ListCollege.java//First Activity it's give me list of College//First Image 这里1)ListCollege.java//第一个活动给了我College //第一个图像的列表

    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//Second Activity it's give me information of college//second and Third Image 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());
     }

}

} }

点击第一大学

在此处输入图片说明

Look at this part of yout code: 看一下您的代码的这一部分:

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

You are Overriding the adapter and itemclicklistener of all items while looping 您正在循环时覆盖所有项目的适配器和itemclicklistener

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

First what you need to do is save the result in result1 parsing it. 首先,您需要做的是将结果保存在result1中进行解析。 After the loop Override the adapter , you must program the onclicklistener , using the arrayData to know which item is (with the position) are you clicking in . 在循环“覆盖适配器”之后,您必须使用数组数据来编程onclicklistener,以了解您单击的是哪个项目(及其位置)。

Maybe it can be easy for you to have an auxiliar array to do it. 也许有一个辅助数组可以很容易地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM