【问题标题】:Android: Populate ListView in a fragmentAndroid:在片段中填充 ListView
【发布时间】:2012-09-02 18:11:10
【问题描述】:

我有一个填充 ListView 的活动,它正在工作。因为我使用标签 ViewPager,所以我必须使用片段而不是活动。所以有人能告诉我我需要改变什么来从我的活动中制作一个片段(FragmentActivity 不能在标签中使用)。

package com.basnijkamp.safanagendatest;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListSample extends Activity {  

    public final static String ITEM_TITLE = "title";  
    public final static String ITEM_CAPTION = "caption";  

    public Map<String,?> createItem(String title, String caption) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_TITLE, title);  
        item.put(ITEM_CAPTION, caption);  
        return item;  
    }  

    @Override  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  

        List<Map<String,?>> september2012 = new LinkedList<Map<String,?>>();  
        september2012.add(createItem("Electra Mining Africa 2012", "Johannesberg South Africa"));  
        september2012.add(createItem("MSV", "Brno Czech Republic"));  
        september2012.add(createItem("IMTS", "Chicago USA"));
        september2012.add(createItem("AMB", "Stuttgart Germany"));  
        september2012.add(createItem("Den Teknise Messe", "Lillestrom Norway"));  
        september2012.add(createItem("ITM", "Plovdiv Bulgaria"));  

        List<Map<String,?>> oktober2012 = new LinkedList<Map<String,?>>();  
        oktober2012.add(createItem("BIMU", "Milan Italy"));  
        oktober2012.add(createItem("Matek", "Istanbul"));
        oktober2012.add(createItem("TIB", "Bucharest Romania"));
        oktober2012.add(createItem("Vienna Tec", "Vienna Austria"));
        oktober2012.add(createItem("Euro Blech 2012", "Hanover Germany"));
        oktober2012.add(createItem("Technica Massan", "Stockholm Sweden"));

        List<Map<String,?>> november2012 = new LinkedList<Map<String,?>>();  
        november2012.add(createItem("JIMTOF", "Tokyo Japan"));
        november2012.add(createItem("Metavak", "Gorinchem Netherlands"));
        november2012.add(createItem("FABTECH", "Las Vegas USA"));
        november2012.add(createItem("Prodex", "Basel Switzerland"));
        november2012.add(createItem("EMAF 2012", "Porto Portugal"));
        november2012.add(createItem("Manufact Indonesia", "Jakarta Indonesia"));

        // create our list and custom adapter  
        SeparatedListAdapter adapter = new SeparatedListAdapter(this);  
        adapter.addSection("September 2012", new SimpleAdapter(this, september2012, R.layout.list_complex,   
            new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
        adapter.addSection("Oktober 2012", new SimpleAdapter(this, oktober2012, R.layout.list_complex,   
            new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
        adapter.addSection("November 2012", new SimpleAdapter(this, november2012, R.layout.list_complex,   
                new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));

        ListView list = new ListView(this);  
        list.setAdapter(adapter);  
        this.setContentView(list);  

    }  

}  

提前致谢

【问题讨论】:

    标签: java android listview android-activity fragment


    【解决方案1】:

    我建议您扩展 ListFragment - http://developer.android.com/reference/android/app/ListFragment.html

    所以从这里:

            ListView list = new ListView(this); 
            list.setAdapter(adapter); 
            this.setContentView(list); 
    

    你应该这样做:

            setListAdapter (adapter); 
    

    来自

    @Override 
    public void onCreate(Bundle icicle) { 
            super.onCreate(icicle);
    

    ->

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    

    来自ListSample extends Activity -> ListSample extends ListFragment

    如果需要更多细节,请告诉我,祝你好运

    【讨论】:

    • 关于“public void onActivityCreated(savedInstanceState)”我需要删除覆盖吗?并且“savedInstanceState”无法解析为类型
    • 现在,SeparatedListAdapter 会导致问题“构造函数SeparatedListAdapter(AgendaTab) 未定义”这是separatedlistadapter(password:android)plaatscode.be/141890 的代码,我想我快解决了
    • 这很简单——适配器需要构造函数中的上下文。 Activity 扩展了 Context,但 Fragment 没有,所以你必须使用 new SeparatedListAdapter(getActivity()) 而不是 new SeparatedListAdapter(this)
    • 是的!最后一件事“构造函数 SimpleAdapter(AgendaTab, List>, int, String[], int[]) 是未定义的”你能帮我做最后一件事吗:)
    • 不也是这样吗?只需将第一个“this”替换为 getActivity()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多