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
here 1)ListCollege.java//First Activity it's give me list of College//First Image
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
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
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. 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 .
Maybe it can be easy for you to have an auxiliar array to do it.
精彩评论