2016-01-06 95 views
0

我必須通過使用Base Adapter的Listview來顯示6個不同列號的內容。我能夠實現這一點,但現在我也想在ListView的頂部顯示標題,它將顯示列的名稱,並且我想在ListView中執行此操作,這意味着我不想添加任何額外的對於頁腳的看法,因爲我將不得不爲不同的頁眉創建6個不同的視圖。如何使用Base Adapter動態顯示Listview標題?

對不起,長代碼。

這裏是我的自定義適配器類,其中只有一宗被認爲

package com.example.bilalrafique.bloodbankmanagementsystem; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 
import java.util.List; 
public class CustomAdapter extends BaseAdapter { 
    Context context; 
    List<ListType> entriesList; 
    boolean header=true; 
public CustomAdapter(Context context,List<ListType> entriesList) { 
    this.context=context; 
    this.entriesList=entriesList; 
} 

@Override 
public int getCount() { 
    return entriesList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return entriesList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return entriesList.get(position).getId(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v=null; 
    if(entriesList.get(position).getTableType().equals("Dnr")){ 
     v=View.inflate(context,R.layout.layout_lv_dnrentry,null); 
     TextView id = (TextView) v.findViewById(R.id.dnrEId); 
     TextView name = (TextView) v.findViewById(R.id.dnrEName); 
     TextView age = (TextView) v.findViewById(R.id.dnrEAge); 
     TextView gender = (TextView) v.findViewById(R.id.dnrEGender); 
     TextView bloodGroup = (TextView) v.findViewById(R.id.dnrEBloodGroup); 
     if(position==0 && header==true){ 
      id.setBackgroundColor(context.getResources().getColor(android.R.color.background_light)); 
      name.setBackgroundColor(context.getResources().getColor(android.R.color.background_light)); 
      age.setBackgroundColor(context.getResources().getColor(android.R.color.background_light)); 
      gender.setBackgroundColor(context.getResources().getColor(android.R.color.background_light)); 
      bloodGroup.setBackgroundColor(context.getResources().getColor(android.R.color.background_light)); 
      header=false; 
     } 
     else { 
      id.setText(String.valueOf(entriesList.get(position).getId())); 
      name.setText(String.valueOf(entriesList.get(position).getName())); 
      age.setText(String.valueOf(entriesList.get(position).getAge())); 
      gender.setText(String.valueOf(entriesList.get(position).getGender())); 
      bloodGroup.setText(String.valueOf(entriesList.get(position).getBloodGroup())); 
     } 
    } 
return v; 
} 
} 

這是DonorView類

package com.example.bilalrafique.bloodbankmanagementsystem; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.ListView; 
import java.util.ArrayList; 
import java.util.List; 
public class DonorView extends Activity { 
List<ListType> entriesList; 
Cursor dCur; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_dnr_view); 
    dCur=MainActivity.db.rawQuery("SELECT * FROM Donor",null); 
    entriesList=new ArrayList<>(); 
    ListType.populateList(this,entriesList, dCur,"Donor"); 
    ListView lvDnr=(ListView)findViewById(R.id.lvDnrView); 
    CustomAdapter lvAdapter=new CustomAdapter(this,entriesList); 
    lvDnr.setAdapter(lvAdapter); 
} 
} 

這是layout_dnrview.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"> 
<ListView 
    android:id="@+id/lvDnrView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 
</LinearLayout> 

這是layout_lv_dnrentry.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="horizontal" 
android:id="@+id/lvDnr_linear"> 
<TextView 
    android:layout_width="50dp" 
    android:layout_height="wrap_content" 
    android:text="Id" 
    android:id="@+id/dnrEId" 
    android:textSize="20dp" 
    android:gravity="center_vertical" 
    android:textColor="#000000" 
    android:background="@color/material_deep_teal_500" 
    android:paddingLeft="5dp"/> 
<TextView 
    android:layout_width="235dp" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:text="Name" 
    android:id="@+id/dnrEName" 
    android:background="@color/accent_material_dark" 
    android:textSize="20dp" 
    android:textColor="#000000" 
    android:paddingLeft="40dp"/> 
<TextView 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    android:text="Age" 
    android:gravity="center_vertical" 
    android:id="@+id/dnrEAge" 
    android:textSize="20dp" 
    android:background="@color/accent_material_dark" 
    android:textColor="#000000" 
    android:paddingLeft="10dp"/> 
<TextView 
    android:layout_width="85dp" 
    android:layout_height="wrap_content" 
    android:text="M/F" 
    android:gravity="center_vertical" 
    android:id="@+id/dnrEGender" 
    android:textSize="20dp" 
    android:background="@color/accent_material_dark" 
    android:textColor="#000000" 
    android:paddingLeft="20dp"/> 
<TextView 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:text="Bld Grp" 
    android:gravity="center_vertical" 
    android:id="@+id/dnrEBloodGroup" 
    android:textSize="20dp" 
    android:background="@color/accent_material_dark" 
    android:textColor="#000000" 
    android:paddingLeft="20dp"/> 
</LinearLayout> 

現在的問題是,它跳過數據庫表中的第一行,並代替該行顯示標題,然後在該標題下開始顯示第2行,第3行和下一行。但我希望它在頂部顯示標題,然後開始顯示實際的表格。 而且,我無法爲不同的標題創建不同的佈局。一些將包含6列一些會4和一些會3.

回答

1

只需要在捐助者視圖類添加addHeader方法。

創造了這個方法在捐助者視圖類

public View donorHeader(Context c){ 
    View v=View.inflate(c,R.layout.layout_lv_dnrentry,null); 
    TextView id = (TextView) v.findViewById(R.id.dnrEId); 
    TextView name = (TextView) v.findViewById(R.id.dnrEName); 
    TextView age = (TextView) v.findViewById(R.id.dnrEAge); 
    TextView gender = (TextView) v.findViewById(R.id.dnrEGender); 
    TextView bloodGroup = (TextView) v.findViewById(R.id.dnrEBloodGroup); 
    id.setBackgroundColor(c.getResources().getColor(android.R.color.background_light)); 
    name.setBackgroundColor(c.getResources().getColor(android.R.color.background_light)); 
    age.setBackgroundColor(c.getResources().getColor(android.R.color.background_light)); 
    gender.setBackgroundColor(c.getResources().getColor(android.R.color.background_light)); 
    bloodGroup.setBackgroundColor(c.getResources().getColor(android.R.color.background_light)); 
    return v; 
} 

,並在DonorView onCreate方法的代碼更改爲'

CustomAdapter lvAdapter=new CustomAdapter(this,entriesList); 
    lvDnr.addHeaderView(donorHeader(this)); 
    lvDnr.setAdapter(lvAdapter); 

現在刪除嵌套如果,否則在定義適配器getView方法。

相關問題