我不得不将一个类从被归类为一个活动改为一个片段,我不知道现在该如何设置它,因为它现在说它无法解析findviewbyid上的方法。如果需要,可以提供任何额外的代码。谢谢你的帮助。
public class CustomizedListView 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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.main, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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)findViewById(R.id.game_list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(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) {
}
});
}
}
这就是findViewByID给我带来麻烦的原因,因为它无法解决方法:
list=(ListView)findViewById(R.id.game_list);
这就是导致我在ArrayList上出现问题的原因,显然是因为LazyAdapter中的LazyAdapter(android.app.activity,ArrayList>)无法应用于(com.example.app.customizedListView,ArrayList>)
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
MIN .XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e5e5e5" >
<ListView
android:id="@+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#e5e5e5"
android:dividerHeight="6dp"
android:background="#e5e5e5"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"/>
</LinearLayout>
最佳答案
错误出现在方法oncreate上:视图创建必须改为方法oncreate view。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Preparing user interface
final View v = inflater.inflate(R.layout.main, container, false);
final ListView = (ListView) v.findViewById(R.id.game_list);
// Do what you want...
return v;
}
…是因为LazyAdapter(android.app.activity,arrayList>)在
LazyAdapter无法应用于(com.example.app.customizedListView,
> >
你有你的问题的线索。我知道你必须改变你使用lazyadapter的方式。我建议您:检查我的代码是否用fragment解决了您的问题,然后再问一个关于arraylist/lazyadapter问题的问题。