在delphi和.net开发中,我们可以使用现成的Grid控件来展现数据,而在android中,则可以用Android Listview来展现网格数据,下面是几种方式和效果图:
第一种效果图:
嵌入式Listview:就是说定义一个xml的layout作为另外一个界面的Listview数据集
activity_chuangbo_detail_linear.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffff03"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/tv2"
android:layout_width="40dip"
android:padding="0dip"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"
android:textColor="#ffffff"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/tv3"
android:layout_width="40dip"
android:padding="0dip"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"
android:textColor="#03ffff"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv4"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"
android:textColor="#ff90ff"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/tv5"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"
android:textColor="#ffff03"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv6"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"
android:textColor="#ffffff"
android:gravity="center_horizontal"/>
</LinearLayout>
activity_chuangbo_detail.xml
<?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">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:text="船名航次:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip">
<TextView android:id="@+id/tv11"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="船名航次"
android:textColor="#ffff03"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv21"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="计划航线"
android:textColor="#ffffff"
android:layout_marginLeft="5dip"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv31"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="始发港"
android:layout_marginLeft="5dip"
android:textColor="#03ffff"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv41"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="中途港1"
android:layout_marginLeft="5dip"
android:textColor="#ff90ff"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv51"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="中途港2"
android:layout_marginLeft="5dip"
android:textColor="#ffff03"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv61"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="中途港3"
android:layout_marginLeft="5dip"
android:textColor="#ffffff"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv71"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="中途港4"
android:layout_marginLeft="5dip"
android:textColor="#03ffff"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv81"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="目的港"
android:layout_marginLeft="5dip"
android:textColor="#ff90ff"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginTop="5dip">
</ListView>
</LinearLayout>
</LinearLayout>
填充数据代码:
protected void InitListView() {
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
map1.put("H", "宝航06轮11020");
map1.put("P", "长+汉-虞-申");
map1.put("G", "武汉");
map1.put("B", "预 02/0100 抵常熟");
map1.put("W", "");
map1.put("R", "上海");
list.add(map1);
map2.put("H", "长虹集2号轮11032");
map2.put("P", "干+申-津");
map2.put("G", "上海");
map2.put("B", "");
map2.put("W", "");
map2.put("R", "天津");
for (int i = 0; i < 20; i++) {
list.add(map2);
}
la = new chuanbo_detail_linearadapter(chuanbo_detail.this, list);
listView = (ListView) findViewById(R.id.list_result);
listView.setAdapter(la);
}
chuanbo_detail_linearadapter.java
package com.nantsing.infoquery;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class chuanbo_detail_linearadapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> list;
String str = null;
public chuanbo_detail_linearadapter(Context context,
ArrayList<HashMap<String, String>> list) {
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size() + 1;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = LayoutInflater.from(context).inflate(R.layout.activity_chuangbo_detail_linear, null);
TextView tv1 = (TextView) view.findViewById(R.id.tv1);
TextView tv2 = (TextView) view.findViewById(R.id.tv2);
TextView tv3 = (TextView) view.findViewById(R.id.tv3);
TextView tv4 = (TextView) view.findViewById(R.id.tv4);
TextView tv5 = (TextView) view.findViewById(R.id.tv5);
TextView tv6 = (TextView) view.findViewById(R.id.tv6);
Log.v("tag111",position+"");
if (position ==list.size()) {
if (null == str) {
str = "";
}
}
else {
tv1.setText(list.get(position).get("H"));
tv2.setText(list.get(position).get("P"));
tv3.setText(list.get(position).get("G"));
tv4.setText(list.get(position).get("B"));
tv5.setText(list.get(position).get("W"));
tv6.setText(list.get(position).get("R"));
}
return view;
}
}