ListView中的有关问题,有点意思

ListView中的问题,有点意思
问题:
报修历史出现了三次,我原本是要它出现一次的,望大神解决之!

ListView中的有关问题,有点意思
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: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:text="报修历史"
         android:textSize="20sp"
android:textColor="#ffffff" />       
</LinearLayout>
    <ListView
        android:layout_below="@id/ht_title"
        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>

java 代码部分:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.gdin_network_service_system.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
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.activity_history, 
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);
}
}


------解决思路----------------------
你SimpleAdapter 里面的布局是加载另一个布局,不能加载原来的大的布局,你这样写是加载了三次activity_history ,所以会出现三个保修历史, 你只要把activity_history里面下面的两个TextView写到另外的一个布局里面,然后SimpleAdapter加载这个布局就行了
------解决思路----------------------
activity

package app.example.test12_24;

import java.util.ArrayList;
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文件布局
------解决思路----------------------
加载adpter时,要重新写一个item.xml 布局,用来显示在listview中,每一行的内容。


2#,代码已经贴上了。

------解决思路----------------------
额,#4就是最完美的了?ListView中的有关问题,有点意思