show data in table view in android

前端 未结 6 715
自闭症患者
自闭症患者 2020-12-01 15:21

I want to get data from database in my android table view.

Should I use loop? Is static good for this?

6条回答
  •  隐瞒了意图╮
    2020-12-01 15:25

    We could imagine a custom component for android : the TableView.

    Its code would be something like :

    public class TableView extends TableLayout {
    
        public TableView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TableView(Context context) {
            super(context);
        }
    
        public void setAdapter(TableAdapter adapter) {
            removeAllViews();
    
            for (int row = 0; row < adapter.getRowCount(); row++) {
                TableRow tableRow = new TableRow(getContext());
                TableLayout.LayoutParams params = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                tableRow.setLayoutParams(params);
                addView(tableRow);
                for (int column = 0; column < adapter.getColumnCount(); column++) {
                    View cell = adapter.getView(row, column);
                    tableRow.addView(cell);
                    TableRow.LayoutParams cellParams = (android.widget.TableRow.LayoutParams) cell.getLayoutParams();
                    cellParams.weight = adapter.getColumnWeight(column);
                                        cellParams.width = 0;
                }
            }
        }
    }
    

    And it would use an adapter like this :

    public interface TableAdapter {
        int getRowCount();
    
        int getColumnWeight(int column);
    
        int getColumnCount();
    
        T getItem(int row, int column);
    
        View getView(int row, int column);
    }
    

提交回复
热议问题