SQLite类对象无法实例化,android?

时间:2014-06-16 04:57:34

标签: android sqlite

我为SQLITE创建了一个类,如下所示:

    public class DatbaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "store";

// Contacts table name
private static final String TABLE_DEALER = "dealer";
// Contacts Table Columns names
private static final String KEY_DEALER_ID = "dealer_id";
private static final String KEY_DEALER_NAME = "dealer_name";

public DatbaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_DEALER + "("
            + KEY_DEALER_ID + " INTEGER PRIMARY KEY," + KEY_DEALER_NAME
            + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEALER);

    // Create tables again
    onCreate(db);
}

public void addContact(Dealer d) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_DEALER_ID, d.getDealer_id());
    values.put(KEY_DEALER_NAME, d.getDealer_name());
    // Inserting Row
    db.insert(TABLE_DEALER, null, values);
    db.close(); // Closing database connection
}

我有一个带有getter和setter方法的经销商类,如下所示

    public class Dealer {
int dealer_id;
String dealer_name;
public Dealer()
{

}
public Dealer(int id,String name)
{
    this.dealer_id=id;
    this.dealer_name=name;
}
/**
 * @return the dealer_id
 */
public int getDealer_id() {
    return dealer_id;
}
/**
 * @param dealer_id the dealer_id to set
 */
public void setDealer_id(int dealer_id) {
    this.dealer_id = dealer_id;
}
/**
 * @return the dealer_name
 */
public String getDealer_name() {
    return dealer_name;
}
/**
 * @param dealer_name the dealer_name to set
 */
public void setDealer_name(String dealer_name) {
    this.dealer_name = dealer_name;
}
 }

所以我正在解析XML并希望将数据保存在表中。我已经使用pull解析器为XML解析创建了一个新类。

当我将我的SQLITE类对象声明为

    DatbaseHandler db = new DatbaseHandler(this);

我收到此错误

    The constructor DatbaseHandler(SyncXmlParser) is undefined
    //SyncXmlParser is the XML parsing class 

SyncXMLParser类

    public class SyncXmlParser {
private XmlPullParserFactory xmlFactoryObject;
private XmlPullParser myparser;
String dataListName, distId, distName, townId, townName, beatName, beatId,
        dealName, dealId;
DatbaseHandler db = new DatbaseHandler(this);
public SyncXmlParser() {
}

public void createXML(String apiResponse) {
    try {
        xmlFactoryObject = XmlPullParserFactory.newInstance();
        xmlFactoryObject.setNamespaceAware(true);
        myparser = xmlFactoryObject.newPullParser();
        myparser.setInput(new StringReader(apiResponse.substring(
                apiResponse.lastIndexOf("<?xml"),
                apiResponse.indexOf("/DataList") + 10)));
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
    parseXMLAndStoreIt(myparser);
}

public void parseXMLAndStoreIt(XmlPullParser myParser) {
    int event;
    try {
        event = myParser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            if (event == XmlPullParser.START_DOCUMENT) {
            } else if (event == XmlPullParser.END_DOCUMENT) {
            } else if (event == XmlPullParser.START_TAG) {
                if (myParser.getName().equals("DataList")) {

                } else if (myParser.getName().equals("Distributor")) {
                } else if (myParser.getName().equals("Town")) {
                } else if (myParser.getName().equals("Beat")) {
                } else if (myParser.getName().equals("Dealer")) {
                }
            } else if (event == XmlPullParser.END_TAG) {

            } else if (event == XmlPullParser.TEXT) {
            }
            event = myParser.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

2 个答案:

答案 0 :(得分:1)

构造函数将Context作为其参数。 SyncXmlParser不是一个Context(我不建议您通过扩展Context来使其成为一个)。您需要提供某种Context,例如Activity或Application context。

你究竟选择这样做取决于应用程序的结构,但有一个建议可能是使用像Dagger这样的依赖注入框架。

答案 1 :(得分:1)

如果您的SyncXmlParser类是一个java类而不是android活动类,那么它会给出错误,因为DatbaseHandler类需要上下文,但是您在没有上下文的情况下调用类。

public class SampleActivity extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        new SyncXmlParser(SampleActivity.this);
     }
}

public class SyncXmlParser()
{
       private Context context;
        SyncXmlParser(Context context)
          {
this.context=context;
           }

DatbaseHandler db = new DatbaseHandler(context);
}