Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add items to ArrayAdapter<String>

I feel like this question has been asked a million times, and answered a million times. I've read and reviewed a dozen answers and I still can't get this to work. It seems very simple to me, but I've spent a pretty long time on it and I'm only seeing the first item of my array-adapter no matter what I do. I expect I'm missing something really simple, but mostly the examples I'm finding show this is the way to do it, so I'm baffled.

    result  = (ListView) rootView.findViewById( R.id.resultesult );

    resultAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
    resultAdapter.add( "one" );
    resultAdapter.clear();
    resultAdapter.add( "two" );
    resultAdapter.add( "three" );

    result.setAdapter( resultAdapter );

When I run my program, the ListView seems only to contain "two." The .count() is 2, like I'd expect ("two" and "three" after the .clear()), but the ListView is only showing "two." Even if I add a thousand items at run time, it'll just show "two." If I .clear() it, then it shows the first thing I .add(), but I'd really like to show everything I add, not just the first one.

This is a really short snipped that makes no sense like this. In my actual program I'm doing .add("blah") and then .notifyDataSetChanged() under .post()s after various things happen; but I get the same results there too.

UPDATE1: My layout (as requested by comment) is as follows. The layout seemed to work fine when the ListView was a TextView instead. I did wonder after I asked if it just wasn't expanding into the ScrollView or something. I should have posted it in the first place.

<AutoCompleteTextView
    android:id="@+id/command"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:ems="10"
    android:hint="@string/commandHint"
    android:imeActionId="@+id/execCommand"
    android:imeActionLabel="@string/exec"
    android:imeOptions="actionSend"
    android:inputType="textAutoCorrect|textAutoComplete|textMultiLine|textUri"
    android:selectAllOnFocus="false"
    android:typeface="monospace" >

</AutoCompleteTextView>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/command"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:fadingEdge="horizontal|vertical"
    android:scrollbars="horizontal|vertical" >

    <ListView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</ScrollView>

UPDATE2: Per a couple of answers below, I tried this with an arraylist, and it didn't make any difference.

    result  = (ListView) rootView.findViewById( R.id.resultesult );

    ArrayList<String> doubtThisllMatter = new ArrayList<String>();
    doubtThisllMatter.add( "one" );
    doubtThisllMatter.add( "two" );
    doubtThisllMatter.add( "three" );

    resultAdapter = new ArrayAdapter<String>( this, 
          android.R.layout.simple_list_item_1,
          doubtThisllMatter );

    result.setAdapter( resultAdapter );

I still just get "one" in the list displayed in the activity.

like image 249
jettero Avatar asked Apr 18 '26 22:04

jettero


1 Answers

Please remove the ScrollView. Using ListView inside ScrollView is not allowed. Because ListView by default has Scrolling effect.

like image 183
Aniruddha Avatar answered Apr 20 '26 11:04

Aniruddha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!