2013-04-11 10:03 阅读 83

Android - 在ListView中显示背景图像

I am a newbie in android. I have started android for only 2 days. I have tried googling on what I needed to do to make the background image in a listview, but all the codes were all too complicated that I couldn't understand, so please don't vote me down.

My list view is supposed to display a background image from the server. I have accomplished connecting my app to the server. I am able to display the url of the images.

Here is my php code:



$con = mysql_connect("localhost", "load2unet_root", "hzXC3rUm");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("load2unet_db", $con);
$result = mysql_query("SELECT Photo FROM Home ORDER BY HomeID DESC");

while($row = mysql_fetch_assoc($result))
    $output[] = $row;


Here is my .java



public class HomeActivity extends ListActivity{

//String to display in ListView
String [] hello = {

ArrayList<String> arrayDataFromServer = new ArrayList<String>();
String array;
String [] thisarray;
public void onCreate(Bundle savedInstanceState){

    String result ="";
    InputStream isr = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new     HttpPost("http://www.mysite.com/android/AndroidHome.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();

    catch (Exception e)
        Log.e("log_tag", "Error in http connection" +e.toString());
        //resultView.setText("Couldn't connect to database");

    try {
        BufferedReader reader = new BufferedReader(new    InputStreamReader(isr, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
            sb.append(line + "

        result = sb.toString();
        Log.e("log_tag", "BufferedReader:" +result);

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

    try {
        String s = "";
        JSONArray jArray = new JSONArray(result);

        for (int i = 0; i< jArray.length(); i++)
            JSONObject json = jArray.getJSONObject(i);
            Log.e("log_tag", "Array: " +array);
            Log.e("log_tag", "arrayDataFromServer: " +arrayDataFromServer);


    catch (Exception e)
        Log.e("log_tag", "Error Parsing Data"+e.toString());
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayDataFromServer));

public void onListItemClick(
        ListView parent, View v, int position, long id)
    //Toast.makeText(this, "You have selected" + colors[position], Toast.LENGTH_LONG).show();



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- Main ListView
         Always give id value as list(@android:id/list)

        android:layout_height="wrap_content" >


I am thinking that at the setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayDataFromServer)); I can use some different code to display it in a background image?

Any help would be very much appreciated. Thanks in Advance!


String urlString = URLEncoder.encode(array);
            URL url = new URL(urlString);
            Bitmap bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            Drawable dr = new BitmapDrawable(bm); // that's the bitmap you downloaded from your server
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    donglv9116 donglv9116 2013-04-11 10:09

    to answer your question:

     // you can call this from inside the ListActivity
     Drawable dr = new BitmapDrawable(bitmap); // that's the bitmap you downloaded from your server

    to give extra advice for a noob:

    DO NOT use StrictMode.enableDefaults(); with that you're masking out a very good protection against bad code. Learn how to do it properly. AsyncTask is a very popular and very easy to use threading platform and you should use it. And if you feel confident and adventurous use Loaders which are a bit more complex but far more powerful!

    点赞 评论 复制链接分享