【发布时间】:2014-05-18 19:17:44
【问题描述】:
这是我的代码:
@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()。
对于tag,我基本上为每个TextView 使用相同的名称,但还要在名称中添加一个唯一的位置。我使用位置(或大小)来保持每个动态添加的文本字段对于搜索都是唯一的。
在我的LogCat 中,我在这一行得到一个NullPointer:
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
这是第一次为动态TextView 赋值,所以我认为它们都找不到。
如何通过tag 找到正确的 TextView 实例?
【问题讨论】:
标签: android listview textview android-adapter