如何在 Android Fragment 中使用 ListView

How to use ListView in Android Fragment

丢失了一些源代码,我忘记了我以前是如何工作的。

我有一个 Feed.java class 加载自定义列表视图,如本文所示: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

public class Feed extends Fragment {

// All static variables
    static final String URL = "=========================";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "date";
    static final String KEY_DURATION = "time";
    static final String KEY_THUMB_URL = "thumb_url";

    ListView list;
    LazyAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //This layout contains your list view 
        View view = inflater.inflate(R.layout.feedview, container, false);

        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        org.w3c.dom.Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

            ListView listview =(ListView)view.findViewById(R.id.listView1);
            adapter=new LazyAdapter(this, songsList);
            listview.setAdapter(adapter);  

            return view;
}

然后我有一个 LazyAdapter.java class 像这样:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.date); // date
    TextView duration = (TextView)vi.findViewById(R.id.time); // time
    TextView address = (TextView)vi.findViewById(R.id.address); // address
    TextView notes = (TextView)vi.findViewById(R.id.notes); // notes

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(Feed.KEY_TITLE));
    artist.setText(song.get(Feed.KEY_ARTIST));
    duration.setText(song.get(Feed.KEY_DURATION));
    return vi;
}

}

在 Feed.java 行中我收到错误 "adapter=new LazyAdapter(this, songsList);" 错误内容如下: "adapter=new LazyAdapter(this, songsList);"

我相信这是因为 LazyAdapter 正在寻找 Activity,而不是这些行中看到的片段:

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());

我怎样才能让它与我的片段一起工作 Feed.java??

谢谢!

它需要一个 activity 上下文,你可以很容易地得到它。

换行就行了

adapter=new LazyAdapter(this, songsList);

adapter=new LazyAdapter(getActivity(), songsList);

Feed.java