解决方案 »
- File.exists()无法检测到SD卡上已经存在的文件
- 怎么入门android驱动开发
- 求基于android的公交查询源码
- 系统颜色android secondary_text_dark在哪
- 如何用子线程创建Dialog?
- 各位牛人,为什么在2.3上正常工作的拍照应用在4.3上崩溃?
- android audiomanage 无法调整音量的问题
- listview中item为gridview,gridview中显示本地图片,图片一多就出现OOM啦,如何解决?求大婶
- 求大神帮助解答如何测量android应用中各个部位耗电情况的做法
- 内存泄漏
- 使用ant编译android工程遇到的问题
- ViewPage+Fragment替换Frament问题
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class History extends Activity{
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
putData(datas,"time", "2014/12/23傍晚","category","Mac地址绑定错误");
putData(datas,"time", "2014/12/22中午","category","用户名或密码错误");
SimpleAdapter sa = new SimpleAdapter(this,
datas,
R.layout.history_list, //加载外部布局
new String[]{"time","category"},
new int[]{R.id.ht_tv_time,R.id.ht_tv_category});
lv = (ListView) findViewById(R.id.ht_lv_list);
lv.setAdapter(sa);
}
private void putData(List<Map<String, String>> datas,String key1,String value1,String key2,String value2){
Map<String,String> data = new HashMap<String, String>();
data.put(key1,value1);
data.put(key2,value2);
datas.add(data);
}
}activity 的XML<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:id="@+id/ht_title"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:gravity="center_horizontal|center_vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="报修历史"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout> <ListView
android:id="@+id/ht_lv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/ht_title" >
</ListView></RelativeLayout>外部XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/ht_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ht_lv_list"
android:textSize="15sp" >
</TextView> <TextView
android:id="@+id/ht_tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ht_tv_time"
android:layout_marginTop="5dp"
android:textSize="20sp" >
</TextView></LinearLayout>
因为你加载adpter的时候用是activity的XML,所有就又多了2次activity的XML。也就看到了你这个效果。记得加载adpter一定要创建一个新的XML文件布局
2#,代码已经贴上了。
//通过inflater将R.layout.history_top布局转换为View对象,再通过ListView中的addHeaderView方法添加进去
View header = getLayoutInflater().inflate(R.layout.history_top, null);
lv.addHeaderView(header);<?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="45dp"
android:id="@+id/ht_title"
android:background="@drawable/title_bar"
android:gravity="center_horizontal|center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ht_tv_head"
android:text="报修历史"
android:textSize="20sp"
android:textColor="#ffffff" />
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/ht_lv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
<TextView
android:layout_below="@id/ht_lv_list"
android:id="@+id/ht_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp">
</TextView>
<TextView
android:layout_below="@id/ht_tv_time"
android:layout_marginTop="5dp"
android:id="@+id/ht_tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp">
</TextView>
</RelativeLayout>