我正在尝试在listview中添加一个加载图标作为footerView。 但是当我滚动并将新数据附加到listview时 我得到ClassCastException,我的应用程序已关闭
我在LoadMoreXML类执行时得到异常
任何人都可以帮助我吗?
这是我的代码
主要片段
public class LayoutActivity extends Fragment implements OnScrollListener{
ListView listview;
ListItemAdapter theAdapter;
ListItemAdapter myExistingAdapter = null;
String URL = "http://localhost/api/question/get_newest_except/0/0/5";
ProgressDialog pDialog;
NodeList nodelist;
ArrayList<ListItemObject> data;
Integer counter=0;
public LayoutActivity() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.layout_main, container,false);
listview = (ListView) rootview.findViewById(R.id.list01);
ProgressBar spinner = new ProgressBar(this.getActivity());
listview.addFooterView(spinner);
new DownloadXML().execute(URL);
listview.setOnScrollListener(this);
return rootview;
}
public class DownloadXML extends AsyncTask<String, Void, Void>{
public DownloadXML() {
super();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
data = new ArrayList<ListItemObject>();
ListItemObject itemData;
try{
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
itemData = new ListItemObject();
itemData.setId(getNode("pb__question__id",eElement));
itemData.setOwner(getNode("pb__question__consumer__id",eElement));
if(!getNode("pb__question__consumer__id",eElement).equalsIgnoreCase("0")){
itemData.setName(getNode("pb__question__consumer__name",eElement));
itemData.setJob(getNode("pb__question__consumer__occupation", eElement));
itemData.setProfilePic(getNode("pb__question__consumer__pp",eElement));
}
itemData.setStatus(getNode("pb__question__title",eElement));
itemData.setExtras(getNode("pb__question__topic__name", eElement));
if(!getNode("att__pict",eElement).isEmpty()){
itemData.setImage(getNode("att__pict", eElement));
}
if(getNode("pb__question__type", eElement).equalsIgnoreCase("1")){
itemData.setOpini(getNode("pb__question__total__opini", eElement));
}else if(getNode("pb__question__type", eElement).equalsIgnoreCase("2") || getNode("pb__question__type", eElement).equalsIgnoreCase("3")){
itemData.setOpini(getNode("pb__question__total__polling", eElement));
}else if(getNode("pb__question__type", eElement).equalsIgnoreCase("4")){
itemData.setOpini(getNode("pb__question__total__rating", eElement));
}
itemData.setTipe(getNode("pb__question__type", eElement));
itemData.setIkuti(getNode("pb__question__total__follow", eElement));
itemData.setSebarkan(getNode("pb__question__total__share", eElement));
data.add(itemData);
}
}
theAdapter = new ListItemAdapter(LayoutActivity.this.getActivity(),data);
listview.setAdapter(theAdapter);
}catch(Exception e){
Toast.makeText(getActivity(), "Koneksi dengan server gagal" , Toast.LENGTH_SHORT).show();
}
}
@Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
nodelist = doc.getElementsByTagName("pb__question");
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
}
public class LoadMoreXML extends AsyncTask<String, Void, Void>{
ArrayList<ListItemObject> data;
public LoadMoreXML() {
super();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
data = new ArrayList<ListItemObject>();
ListItemObject itemData;
for (int temp = 0; temp < nodelist.getLength(); temp++) {
Node nNode = nodelist.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
itemData = new ListItemObject();
itemData.setId(getNode("pb__question__id",eElement));
itemData.setOwner(getNode("pb__question__consumer__id",eElement));
if(!getNode("pb__question__consumer__id",eElement).equalsIgnoreCase("0")){
itemData.setName(getNode("pb__question__consumer__name",eElement));
itemData.setJob(getNode("pb__question__consumer__occupation", eElement));
itemData.setProfilePic(getNode("pb__question__consumer__pp",eElement));
}
itemData.setStatus(getNode("pb__question__title",eElement));
itemData.setExtras(getNode("pb__question__topic__name", eElement));
if(!getNode("att__pict",eElement).isEmpty()){
itemData.setImage(getNode("att__pict", eElement));
}
if(getNode("pb__question__type", eElement).equalsIgnoreCase("1")){
itemData.setOpini(getNode("pb__question__total__opini", eElement));
}else if(getNode("pb__question__type", eElement).equalsIgnoreCase("2") || getNode("pb__question__type", eElement).equalsIgnoreCase("3")){
itemData.setOpini(getNode("pb__question__total__polling", eElement));
}else if(getNode("pb__question__type", eElement).equalsIgnoreCase("4")){
itemData.setOpini(getNode("pb__question__total__rating", eElement));
}
itemData.setTipe(getNode("pb__question__type", eElement));
itemData.setIkuti(getNode("pb__question__total__follow", eElement));
itemData.setSebarkan(getNode("pb__question__total__share", eElement));
data.add(itemData);
}
}
myExistingAdapter = (ListItemAdapter) listview.getAdapter();
myExistingAdapter.addItems(data);
}
@Override
protected Void doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
nodelist = doc.getElementsByTagName("pb__question");
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
}
private static String getNode(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
String result = "";
if(nValue!=null){
result = nValue.getNodeValue();
}
return result;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int threshold = 2;
int count = listview.getCount();
if(scrollState == SCROLL_STATE_IDLE){
if(listview.getLastVisiblePosition() >= count - threshold){
counter+=5;
new LoadMoreXML().execute("http://localhost/api/question/get_newest_except/0/"+counter+"/5");
}
}
}
}
这是错误日志
谢谢
答案 0 :(得分:1)
当你调用listview.addFooterView时,Android正在为你创建一个支持页脚的适配器。然后,当您调用setAdapter时,Android不会替换它创建的适配器,但它仍会保留ListItemAdapter并将大多数适配器请求委托给该对象。然后,当您将ListView的适配器强制转换为类时,会崩溃,因为您的适配器实际上位于支持页脚的包装器适配器中。这有意义吗?
您需要做的是在onCreateView中创建ListItemAdapter,并立即将其附加到ListView。请注意,只要您调用addFooterView,ListView的适配器实际上是一个不同的适配器,但您的适配器仍在新的适配器中。
public class LayoutActivity extends Fragment implements OnScrollListener{
ListItemAdapter theAdapter; // don't need myExistingAdapter any more
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.layout_main, container,false);
listview = (ListView) rootview.findViewById(R.id.list01);
// Create your adapter with no initial data
theAdapter = new ListViewAdapter(LayoutActivity.this.getActivity(), null);
listview.setAdapter(theAdapter);
现在完成你的任务,只需直接引用适配器,然后添加项目:
public class DownloadXML extends AsyncTask<String, Void, Void>{
@Override
protected void onPostExecute(Void result) {
...
theAdapter.addItems(data);
public class LoadMoreXML extends AsyncTask<String, Void, Void>{
@Override
protected void onPostExecute(Void result) {
...
theAdapter.addItems(data);