我必须使用Base Adapter通过Listview显示6个不同列号的不同表的内容。我能够实现这一点,但现在我还要在ListView的顶部显示标题,它将告诉Column的名称,我想在ListView中执行此操作,这意味着我不想添加任何额外的页脚的视图因为我必须为不同的标题创建6个不同的视图。
对不起长码。
这是我的自定义适配器类,其中只考虑一个案例
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>
现在问题是它跳过了数据库表中的第一行,并且代替该行显示了标题,然后在该标题下面开始显示第二行,第三行和下一行。但我希望它在顶部显示标题,然后开始显示实际的表格。 而且,我不能为不同的标题创建不同的布局。有些会包含6列,有些会包含4列,有些会包含3列。
答案 0 :(得分:1)
只需在Donor视图类中添加addHeader方法。
在Donor视图类
中创建了此方法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方法中的嵌套if,else。