在Adapter getView()方法中按标记查找动态创建的TextView

时间:2014-05-18 19:17:45

标签: android listview textview android-adapter

这是我的代码:

@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实例?

2 个答案:

答案 0 :(得分:4)

findViewWithTag()代码看起来是正确的。您是否检查过convertView是否为空?

创建新行时

convertView将为null(即不重复使用前一行),但您只分配rowView,而不是convertView

答案 1 :(得分:0)

每个职位都有不同的debtSize。当convertView != null debtSize NullPointer {{1}}时,{{1}}为{{1}}时,您有空文本视图。