如何在Android Fragment中使用ListView

时间:2015-02-01 03:29:03

标签: java android listview android-fragments android-listview

丢失了一些源代码,我忘记了以前的工作原理。

我有一个Feed.java类,可以加载自定义列表视图,如本文所示: 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类:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

它需要一个活动环境,你可以很容易地得到它。

只需更改行

即可
adapter=new LazyAdapter(this, songsList);

adapter=new LazyAdapter(getActivity(), songsList);
Feed.java

中的