我正在尝试使用远程数据填充列表视图,我将这部分应用程序基于教程代码。在教程中,他们只使用类,但在我的使用中我想使用片段,因为我还在将滑动导航栏集成到应用程序中。这很可能是我的一个愚蠢的错误,但是我可以使用一些方向,谢谢。
这是我得到的错误:
The constructor LazyAdapter(HomeFragment, ArrayList<HashMap<String,String>>) is undefined
这发生在HomeFragment的以下行:
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(HomeFragment.this, songsList);
list.setAdapter(adapter);
HomeFragment代码:
public class HomeFragment extends Fragment {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// 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 = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.blog, 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
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);
}
list=(ListView)getView().findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(HomeFragment.this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
ListAdapters代码:
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.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(HomeFragment.KEY_TITLE));
artist.setText(song.get(HomeFragment.KEY_ARTIST));
duration.setText(song.get(HomeFragment.KEY_DURATION));
imageLoader.DisplayImage(song.get(HomeFragment.KEY_THUMB_URL), thumb_image);
return vi;
}
}
答案 0 :(得分:0)
更改
adapter=new LazyAdapter(HomeFragment.this, songsList);
到
adapter=new LazyAdapter(getActivity(), songsList);
public final Activity getActivity ()
返回此片段当前与之关联的活动。
同时更改
list=(ListView)getView().findViewById(R.id.list);
到
list=(ListView)rootView.findViewById(R.id.list);
还要确保您在bakgroud线程上进行与网络相关的操作。
String xml = parser.getXmlFromUrl(URL);
编辑:onCreateView
});
return rootView;
}