这是我的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
PlannerMonth m = getItem(position);
ArrayList<PlannerMonthDebt> debts = new ArrayList<PlannerMonthDebt>();
debts = m.debtList;
int debtSize = debts.size();
if (rowView == null) {
rowView = inflater.inflate(R.layout.row_planner_month, null, true);
holder = new ViewHolder();
holder.tv1 = (TextView) rowView.findViewById(R.id.tvPlannerMonth);
holder.tv2 = (TextView)
holder.ll = (LinearLayout) rowView.findViewById(R.id.rlInnerDebtHolder);
for (int i = 0; i < debtSize; i++) {
View custom = inflater.inflate(R.layout.inner_debt_row, null);
TextView tvName = (TextView) custom.findViewById(R.id.tvInnerDebtName);
tvName.setTag(String.valueOf("tvName" + i));
TextView tvPayment = (TextView) custom.findViewById(R.id.tvInnerDebtPayment);
tvPayment.setTag(String.valueOf("tvPayment" + i));
TextView tvDebt = (TextView) custom.findViewById(R.id.tvInnerDebtBalance);
tvDebt.setTag(String.valueOf("tvDebt" + i));
TextView tvBalance = (TextView) custom.findViewById(R.id.tvInnerDebtFee);
tvBalance.setTag(String.valueOf("tvBalance" + i));
holder.ll.addView(custom);
}
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
String month = m.date;
String debtToal = m.debtTotal;
String intTotal = m.intTotal;
holder.tv1.setText(month);
holder.tv2.setText(debtToal);
// now inner debts
int size = 0;
for (PlannerMonthDebt d : debts) {
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
tvName.setText(d.name);
TextView tvPayment = (TextView) convertView.findViewWithTag(String.valueOf("tvPayment" + size));
tvPayment.setText(d.payment);
TextView tvDebt = (TextView) convertView.findViewWithTag(String.valueOf("tvDebt" + size));
tvDebt.setText(d.balance);
TextView tvBalance = (TextView) convertView.findViewWithTag(String.valueOf("tvBalance" + size));
tvBalance.setText(d.fees);
size++;
}
return rowView;
}
这是我要动态添加的行:
inner_debt_row.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" >
<TextView
android:id="@+id/tvInnerDebtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="@+id/tvInnerDebtFee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="@+id/tvInnerDebtPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="@+id/tvInnerDebtBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
我有一个LinearLayout(holder.ll),在其中添加了X个自定义行(基于上面粘贴的inner_debt_row布局).
我知道如何显示行,但是在使用setTag()查找和检索它们时遇到了麻烦,因此我可以通过setText()将值放入行中.
对于标签,我基本上为每个TextView使用相同的名称,但还应在名称中添加一个唯一的位置.我使用位置(或大小)来使每个动态添加的文本字段对于搜索而言都是唯一的.
在我的LogCat中,在此行上得到一个NullPointer:
TextView tvName =(TextView)convertView.findViewWithTag(String.valueOf(“ tvName” size)));
这是将值分配给动态TextView的第一次遇到,因此我假设找不到它们.
如何通过标签找到正确的TextView实例?
解决方法:
findViewWithTag()代码看起来正确.您是否检查convertView不为null?
创建新行(即不重用上一行)时,convertView将为null,但您仅分配的是rowView,而不分配的是convertView.