使用x5cloud云平台来做网络彩讯-------------好友列表(六)
很多时候,避免不了数据的显示,而手机端最好的显示还是需要列表控件
list_items.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="320px" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip"> <ImageView android:paddingTop="12dip" android:layout_alignParentRight="true" android:layout_width="48px" android:layout_height="48px" android:id="@+id/ItemImage" /> <TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/ItemTitle" /> <TextView android:text="TextView02" android:layout_height="wrap_content" android:textSize="10dip" android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle" android:id="@+id/ItemText" /> </RelativeLayout>
gname.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="wrap_content"/> </LinearLayout>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gname); ListView list = (ListView)findViewById(R.id.listView1); list=listFriends(list); } public ListView listFriends(ListView list){ ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); String sql="select friends.FriendId,users.name,friends.Describe from users inner join friends on users.Id=friends.FriendId"; DataSourse ds=new DataSourse(sql,"8888888888888888",1,"Android_CX","service.x5cloud.com"); X5DBResult rs=ds.getData(); int i=0; while(rs.hasNext()){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.icon);//图像资源的ID map.put("ItemTitle", rs.getObject(2).toString()); map.put("ItemText", "\t"+rs.getObject(1).toString()); listItem.add(map); i++; } //生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源 R.layout.list_items,//ListItem的XML实现 //动态数组与ImageItem对应的子项 new String[] {"ItemImage","ItemTitle", "ItemText"}, //ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText} ); //添加并且显示 list.setAdapter(listItemAdapter); return list; }
上面的数据源采用了x5cloud云平台提供的API : X5DBResult
public class DataSourse { private String sql; private String userid; private int dbtype; private String dbname; private String serverip; X5DBResult rs = null; DataSourse(String sql,String userid,int dbtype,String dbname,String serverip){ this.sql=sql; this.userid=userid; this.dbtype=dbtype; this.dbname=dbname; this.serverip=serverip; } public X5DBResult getData(){ X5Persist x5=new X5Persist(userid,dbtype,dbname,serverip); try { rs=x5.select(sql); return rs; } catch (X5Exception e) { e.printStackTrace(); } return null; } }