0

我是android的新手。我已经启动了 android 仅 2 天。我曾尝试在谷歌上搜索在列表视图中制作背景图像所需的操作,但所有代码都太复杂以至于我无法理解,所以请不要投票给我。

我的列表视图应该显示来自服务器的背景图像。我已经完成将我的应用程序连接到服务器。我能够显示url图像的。

这是我的php代码:

安卓首页.php

<?php

$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;
}
print(json_encode($output));
mysql_close($con);

?>

这是我的 .java

HomeActivity.java

//@SuppressLint("NewApi")

public class HomeActivity extends ListActivity{


//String to display in ListView
String [] hello = {
        "Red",
        "Blue",
        "White",
        "Black",
        "Orange",
        "Pink"
};

ArrayList<String> arrayDataFromServer = new ArrayList<String>();
String array;
String [] thisarray;
@SuppressLint("NewApi")
@Override  
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_home); 

    StrictMode.enableDefaults();
    String result ="";
    InputStream isr = null;
    try{
        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 + "\n");
        }

        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);
            array=json.getString("Photo");
            arrayDataFromServer.add(array);
            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

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


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

我在想setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayDataFromServer));我可以使用一些不同的代码在背景图像中显示它吗?

任何帮助将不胜感激。提前致谢!

编辑

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
            getListView.setBackground(dr);
4

1 回答 1

0

回答你的问题:

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

为菜鸟提供额外建议:

不要使用StrictMode.enableDefaults();它来掩盖对不良代码的非常好的保护。学习如何正确地做到这一点。AsyncTask是一个非常流行且非常易于使用的线程平台,您应该使用它。如果您有信心和冒险精神,请使用复​​杂但功能更强大的加载器!

于 2013-04-11T10:09:47.677 回答