我觉得这个问题已经被问了一百万次,回答了一百万次。我已经阅读并查看了十几个答案,但我仍然无法让它发挥作用。这对我来说似乎很简单,但我已经花了很长时间在它上面,无论我做什么,我都只看到我的数组适配器的第一项。我希望我遗漏了一些非常简单的东西,但我发现的大多数示例都表明这是这样做的,所以我很困惑。
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 );
当我运行我的程序时,ListView 似乎只包含“两个”。 .count() 是 2,就像我期望的那样(.clear() 之后的“两个”和“三个”),但 ListView 只显示“两个”。即使我在运行时添加一千个项目,它也只会显示“两个”。如果我 .clear() 它,那么它会显示我 .add() 的第一件事,但我真的很想显示我添加的所有内容,而不仅仅是第一个。
这是一个非常短的 fragment ,这样毫无意义。在我的实际程序中,在发生各种事情后,我正在执行 .add("blah") 和 .post()s 下的 .notifyDataSetChanged();但我在那里也得到了相同的结果。
UPDATE1:我的布局(根据评论要求)如下。当 ListView 是 TextView 时,布局似乎工作正常。在我问它是否只是没有扩展到 ScrollView 或其他东西之后,我确实想知道。我应该首先发布它。
<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:根据下面的几个答案,我用数组列表尝试了这个,但没有任何区别。
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 );
我仍然只是在 Activity 中显示的列表中获得“一个”。
最佳答案
请移除 ScrollView。不允许在 ScrollView 中使用 ListView。因为ListView默认有滚动效果。
关于java - 将项目添加到 ArrayAdapter<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030425/