简体   繁体   English

我可以在ActionBarActivity中动态填充ListView吗?

[英]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.

相关问题 我如何使用arraylist填充listview - How can I populate listview using arraylist 如何使用自定义对象在 JavaFX 中填充 ListView? - How can I Populate a ListView in JavaFX using Custom Objects? 我可以创建一个ListView来填充Android中的实际表名吗? - Can I create a ListView that will populate the actual table names in Android? 我可以从自定义适配器的getView填充一个listview吗? - Can I populate a listview from getView of a custom adapter? 如何使用 Android 中 FirstActivity 的数据填充 SecondActivity 上的 ListView? - How can I populate a ListView on a SecondActivity with data from FirstActivity in Android? 使用位图ArrayList动态填充ListView - Using Bitmap ArrayList to dynamically populate ListView 动态使用HashMap填充listView中的列 - Dynamically use HashMap to populate columns in listView 如何动态从* .properties文件填充h:selectItems - How can I populate h:selectItems from *.properties files dynamically 我正在获取类型为ActionBarActivity错误的onListItemClick(ListView,View,int,long)方法未定义 - I am getting the The method onListItemClick(ListView, View, int, long) is undefined for the type ActionBarActivity Error 我可以在java代码中动态扩展列表视图的大小吗 - Can i extend the size of a listview dynamically in java code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM