Java XMLpullparser未显示listview项

Java XMLpullparser未显示listview项,java,android,xml,sqlite,xmlpullparser,Java,Android,Xml,Sqlite,Xmlpullparser,我正在开发一个酒店应用程序,其中每个酒店都会显示在城市的列表视图上,通过xmlpullparser调用每个酒店,但我的db返回0行,我不知道为什么 我想这个班有问题,请检查一下 package com.hamza.khihotel.db; import java.util.ArrayList; import java.util.List; import com.hamza.khihotel.model.Hotel; import android.content.ContentValues;

我正在开发一个酒店应用程序,其中每个酒店都会显示在城市的列表视图上,通过xmlpullparser调用每个酒店,但我的db返回0行,我不知道为什么 我想这个班有问题,请检查一下

package com.hamza.khihotel.db;

import java.util.ArrayList;
import java.util.List;

import com.hamza.khihotel.model.Hotel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class HotelDataSource {

private static final String LOGTAG = "khiHotel";

HoteldbOpenHelper dbHelper;
SQLiteDatabase database;

private static final String allAttributes[] = { HoteldbOpenHelper.HOTEL_ID,
        HoteldbOpenHelper.HOTEL_TITLE, HoteldbOpenHelper.HOTEL_DESC,
        HoteldbOpenHelper.HOTEL_IMAGE, HoteldbOpenHelper.HOTEL_PRICE,
        HoteldbOpenHelper.HOTEL_LINK }; 

public HotelDataSource(Context context) {
    dbHelper = new HoteldbOpenHelper(context);

}

public void open() {
    database = dbHelper.getWritableDatabase();

}

public void close() {
    dbHelper.close();

}

public Hotel create(Hotel hotel) {
    ContentValues keyValues = new ContentValues();
    keyValues.put(HoteldbOpenHelper.HOTEL_TITLE, hotel.getTitle());
    keyValues.put(HoteldbOpenHelper.HOTEL_DESC, hotel.getDescription());
    keyValues.put(HoteldbOpenHelper.HOTEL_IMAGE, hotel.getImage());
    keyValues.put(HoteldbOpenHelper.HOTEL_PRICE, hotel.getPrice());
    keyValues.put(HoteldbOpenHelper.HOTEL_LINK, hotel.getLink());

    long insertId = database.insert(HoteldbOpenHelper.TABLE_NAME, null,
            keyValues);
    hotel.setId(insertId);

    return hotel;

}

public List<Hotel> allData() {
    Cursor cursor = database.query(HoteldbOpenHelper.TABLE_NAME,
            allAttributes, null, null, null, null, null);

    //**check this logtag this shows i am not getting the rows**  

    Log.i(LOGTAG, "Returned" + cursor.getCount() + "rows");
    List<Hotel> hotels = cursorToList(cursor);

    return hotels;
}
/*
public List<Hotel> findFiltered(String selection, String orderBy) {
    Cursor cursor = database.query(HoteldbOpenHelper.TABLE_NAME,
            allAttributes, selection, null, null, null, orderBy);

    Log.i(LOGTAG, "Returned" + cursor.getCount() + "rows");
    List<Hotel> hotels = cursorToList(cursor);

    return hotels;

}
*/
private List<Hotel> cursorToList(Cursor cursor) {
    List<Hotel> hotels = new ArrayList<Hotel>();

    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            Hotel hotel = new Hotel();
            hotel.setId(cursor.getLong(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_ID)));
            hotel.setTitle(cursor.getString(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_TITLE)));
            hotel.setDescription(cursor.getString(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_DESC)));
            hotel.setImage(cursor.getString(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_IMAGE)));
            hotel.setPrice(cursor.getDouble(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_PRICE)));
            hotel.setLink(cursor.getString(cursor
                    .getColumnIndex(HoteldbOpenHelper.HOTEL_LINK)));
            hotels.add(hotel);
        }
    }
    return hotels;

}
}

xmlpullparser类

package com.hamza.khihotel.pullparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;

import com.hamza.khihotel.model.Hotel;

public class HotelPullParser {

@SuppressWarnings("unused")
private static final String LOGTAG = "KhiHotel";

private static final String HOTEL_TITLE = "title";
private static final String HOTEL_DESC = "description";
private static final String HOTEL_ID = "id";
private static final String HOTEL_PRICE = "price";
private static final String HOTEL_LINK = "website link";
private static final String HOTEL_IMAGE = "image";

private Hotel currentHotels = null;
private String currentTag = null;
List<Hotel> hotels = new ArrayList<Hotel>();

public List<Hotel> parseXML(Context context) {

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();

        InputStream stream = context.getResources().openRawResource(
                com.hamza.khihotel.R.raw.hotels);
        parser.setInput(stream, null);

        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {
                handleText(parser.getName());

            } else if (eventType == XmlPullParser.END_TAG) {
                currentTag = null;

            } else if (eventType == XmlPullParser.TEXT) {
                handleText(parser.getText());

            }
            eventType = parser.next();

        }

    } catch (XmlPullParserException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return hotels;

}

private void handleText(String text) {
    String xmlText = text;

    if (currentHotels != null && currentTag != null) {
        if (currentTag.equals(HOTEL_ID)) {
            int id = Integer.parseInt(xmlText);
            currentHotels.setId(id);

        } else if (currentTag.equals(HOTEL_TITLE)) {
            currentHotels.setTitle(xmlText);

        } else if (currentTag.equals(HOTEL_DESC)) {
            currentHotels.setDescription(xmlText);

        } else if (currentTag.equals(HOTEL_IMAGE)) {
            currentHotels.setImage(xmlText);

        } else if (currentTag.equals(HOTEL_LINK)) {
            currentHotels.setLink(xmlText);

        } else if (currentTag.equals(HOTEL_PRICE)) {
            double price = Double.parseDouble(xmlText);
            currentHotels.setPrice(price);

        }

    }

}

public void handleStartTag(String name) {
    if (name.equals("tour")) {
        currentHotels = new Hotel();
        hotels.add(currentHotels);

    } else {
        currentTag = name;

    }

}
}

主要活动类

package com.hamza.khihotel;

import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.hamza.khihotel.db.HotelDataSource;
import com.hamza.khihotel.model.Hotel;
import com.hamza.khihotel.pullparser.HotelPullParser;

public class MainActivity extends ListActivity {

HotelDataSource dataSource;
List<Hotel> listItem;
HotelPullParser xmlData;

@SuppressWarnings("unused")
private static final String VIEWIMAGES = "images";
@SuppressWarnings("unused")
private static final String LOGTAG = "khiHotel";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataSource = new HotelDataSource(this);
    dataSource.open();

    listItem = dataSource.allData();
    if (listItem.size() == 0) {
        createData();
        listItem = dataSource.allData();

    }
    refreshDispaly();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

private void createData() {

    HotelPullParser xmlData = new HotelPullParser();
    listItem = xmlData.parseXML(this);

    for (Hotel hotel : listItem) {
        dataSource.create(hotel);
    }
}

private void refreshDispaly() {
    ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this,
            android.R.layout.simple_list_item_1, listItem);
    setListAdapter(adapter);
    // ArrayAdapter<Hotel> adapter = new HotelListAdapter(this, listItem);
    // setListAdapter(adapter);

}

@Override
protected void onPause() {
    super.onPause();
    dataSource.close();
}

@Override
protected void onResume() {
    super.onResume();
    dataSource.open();
}
}

我的应用程序正在运行,但屏幕上没有显示任何内容。 日志标记输出为:

10-14 12:39:09.373:I/khiHotel488:Returned0行
10-14 12:39:09.563:I/khiHotel488:Returned0rows

请发布您的logcat错误。正如我所说的,应用程序正在运行,logcat中没有错误。请确保您的数据库包含您插入的数据。数据是通过xml文件插入的。您在数据库中检查了它吗?
package com.hamza.khihotel.model;

import java.text.NumberFormat;


public class Hotel {

private long id;
private String title;
private String desc;
private String image;
private String link;
private double price;


public long getId(){
    return id;

    }
public void setId(long id){
    this.id = id;

}
public String getTitle(){
    return title;

}

public void setTitle(String title){
    this.title = title;

}

public String getDescription(){
    return desc;

}

public void setDescription(String desc){
    this.desc = desc;
}

public String getImage(){
    return this.image;

}

public void setImage(String image){
    this.image = image;

}

public double getPrice(){
    return price;

}

public void setPrice(double price){
    this.price = price;

}

public String getLink(){
    return link;

}

public void setLink(String link){
    this.link = link;

}


public String toString(){
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    return title + "\n(" + nf.format(price) + ")";

}
package com.hamza.khihotel;

import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.hamza.khihotel.db.HotelDataSource;
import com.hamza.khihotel.model.Hotel;
import com.hamza.khihotel.pullparser.HotelPullParser;

public class MainActivity extends ListActivity {

HotelDataSource dataSource;
List<Hotel> listItem;
HotelPullParser xmlData;

@SuppressWarnings("unused")
private static final String VIEWIMAGES = "images";
@SuppressWarnings("unused")
private static final String LOGTAG = "khiHotel";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataSource = new HotelDataSource(this);
    dataSource.open();

    listItem = dataSource.allData();
    if (listItem.size() == 0) {
        createData();
        listItem = dataSource.allData();

    }
    refreshDispaly();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

private void createData() {

    HotelPullParser xmlData = new HotelPullParser();
    listItem = xmlData.parseXML(this);

    for (Hotel hotel : listItem) {
        dataSource.create(hotel);
    }
}

private void refreshDispaly() {
    ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this,
            android.R.layout.simple_list_item_1, listItem);
    setListAdapter(adapter);
    // ArrayAdapter<Hotel> adapter = new HotelListAdapter(this, listItem);
    // setListAdapter(adapter);

}

@Override
protected void onPause() {
    super.onPause();
    dataSource.close();
}

@Override
protected void onResume() {
    super.onResume();
    dataSource.open();
}