Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
If you post the logcat when it crashes we can try to help you figure out why it crashes –  codeMagic Jun 21 '13 at 19:32

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Hi there :) The bad news is I guess nobody else answered this topic. The good news is I eventually figured it out. I don't know about your case, but in my case the only thing I had to do was include the whole insertPlayerInScrollView() function in a loop. Not quite sure why, but I am very relieved it works. Perhaps you could try that on yours? Hope this helps! Best of luck for you –  theJuls Jun 27 '13 at 1:07
    
Good!, glad to hear that. My case works pretty well. My case works with cursors and yours with an array. Is almost the same. Note that I have an iteration as well, because I'm generating the rows one by one. Thats why you had to put your function in a loop. But in the future if you want to play with cursors and DB instead of arrays, you can have the reference of what I did ;). Good luck for you too. –  kiduxa Jun 27 '13 at 12:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.