Program Club

테이블 레이아웃에 동적으로 행을 추가하는 방법

proclub 2021. 1. 6. 19:59
반응형

테이블 레이아웃에 동적으로 행을 추가하는 방법


sqllite에 특정 데이터가 있으며 저장 버튼을 클릭 할 때마다 업데이트되며 업데이트 된 데이터에 대해 더 많은 행을 추가하기 위해 테이블 ​​레이아웃에 데이터를 표시하고 싶습니다.

특정 코드가 있지만 이전 데이터를 대체하는 업데이트 된 데이터 만 표시되며 데이터가 업데이트되면 행을 더 추가하고 싶습니다.

나는 이것이 테이블 레이아웃에 하나의 행을 추가하는 것임을 알고 있지만 어떻게 더 많은 행을 추가 할 수 있습니까?

TableLayout tl=(TableLayout)findViewById(R.id.maintable);    
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(data);
//textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
tl.addView(tr1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

테이블 레이아웃에 행을 추가 한 방식으로 객체에 여러 TableRow인스턴스를 추가 할 수 있습니다.tableLayout

tl.addView(row1);
tl.addView(row2);

기타...


다음은 XML 스타일을 보존하고 a 사용 문제를 피할 수 있도록 약간의 시행 착오를 거쳐 알아 낸 기술입니다 <merge/>(예 : inflate ()는 루트에 연결하기 위해 병합이 필요하고 루트 노드를 반환합니다). 어떤 런타임 new TableRow()에요하거나 new TextView()요구 s의.

암호

참고 : 다음 CheckBalanceActivity은 몇 가지 샘플 Activity클래스입니다.

TableLayout table = (TableLayout)CheckBalanceActivity.this.findViewById(R.id.attrib_table);
for(ResourceBalance b : xmlDoc.balance_info)
{
    // Inflate your row "template" and fill out the fields.
    TableRow row = (TableRow)LayoutInflater.from(CheckBalanceActivity.this).inflate(R.layout.attrib_row, null);
    ((TextView)row.findViewById(R.id.attrib_name)).setText(b.NAME);
    ((TextView)row.findViewById(R.id.attrib_value)).setText(b.VALUE);
    table.addView(row);
}
table.requestLayout();     // Not sure if this is needed.

attrib_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow style="@style/PlanAttribute"  xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        style="@style/PlanAttributeText"
        android:id="@+id/attrib_name"
        android:textStyle="bold"/>
    <TextView
        style="@style/PlanAttributeText"
        android:id="@+id/attrib_value"
        android:gravity="right"
        android:textStyle="normal"/>
</TableRow>

CursorAdapter (또는 SimpleCursorAdapter ) 와 함께 ListView사용하는 것이 더 나을 수 있습니다 .

이들은 sqlite 데이터베이스의 행을 표시하도록 만들어졌으며 최소한의 프로그래밍으로 새로 고칠 수 있습니다.

편집 - 샘플 코드를 포함하여 SimpleCursorAdapter 및 ListView와 관련된 튜토리얼 이 있습니다.


You are doing it right; every time you need to add a row, simply so new TableRow(), etc. It might be easier for you to inflate the new row from XML though.


Solution 1. Set your TableLayout tl as class member variable, only call TableLayout tl=(TableLayout)findViewById(R.id.maintable); when you initiate the class. When button clicked use tl directly, eg. tl.addView(row), don't call FindViewById anymore. So the next new row wouldn't replace the previous new row.

Solution 2. Everytime after button click save your updated data into an array, and then re-render your whole table layout by loop through the array.

ReferenceURL : https://stackoverflow.com/questions/5183968/how-to-add-rows-dynamically-into-table-layout

반응형