Android 在列表视图中混合文本和图像的帮助
我的应用程序正在从网页上的Xml文件获取数据。该日期放置在列表视图中。在XML文件中,每个项目都有一个指向图像的链接。我需要一些帮助,找出将图像从xml文件与文本一起放入listView的最简单方法 在我的代码中,我刚刚在布局文件的android:src=”“中放置了一个静态图像 截图: XML文件: 主要类别:Android 在列表视图中混合文本和图像的帮助,android,image,listview,android-listview,android-imageview,Android,Image,Listview,Android Listview,Android Imageview,我的应用程序正在从网页上的Xml文件获取数据。该日期放置在列表视图中。在XML文件中,每个项目都有一个指向图像的链接。我需要一些帮助,找出将图像从xml文件与文本一起放入listView的最简单方法 在我的代码中,我刚刚在布局文件的android:src=”“中放置了一个静态图像 截图: XML文件: 主要类别: package com.bandreminder; import java.util.ArrayList; import java.util.HashMap; import o
package com.bandreminder;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class bandR extends ListActivity {
private ConnectivityManager connMgr;
private android.net.NetworkInfo network_info;
private boolean blnNetworkAccess=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
network_info = connMgr.getActiveNetworkInfo();
if(network_info != null && network_info.isConnectedOrConnecting()) {
blnNetworkAccess = true;
}
if(blnNetworkAccess) {
fetchData();
}
else {
Toast.makeText(bandR.this, "No Internet connection, please enable", Toast.LENGTH_LONG).show();
}
}
public void onResume(){
super.onResume();
if(network_info != null && network_info.isConnectedOrConnecting()) {
Log.w("resume", ""+network_info);
}
else {
Toast.makeText(bandR.this, "No Internet connection, please enable", Toast.LENGTH_LONG).show();
}
}
public void onPause(){
super.onPause();
blnNetworkAccess = false;
Log.w("onPause", "test");
}
private void fetchData() {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Integer numResults;
String xml = XmlHandler.getXML();
Document doc = XmlHandler.XMLfromString(xml);
numResults = XmlHandler.numResults(doc);
if((numResults <= 0)){
Toast.makeText(bandR.this, "No Bands found", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("artistName", "Artist Name: "+ XmlHandler.getValue(e, "artistName"));
map.put("country", "Country: " + XmlHandler.getValue(e, "country"));
map.put("scene", "Scene: " + XmlHandler.getValue(e, "scene"));
map.put("tidspunkt", "Tidspunkt: " + XmlHandler.getValue(e, "tidspunkt"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "artistName", "country", "scene","tidspunkt" },
new int[] { R.id.item_artistname, R.id.item_country,R.id.scene ,R.id.tidspunkt});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(bandR.this, "artistName '" + o.get("artistName") + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
}
}
布局:
列表占位符:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data"/>
</LinearLayout>
主要布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="7dp"
android:id="@+id/layout"
>
<TextView
android:id="@+id/item_artistname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="15dp" />
<TextView
android:id="@+id/item_country"
android:layout_below="@id/item_artistname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="@+id/scene"
android:layout_below="@id/item_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="@+id/tidspunkt"
android:layout_below="@id/scene"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<ImageView
android:id="@+id/img"
android:layout_below="@id/item_artistname"
android:layout_alignParentRight="true"
android:src="@drawable/abe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
您在xml文件中有图像URL。像处理其他值一样提取URL。 您必须实现自己的适配器,该适配器将加载图像并将它们绑定到行中的图像视图 加载图像时有答案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="7dp"
android:id="@+id/layout"
>
<TextView
android:id="@+id/item_artistname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="15dp" />
<TextView
android:id="@+id/item_country"
android:layout_below="@id/item_artistname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="@+id/scene"
android:layout_below="@id/item_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="@+id/tidspunkt"
android:layout_below="@id/scene"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<ImageView
android:id="@+id/img"
android:layout_below="@id/item_artistname"
android:layout_alignParentRight="true"
android:src="@drawable/abe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>