Java 如何在Android片段中使用ListView

Java 如何在Android片段中使用ListView,java,android,listview,android-fragments,android-listview,Java,Android,Listview,Android Fragments,Android Listview,丢失了一些源代码,我忘记了我以前是如何工作的 我有一个Feed.java类,它加载一个自定义listview,如本文所示: 然后我有一个LazyAdapter.java类,如下所示: public class LazyAdapter extends BaseAdapter { private Activity activity; private ArrayList<HashMap<String, String>> data; private static Layout

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

我有一个Feed.java类,它加载一个自定义listview,如本文所示:

然后我有一个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=新LazyAdapter此,歌曲列表; 错误内容如下: adapter=新LazyAdapter此,歌曲列表

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

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());
我怎样才能让它与我的片段提要.java一起工作


谢谢

它需要一个活动上下文,您可以很容易地获得它

换线就行了

adapter=new LazyAdapter(this, songsList);


在Feed.java中,检查您是否获得了xml。放个日志看看。你肯定修正了我的问题。我现在正在处理另一个无关的问题。必须是XML中的某种东西。没有在提要中显示任何内容。
adapter=new LazyAdapter(this, songsList);
adapter=new LazyAdapter(getActivity(), songsList);