[英]Android - Display background image in ListView
I am a newbie in android. 我是android的新手。 I have started android for only 2 days. 我只启动了android 2天。 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. 我能够显示图像的url
。
Here is my php code: 这是我的PHP代码:
AndroidHome.php AndroidHome.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);
?>
Here is my .java 这是我的.java
HomeActivity.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();
}
} }
activity_home.xml activity_home.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>
I am thinking that at the setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayDataFromServer));
我正在考虑在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! 提前致谢!
EDIT 编辑
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);
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
getListView().setBackground(dr);
to give extra advice for a noob: 给菜鸟更多建议:
DO NOT use StrictMode.enableDefaults();
不要使用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. AsyncTask是一个非常流行且非常易于使用的线程平台,您应该使用它。 And if you feel confident and adventurous use Loaders which are a bit more complex but far more powerful! 如果您有信心和冒险精神 ,请使用装载机 , 装载机要稍微复杂一些,但功能却强大得多!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.