Use view to inflate multiple times

前端 未结 4 1008
Happy的楠姐
Happy的楠姐 2021-01-11 19:57

I have a some problem regarding inflating and re-using the same TextView.
Its like its trying to overwrite the same textview over and over again or something and it can

相关标签:
4条回答
  • 2021-01-11 20:01

    You're trying to add a single TextView to multiple rows, which is what is causing your error. You'll need to reinflate layout_number each time to get a new TextView.

    I'm not sure what is contained within your layout_number file, but based off what you have here, it seems like you might be better served to create new TextViews inside your loop ala

    for (int i = 0; i < 10; i++) {
        row = new TableRow(this);
        number = new TextView(this);
        number.setTag(i);
        number.setText(Integer.toString(i));
        row.addView(number);
    }
    
    0 讨论(0)
  • 2021-01-11 20:03

    Try this:

    View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
    
    for (int i = 0; i < 10; i++) {
        row = new TableRow(this);
        View layout_number = inflater.inflate(R.layout.inflate_number, null);
        TextView number = (TextView) layout_number.findViewById(R.id.Number);
        number.setTag(i);
        number.setText(Integer.toString(i));
        row.addView(number);
    }
    
    0 讨论(0)
  • 2021-01-11 20:08

    You're never actually adding any Views to your Activity. You're creating new TableRows and adding TextViews to them, but you're never adding the rows to anything. Assuming you have a TableLayout in R.layout.days_monday_inflate, you should first get a reference to that view (TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id)) and then add each row to that TableLayout:

    TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id);
    
    for(int i = 0; i < 10; i++) {
        row = new TableRow(this);
        View layout_number = inflater.inflate(R.layout.inflate_number, layout, false);
        TextView number = (TextView) layout_number.findViewById(R.id.Number);
        number.setTag(i);
        number.setText(Integer.toString(i));
        row.addView(number);
        layout.addView(row);
    }
    

    Although I would recommend setting up your layout fully in XML if at all possible, unless needed dynamically. Also, if you're only adding one TextView per row, you're better off just using a LinearLayout, I would suggest.

    0 讨论(0)
  • 2021-01-11 20:11

    In my case, setTag(i) didn't work, what worked was changing the id after inflating the layout but before adding it to the parent view like this:

    LayoutInflater inflater = LayoutInflater.from(this);
    View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
    View layout_number = inflater.inflate(R.layout.inflate_number, null);
    
    for (int i = 0; i < 10; i++) {
        row = new TableRow(this);
        number = (TextView) layout_number.findViewById(R.id.Number);
        number.setId(i); //THIS IS THE KEY STEP
        number.setText(Integer.toString(i));
        row.addView(number);
    }
    setContentView(mainlayout);
    
    0 讨论(0)
提交回复
热议问题