这是我的代码:
@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.
总结以上是内存溢出为你收集整理的android-通过Adapter getView()方法中的标记查找动态创建的TextViews全部内容,希望文章能够帮你解决android-通过Adapter getView()方法中的标记查找动态创建的TextViews所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)