Android-动态添加项目到scrollview

the*_*uls 2 java xml android scrollview

我已经花了好几个小时试图按照各种教程来动态地将项目添加到滚动视图中,但似乎没有任何工作......

想法只是将字符串添加到Checkbox旁边的TextView中,这是在一个单独的xml文件中完成的,我只是希望列表随着我不断添加它们而增长.

现在,只添加了一个String(最后一个替换了前一个),并且在尝试更改它的任何尝试中,我的应用程序停止工作.这是代码:

TableLayout addPlayersTableLayout;

TableRow tableRow1;

ScrollView addPlayersScrollView;

String[] players = new String[]{ "Blah", "Whatever", "Test", "Nipah!"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPlayersTableLayout = (TableLayout)findViewById(R.id.TableLayout1);

    tableRow1 =(TableRow)findViewById(R.id.tableRow1);

    addPlayersScrollView = (ScrollView)findViewById(R.id.playersScrollView);

    insertPlayerInScrollView();     
}
Run Code Online (Sandbox Code Playgroud)

以下是应该添加项目的函数的实现:(add_player_row.xml是文本视图和应该是项目的复选框的文件)

private void insertPlayerInScrollView() {
    LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View newPlayerRow = inflator.inflate(R.layout.add_player_row, null);

    TextView newPlayerTextView = (TextView) newPlayerRow.findViewById(R.id.addPlayerTextView);

    CheckBox playerCheckBox = (CheckBox)findViewById(R.id.playerCheckBox);
    newPlayerTextView.setText(players[1]);
    addPlayersTableLayout.addView(newPlayerRow,0);

}
Run Code Online (Sandbox Code Playgroud)

最后一行代码是它工作的唯一方式,即使在大多数论坛和教程中,人们会指示我使用addPlayersScrollView,但如果我这样做,应用程序崩溃.所以...任何想法?非常感谢你!

kid*_*uxa 6

我面临着与你相似的事情.我有这样的布局:

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent">


            <TableLayout
                android:id="@+id/tableLayoutList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" /> 

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

我的行定义如下(mRowLayout.xml):

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayoutRow"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/checkBoxServEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</TableRow>
Run Code Online (Sandbox Code Playgroud)

然后我使用以下代码来扩充我的行:

private void fillTable(View v, Cursor c) {

TableLayout ll = (TableLayout) v.findViewById(R.id.tableLayoutList);

View mTableRow = null;
int i = 0;
while(!c.isAfterLast()){
    i++;
    mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mRowLayout, null);

     CheckBox cb = (CheckBox)mTableRow.findViewById(R.id.checkBoxServEmail);
     cb.setText( c.getString(c.getColumnIndex(Empleado.EMAIL)));


     mTableRow.setTag(i);

    //add TableRows to TableLayout
    ll.addView(mTableRow);

    c.moveToNext();
}
}
Run Code Online (Sandbox Code Playgroud)

我在这里尝试做的是动态地膨胀我的行.光标包含我想从数据库中显示的项目.我不确定这是否可以解决您的问题.让我知道.