table 布局中不需要的行

Unwanted rows in a table layout

在我的适配器 class 中,我想制作一个只有 4 行的 table 布局,以在不同的 4 行中表示我的 4 个值。

一切都很好,除了行数增加了两倍(12 行),但我将其设计为只有 4 行,如下面的代码所示:

public View getView(final int position, View view, ViewGroup parent) {
    final arraybill.ViewHolder holder;

    if (view == null) {
        holder = new arraybill.ViewHolder();
        view = inflater.inflate(R.layout.layout_bill, null);
        // Locate the TextViews in listview_item.xml
        holder.ll = (TableLayout) view.findViewById(R.id.displayLinear);

        view.setTag(holder);
    } else {
        holder = (arraybill.ViewHolder) view.getTag();
    }
    // Set the results into TextViews



    label = new String[] {"A","B","C","D"};
    value = new String[]{modelList.get(0).getbill(),modelList.get(0).getaccepting(),
            modelList.get(0).getdeclining(),modelList.get(0).getwaiting()};



    for (int i = 0; i < label.length; i++) {

        TableRow row= new TableRow(mContext);
        row.setGravity(Gravity.CENTER);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);

        tv = new TextView(mContext);
        tv.setPadding(20,10,5,10);
        tv.setTextColor(Color.parseColor("#000000"));
        tv.setText(label[i]);

        tv2 =new TextView(mContext);
        tv2.setText("");
        tv2.setPadding(10,10,10,10);
        tv2.setTextColor(Color.parseColor("#000000"));

        tv3 =new TextView(mContext);
        tv3.setText(value[i]);
        tv3.setPadding(5,10,20,10);
        tv3.setTextColor(Color.parseColor("#000000"));

        row.addView(tv3,0);
        row.addView(tv2,1);
        row.addView(tv,2);

        row.setBackgroundResource(R.drawable.border);
        holder.ll.setColumnShrinkable(0,true);
        holder.ll.addView(row,i);
   }

    //...

    return view;
}

我不是专家,所以我的 "help" 可能完全错误,但我尝试:label.length 是 4,所以您创建 4X3 tv、tv2、tv3 并添加它们。这就是为什么你有 12 行而不是我猜的 4 行。

尝试将 holder.ll.removeAllViews(); 放在 for 周期之前:

label = new String[] {"A","B","C","D"};
value = new String[]{modelList.get(0).getbill(),modelList.get(0).getaccepting(),
        modelList.get(0).getdeclining(),modelList.get(0).getwaiting()};

holder.ll.removeAllViews();

for (int i = 0; i < label.length; i++) {

    TableRow row= new TableRow(mContext);
    row.setGravity(Gravity.CENTER);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);

    //...

}