0

我想使用适配器显示列表视图。我的问题是项目不是一个接一个地直接显示,而是每页一个(每两个项目之间有一个空格)。

这是代码

程序项适配器.java

    public class ProgrammeItemAdapter extends BaseAdapter {  

            List<Programme> progs;  
            LayoutInflater inflater;

            static class ProgrammeHolder {
            TextView heure_deb;   
            TextView heure_fin ;  
            TextView nom ;
            TextView genre ; 
            TextView duree ;

            }
        public ProgrammeItemAdapter(Context context,List<Programme> progs) {

            inflater = LayoutInflater.from(context);

            this.progs = progs;

            }




        public View getView(int position, View convertView, ViewGroup parent) {


            ProgrammeHolder holder=null;

            if(convertView == null) {

            holder = new ProgrammeHolder();

            convertView = inflater.inflate(R.layout.item_programme, null);

            holder.heure_deb = (TextView) convertView.findViewById(R.id.heure_debut);  
            holder.heure_fin = (TextView) convertView.findViewById(R.id.heure_fin);   
            holder.nom = (TextView) convertView.findViewById(R.id.nom_programme); 
            holder.genre = (TextView) convertView.findViewById(R.id.genre_programme); 
            holder.duree = (TextView) convertView.findViewById(R.id.duree_programme);

            convertView.setTag(holder);

            } else {

            holder = (ProgrammeHolder) convertView.getTag();

            }

            holder.heure_deb.setText(progs.get(position).getHeure_deb());

            holder.heure_fin.setText(progs.get(position).getHeure_fin());
            holder.nom.setText(progs.get(position).getNom());
            holder.genre.setText(progs.get(position).getGenre());
            holder.duree.setText(progs.get(position).getDuree());

            return convertView;

            }








        public int getCount() {
            // TODO Auto-generated method stub
            return progs.size();
        }

        public Object getItem(int position) {
            return progs.get(position);

        }

        public long getItemId(int position) {
            return position;
        }




  }

程序活动.java

public class ProgrammeTvActivity extends Activity{
    private ListView listView;    
    private ProgrammeItemAdapter adapter;



    @Override      

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.programme_list);

        listView = (ListView) findViewById(R.id.listview_programme);
        List<Programme> m_programmes= new ArrayList<Programme>(); 

        m_programmes=FillList();


        adapter = new ProgrammeItemAdapter(this, m_programmes); 
        adapter.notifyDataSetChanged(); 
        listView.setAdapter(adapter);








    }
private List<Programme>  FillList(){
        List<Programme> m_programmes= new ArrayList<Programme>(); 
        try{

          //  m_programmes.clear();
            Programme p1 = new Programme("10:00-","11:00","programme1","série","60 mn");
            Programme p2 = new Programme("11:05-","12:00","programme2","foot","55 mn");
            /*p1.setHeure_deb("10:00");
            p1.setHeure_fin("10:50");
            p1.setNom("ddd");
            p1.setGenre("serie");
            p1.setDuree("50mn");*/
            m_programmes.add(p1);        

            m_programmes.add(p2);
            return m_programmes;


          } catch (Exception e) { 
            Log.e("BACKGROUND_PROC", e.getMessage());
            return (m_programmes);
          }

      }

item_programme.xml

 <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

     android:id="@+id/Layout1"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:orientation="horizontal"
    android:background="@drawable/fond_rouge">

  <TextView
                   android:id="@+id/heure_debut"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:gravity="left"                    
                   android:layout_marginTop="10dip"
                   android:layout_marginLeft="10dip"
                   android:textSize="12sp" 
                   android:textStyle="bold" 
                   android:textColor="@android:color/white"   

                   />
    <TextView
                   android:id="@+id/heure_fin"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:gravity="left"                    
                   android:layout_marginTop="10dip"
                   android:textSize="12sp" 
                   android:textStyle="bold" 
                   android:textColor="@android:color/white"   

                   />


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_marginTop="0dip"
    android:layout_marginLeft="10dip"
    >  


        <TextView
                   android:id="@+id/nom_programme"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:gravity="left"                    
                   android:layout_marginTop="10dip"
                   android:textSize="14sp" 
                   android:textStyle="bold" 
                   android:textColor="@android:color/white" 

                   />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Layout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dip"
        android:layout_marginTop="0dip"
        android:orientation="horizontal" > 



        <TextView
                   android:id="@+id/genre_programme"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"                  
                   android:layout_marginLeft="0dp"
                   android:layout_marginTop="0dip"
                   android:textSize="12sp"                    
                   android:textColor="@android:color/white"  


                   />  

     <TextView
                   android:id="@+id/duree_programme"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"                  
                   android:layout_marginLeft="10dp"
                   android:layout_marginTop="0dip"
                   android:textSize="12sp"                    
                   android:textColor="@android:color/white"   



                   />   


         </LinearLayout>         


    </LinearLayout>

</LinearLayout> 

program_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/fond_rouge" 
 android:id="@+id/Layout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">






<ListView
    android:id="@+id/listview_programme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
  />

感谢帮助。

4

1 回答 1

0
<?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/heure_debut"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="left"                    
               android:layout_marginTop="10dip"
               android:layout_marginLeft="10dip"
               android:textSize="12sp" 
               android:textStyle="bold" 
               android:text="heure_debut"
               android:textColor="@android:color/white"   
               />
    <TextView android:id="@+id/heure_fin"
               android:text="heure_fin"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="left"                    
               android:layout_marginLeft="10dip"
               android:textSize="12sp" 
               android:textStyle="bold" 
               android:textColor="@android:color/white"   

               />

    <TextView  android:id="@+id/nom_programme"
               android:layout_width="wrap_content"
               android:text="nom_programme"
               android:layout_height="wrap_content"
               android:gravity="left"                    
               android:layout_marginLeft="10dip"
               android:textSize="14sp" 
               android:textStyle="bold" 
               android:textColor="@android:color/white" 

               />

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:orientation="horizontal" > 

    <TextView  android:id="@+id/genre_programme"
               android:text="genre_programme"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"                  
               android:layout_marginLeft="0dp"
               android:textSize="12sp"                    
               android:textColor="@android:color/white"  
               />  

    <TextView android:id="@+id/duree_programme"
            android:text="duree_programme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                  
            android:layout_marginLeft="10dp"
            android:textSize="12sp"                    
            android:textColor="@android:color/white"   
            />   

     </LinearLayout>         

 </LinearLayout>

如果你想逐行列出项目然后尝试用上面的 xml 替换你的 item_programme.xml 并检查

于 2012-05-02T12:27:04.710 回答