0

I have an empty LinearLayout, and I need to add a dynamic number of TextViews to it. However when I use the code below, only the first TextView is shown:

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

    String[] listofnumbers = new String[1000];
    for ( int i = 0 ; i < 1000 ; ++i )  {
        listofnumbers[i] = "null";
    }

    Context context = getBaseContext();

    String text = null;

    Uri uri = Uri.parse("content://sms");
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

    String[] columnNames = cursor.getColumnNames();
    LinearLayout lv = new LinearLayout(context);
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));

    boolean v = true;
    while (cursor.moveToNext()) 
    {
        String numberString = (cursor.getString( cursor.getColumnIndexOrThrow("address") ) ).replace(" ", "");

        int i = 0;
        boolean numberNotPresent = true;
        for (  ; listofnumbers[i] != "null" ; ++i ) {
            if ( numberString.equals(listofnumbers[i])  )   {
                numberNotPresent = false;
            }
        }
        if ( numberNotPresent == true ) {
            text = (CharSequence) "From: " + cursor.getString(cursor.getColumnIndexOrThrow("address")) + ": " + cursor.getString(cursor.getColumnIndexOrThrow("body"));
            listofnumbers[i] = numberString;
            TextView tv = new TextView(this);
            tv.setText(text);
            tv.setLayoutParams(textViewParams);
            lv.addView( tv );
        }
    }
    setContentView(lv);
}

Where have I gone wrong?

4
  • are you sure the condition if ( numberNotPresent == true ) is being met? maybe add some logging and see if you're actually getting into that code and trying to add another TextView Commented Mar 18, 2012 at 16:28
  • Remove the condition for once and check again, if you get the TextView this time, means there's a problem in your condition. And I'd go with @dldnh about logging too. Commented Mar 18, 2012 at 16:45
  • set the params for thr linear layout Commented Mar 18, 2012 at 16:50
  • The conditions were being met. I had forgotten to set the LinearLayout orientation, as @bytebiscuit had suggested Commented Mar 19, 2012 at 17:19

2 Answers 2

3

try to change these two lines:

LinearLayout lv = new LinearLayout(context);
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));

with these:

LinearLayout lv = new LinearLayout(context);
        lv.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

basically you need to set the orientation for the lv LinearLayout to vertical as the default one is horizontal.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure, but may be for each textView you set fill_parent param? The 1st textView shows on all your layout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.