2012-05-02 178 views
0

我想使用適配器顯示列表視圖。我的問題是,項目不是一個接一個直接顯示,而是每個頁面顯示一個項目(每個項目之間有一個空格)。帶適配器的列表視圖android

這裏是代碼

ProgrammeItem Adapter.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; 
     } 




    } 

ProgrammeActivity.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> 

programme_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" 
    /> 

感謝您的幫助。

+0

是什麼程序清單< Programm>包含? – sush

回答

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> 

如果u想列表項由lline線,然後嘗試使用上面的XML替換您item_programme.xml和檢查