I am able to have a list view with results pulled from a mysql remote database depending on an input from a user with the code below. The last thing I need to do is add an Icon in the list view with a dynamic Icon coming from a URL for each result. I am trying to keep it within the same activity/java file. Does anyone know how to do this? If so, can you please edit my code to show the results? Thanks.
'=========================================================
package com.packagename;
import android.app.ListActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Users extends ListActivity {
InputStream is;
ArrayList<String> results = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String result = "";
String uv = "";
uv = getIntent().getExtras().getString("dated");
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year",uv));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/Android53D6E3B5KF83S/users.php");
httppost.setEntity(new
UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
Toast.makeText(getApplicationContext(),"failed"+e.toString(), Toast.LENGTH_SHORT).show();
}
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();
}catch(Exception e)
{
Log.e("log_tag", "Error converting result"+e.toString());
Toast.makeText(getApplicationContext(),"failed"+e.toString(), Toast.LENGTH_SHORT).show();
}
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
json_data = jArray.getJSONObject(i);
results.add((String) json_data.get("id") + " "+ json_data.get("name")+ " " + json_data.get("sex")+ " " + json_data.get("birthyear"));
}
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
ListView lv; lv = getListView(); lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View name, int id, long resultsb) {
Toast.makeText(getApplicationContext(), " (Selection #" + resultsb +") ", Toast.LENGTH_SHORT).show();
}});
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(),"No Entries", Toast.LENGTH_SHORT).show();
}
}
}