簡體   English   中英

我可以在ActionBarActivity中動態填充ListView嗎?

[英]Can I dynamically populate a ListView in an ActionBarActivity?

我看到了許多在ListActivity中填充ListViews的示例。 我既需要動態填充的ListView,又要具有活動欄的活動。

因此,與其代替(如我所看到的示例):

public class EditHolidaysActivity extends ListActivity {

我有:

public class EditHolidaysActivity extends ActionBarActivity {

但是setListAdapter方法無法解析。 我可以使用哪種方法填充ListView? 這是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的:

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);
    }

謝謝!

像這樣:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM