Reputation: 7460
I have been for hours trying to follow various tutorials in order to add items dynamically into a scrollview but nothing seems to be working...
The idea is just to add Strings into a TextView next to a Checkbox, which were done in a separate xml file and I just wanted the list to grow as I keep adding them.
For now, only one String is being added (the last one substitutes the previous one), and in any attempt to try to change that, my app stops working. Here is the code as it stands:
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();
}
Here is the implementation of the function that is supposed to add the items: (add_player_row.xml is the file where the textview and checkbox that are supposed to be the items were made)
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);
}
The final line of code is the only way it works at all, even though on most forums and tutorials, people would indicate for me to use the addPlayersScrollView, but if I do so, the app crashes. So... Any ideas? Thank you very much!
Upvotes: 3
Views: 9792
Reputation: 3351
I'm facing something similar than you. I have a layout like this:
<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>
And have my row defined like this (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>
And then I use the following code to inflate my row:
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();
}
}
What I'm trying to do here is inflate my rows dynamically. The cursor have the items that I wanna show from data base. I'm not sure if this can solve your problem. Let me know.
Upvotes: 9