gpt4 book ai didi

android - Mysql在Android应用中没有得到完美的结果

转载 作者:行者123 更新时间:2023-11-30 23:35:43 25 4
gpt4 key购买 nike

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

第一张图片你可以看到大学列表。当我点击第一所大学时,我得到了完美的结果当我点击上一所大学时,我没有得到结果..请帮助

在这里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//Second Activity 它给我大学的信息//第二张和第三张图片

    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

最佳答案

看看你的这部分代码:

     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中解析它。在循环覆盖适配器之后,您必须对 onclicklistener 进行编程,使用 arrayData 知道您点击的是哪个项目(具有位置)。

也许你有一个辅助阵列可以很容易地做到这一点。

关于android - Mysql在Android应用中没有得到完美的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com