[英]Can I dynamically populate a ListView in an ActionBarActivity?
I saw a number of examples that populated ListViews in a ListActivity. 我看到了许多在ListActivity中填充ListViews的示例。 I want both a dynamically populated ListView, but in an activity with an action bar.
我既需要动态填充的ListView,又要具有活动栏的活动。
So instead of (as in the examples I've seen): 因此,与其代替(如我所看到的示例):
public class EditHolidaysActivity extends ListActivity {
I have: 我有:
public class EditHolidaysActivity extends ActionBarActivity {
But the setListAdapter method cannot be resolved. 但是setListAdapter方法无法解析。 What method might I use to populate the ListView?
我可以使用哪种方法填充ListView? Here are snippets of the xml and java:
这是xml和java的片段:
...
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Anni"
android:id="@+id/chkbxAnniversary"
android:checked="false"
android:onClick="saveAnniPref"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
Java: Java的:
public class EditHolidaysActivity extends ActionBarActivity {
public static final String PREFS_NAME = MainActivity.PREFS_NAME;
private final static String TEXT_DATA_KEY = "textData";
private CommentsDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_holidays);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
Thank you! 谢谢!
Something like this: 像这样:
public class EditHolidaysActivity extends ActionBarActivity {
public static final String PREFS_NAME = MainActivity.PREFS_NAME;
private final static String TEXT_DATA_KEY = "textData";
private CommentsDataSource datasource;
private ListView lstView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_holidays);
lstView = (ListView)findViewById(R.id.list);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
lstView.setAdapter(adapter);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.