我有一个listview并使用arrayadpter创建它但是当我尝试添加数据时,它显示空行,每次我试图清空数据,香港专业教育学院检查数据库,输入的信息只是从未显示在行上。代码如下:
public class Column_Adapter extends ArrayAdapter<Income>{
public LayoutInflater inflater;
public ArrayList<Income> incomes;
public int viewResource;
public Column_Adapter(Context context, int textViewResourceId, ArrayList<Income> incomes){
super(context, textViewResourceId, incomes);
this.incomes = incomes;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewResource = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent){
convertView = inflater.inflate(viewResource, null);
Income income = incomes.get(position);
if(income !=null) {
TextView infoView = (TextView) convertView.findViewById(R.id.expense_type);
TextView amountView = (TextView) convertView.findViewById(R.id.expense_amount);
TextView budgetView = (TextView) convertView.findViewById(R.id.expense_budget);
if (infoView != null) {
infoView.setText(income.getiDate());
}if(amountView != null){
amountView.setText(income.getiAmount());
}if(budgetView != null){
budgetView.setText(income.getiMethod());
}
}
return convertView;
}
}
布局模板:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/expense_budget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eExpInc"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_alignBaseline="@+id/expense_amount"
android:layout_alignBottom="@+id/expense_amount"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="18sp"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/expense_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eDefaultType"
android:layout_marginRight="52dp"
android:layout_marginEnd="52dp"
android:layout_alignBaseline="@+id/expense_amount"
android:layout_alignBottom="@+id/expense_amount"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/expense_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:text="@string/eDefaultAmount"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:0)
似乎你忘记了这个
public int getCount() {
if(incomes!=null)
return incomes.size();
return 0;
}
答案 1 :(得分:0)
以下链接包含的代码可以帮助您
http://www.mediafire.com/file/wy6pv7664jp5m8m/CustomListviewAdapterExam.7z
这将在listadapter类和一个活动
上有setter和getter类