Android Studio从另一个类为SQLiteOpenHelper定义参数时出错

Android Studio从另一个类为SQLiteOpenHelper定义参数时出错,android,android-database,Android,Android Database,当我创建一个新的DatabaseHandler时,我似乎无法获得正确的参数 我已经尝试过各种方法,当代码位于使用 DatabaseHandler db=新的DatabaseHandler(此) 当从另一个类调用参数时,我不知道必须为参数输入什么 代码如下 package com.example.micheal.scannerv1; import android.content.Context; import android.util.Log; import org.xmlpull.v1.Xm

当我创建一个新的DatabaseHandler时,我似乎无法获得正确的参数

我已经尝试过各种方法,当代码位于使用

DatabaseHandler db=新的DatabaseHandler(此)

当从另一个类调用参数时,我不知道必须为参数输入什么

代码如下

package com.example.micheal.scannerv1;

import android.content.Context;
import android.util.Log;

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

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by micheal on 19/10/2015.
 */



 public class ParseOrderList {
    private String result;
    private ArrayList<OrderList> orderLists;
    // Database Version
    private static final int DATABASE_VERSION = 1;
    Context mContext;
    DatabaseHandler db;

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

    ArrayList<String> items=new ArrayList<String>();

    public ParseOrderList(Context mContext){
        this.mContext=mContext;
        this.db = new DatabaseHandler(mContext);
    }


    public ParseOrderList(String result) {
        this.result = result;
        orderLists= new ArrayList<>();
    }

    public ArrayList<OrderList> getOrderLists() {
        return orderLists;
    }


    public boolean process(){
        boolean status = true;
        OrderList currentRecord = null;
        boolean inEntry = false;
        String textValue = "";

        // Inserting Contacts
        Log.d("Insert: ", "Inserting ..");
        db.addContact(new Contact("Ravi", "9100000000"));
        db.addContact(new Contact("Srinivas", "9199999999"));
        db.addContact(new Contact("Tommy", "9522222222"));
        db.addContact(new Contact("Karthik", "9533333333"));

        // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<Contact> contacts = db.getAllContacts();


        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(new StringReader(this.result));
            int eventType = xpp.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT){
                String tagName = xpp.getName();
                switch(eventType){
                    case XmlPullParser.START_TAG:
                        Log.d("Parse OrderList","Starting tag for " + tagName);
                        if(tagName.equalsIgnoreCase("item")){
                            inEntry = true;
                           currentRecord = new OrderList();

                        }
                        break;

                    case XmlPullParser.TEXT:
                        textValue = xpp.getText();
                        break;

                    case XmlPullParser.END_TAG:
                        Log.d("Parse OrderList","Ending tag for " + tagName);
                        if(inEntry){
                            if(tagName.equalsIgnoreCase("item")){
                                orderLists.add(currentRecord);
                                inEntry = false;
                                Log.d ("progress","this far");
                            } else if(tagName.equalsIgnoreCase("increment_id")){
                                currentRecord.setIncrement_id(textValue);

                            } else if(tagName.equalsIgnoreCase("customer_id")){
                                currentRecord.setCustomer_id(textValue);
                            } else if(tagName.equalsIgnoreCase("grand_total")){
                                currentRecord.setGrand_total(textValue);
                            }
                        }
                        break;

                    default:
                        //Nothing else to do
                }
                eventType = xpp.next();
            }

            Log.d ("progress","this far2");

        } catch (Exception e){
            status = false;
            e.printStackTrace();
        }
        for (OrderList ord : orderLists){
            Log.d ("Customer ", "is " + ord.getCustomer_id());
            Log.d ("Grand Total ", "is " + ord.getGrand_total());
            Log.d ("Increment ID ", "is " + ord.getIncrement_id());
        }
        return true;
    }



}
package com.example.micheal.scannerv1;
导入android.content.Context;
导入android.util.Log;
导入org.xmlpull.v1.XmlPullParser;
导入org.xmlpull.v1.XmlPullParserFactory;
导入java.io.StringReader;
导入java.util.ArrayList;
导入java.util.List;
/**
*由micheal于2015年10月19日创建。
*/
公共类ParseOrderList{
私有字符串结果;
私有ArrayList订单列表;
//数据库版本
私有静态最终int数据库_VERSION=1;
语境;
数据库处理程序数据库;
//数据库名称
私有静态最终字符串数据库\u NAME=“Magento”;
ArrayList items=新建ArrayList();
公共ParseOrderList(上下文mContext){
this.mContext=mContext;
this.db=新的DatabaseHandler(mContext);
}
公共ParseOrderList(字符串结果){
this.result=结果;
OrderList=新的ArrayList();
}
公共ArrayList GetOrderList(){
返回订单列表;
}
公共布尔过程(){
布尔状态=真;
OrderList currentRecord=null;
布尔值=假;
字符串textValue=“”;
//插入联系人
日志d(“插入:”,“插入..”);
db.addContact(新联系人(“Ravi”,“9100000000”));
db.addContact(新联系人(“Srinivas”、“919999999”);
db.addContact(新联系人(“Tommy”,“952222”));
db.addContact(新联系人(“Karthik”、“9533333”);
//读取所有联系人
日志d(“读取:”,“读取所有联系人…”);
List contacts=db.getAllContacts();
试一试{
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
setInput(新的StringReader(this.result));
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
字符串标记名=xpp.getName();
开关(事件类型){
case XmlPullParser.START_标记:
Log.d(“解析订单列表”,“开始标记“+标记名”);
if(标记名.equalsIgnoreCase(“项”)){
真实=真实;
currentRecord=新订单列表();
}
打破
case XmlPullParser.TEXT:
textValue=xpp.getText();
打破
case XmlPullParser.END_标记:
Log.d(“解析订单列表”,“结束“+标记名的标记”);
如有(内地){
if(标记名.equalsIgnoreCase(“项”)){
orderLists.add(currentRecord);
错误=错误;
日志d(“进展”,“目前为止”);
}else if(标记名.equalsIgnoreCase(“增量标识”)){
currentRecord.setIncrement_id(textValue);
}else if(标记名.equalsIgnoreCase(“客户id”)){
currentRecord.setCustomer_id(textValue);
}else if(标记名.equalsIgnoreCase(“总计”)){
currentRecord.setGrand_总计(文本值);
}
}
打破
违约:
//别无选择
}
eventType=xpp.next();
}
日志d(“进度”、“本far2”);
}捕获(例外e){
状态=假;
e、 printStackTrace();
}
对于(订单列表ord:OrderList){
Log.d(“客户”,“是”+ord.getCustomer_id());
Log.d(“总计”,“is”+ord.getGrand_Total());
Log.d(“增量ID”,“is”+ord.getIncrement_ID());
}
返回true;
}
}
数据库处理程序

    package com.example.micheal.scannerv1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

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

/**
 * Created by micheal on 25/10/2015.
 */

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

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

    // Contacts table name
    private static final String TABLE_CONTACTS = "orders";

    // Contacts Table Columns names
    private static final String KEY_INCREMENT_ID = "increment_id";
    private static final String KEY_CUSTOMER_ID = "customer_id";
    private static final String KEY_GRANDTOTAL = "grandTotal";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_INCREMENT_ID + " INTEGER PRIMARY KEY," + KEY_CUSTOMER_ID + " TEXT,"
                + KEY_GRANDTOTAL + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_CUSTOMER_ID, contact.getName()); // Contact Name
        values.put(KEY_GRANDTOTAL, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    Contact getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_INCREMENT_ID,
                        KEY_CUSTOMER_ID, KEY_GRANDTOTAL }, KEY_INCREMENT_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        // return contact
        return contact;
    }

    // Getting All Contacts
    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setPhoneNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Updating single contact
    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_CUSTOMER_ID, contact.getName());
        values.put(KEY_GRANDTOTAL, contact.getPhoneNumber());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_INCREMENT_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }

    // Deleting single contact
    public void deleteContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_INCREMENT_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }


    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}
package com.example.micheal.scannerv1;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入java.util.ArrayList;
导入java.util.List;
/**
*由micheal于2015年10月25日创建。
*/
公共类DatabaseHandler扩展了SQLiteOpenHelper{
//所有静态变量
//数据库版本
私有静态最终int数据库_VERSION=1;
//数据库名称
私有静态最终字符串数据库\u NAME=“Magento”;
//联系人表名称
私有静态最终字符串表\u CONTACTS=“orders”;
//联系人表列名称
私有静态最终字符串键\u INCREMENT\u ID=“INCREMENT\u ID”;
私有静态最终字符串密钥\u CUSTOMER\u ID=“CUSTOMER\u ID”;
私有静态最终字符串键\u GRANDTOTAL=“GRANDTOTAL”;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//创建表
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_CONTACTS_TABLE=“CREATE TABLE”+TABLE_CONTACTS+”(“
+KEY\u INCREMENT\u ID+“整数主键,”+KEY\u CUSTOMER\u ID+“文本,”
+键_GRANDTOTAL+“TEXT”+”;
execSQL(创建联系人表);
}
//升级数据库
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧标签
package com.example.micheal.scannerv1;

import android.os.AsyncTask;
import android.util.Log;

import com.example.michealsadleir.scannerv1.ParseOrderList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalHashtable;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Hashtable;

/**
 * Created by micheal on 26/10/2015.
 */


public class DownloadData extends AsyncTask<Void, Void, String> {
    private String mFileContents;
    private String downLoadXMLFile;
    private static final String NAMESPACE = "urn:Magento";
    private static final String URL = "http://www..com.com/api/v2_soap";
    public String requestDump = null;
    public String resultDump = null;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected String doInBackground(Void... params) {

        try {

            SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            env.dotNet = false;
            env.xsd = SoapSerializationEnvelope.XSD;
            env.enc = SoapSerializationEnvelope.ENC;

            SoapObject request = new SoapObject(NAMESPACE, "login");

            request.addProperty("username", "");
            request.addProperty("apiKey", "");
            env.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;
            androidHttpTransport.call("", env);
            env.dotNet = false;
            Object result = env.getResponse();
            Log.d("sessionId", result.toString());
            String sessionId = result.toString();

            //Making a hashtable which outputs xml to pass as a filter in the request
            Hashtable<String, String> map = new Hashtable<String, String>();
            map.put("status", "processing");
            // Add another filter by adding something like the line below
            // map.put("increment_id", "100001886");
            Log.d("hashmapValue", map.toString());

            // found out how to add an array to the soap request as magento needs a nested array
            // to be sent in ksoap2 but it doesnt support this so the work around below worked
            // with a hash table
            // https://stackoverflow.com/questions/17342327/in-android-how-to-send-complex-array-in-magento-using-ksoap2-library/25974038#25974038

            SoapObject EntityArray = new SoapObject(NAMESPACE, "shoppingCartProductEntityArray");
            EntityArray.addProperty("filter", map);


            SoapObject requestCart = new SoapObject(NAMESPACE, "salesOrderList");
            requestCart.addProperty("sessionId", sessionId);
            requestCart.addProperty("filters", EntityArray);
            (new MarshalHashtable()).register(env);

            env.setOutputSoapObject(requestCart);
            androidHttpTransport.call("", env);
            requestDump = androidHttpTransport.requestDump;
            Log.d("request", requestDump.toString());
            (new MarshalHashtable()).register(env);
            resultDump = androidHttpTransport.responseDump;
            Log.d("result", resultDump.toString());


        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDump;



    }
    @Override
    protected void onPostExecute(String result) {

        // take the results from the soap request and pass them to the parser

        ParseOrderList parseOrderList = new ParseOrderList(resultDump);
        parseOrderList.process();
        Log.d("Parsed data", parseOrderList.getOrderLists().toString());

    }


}
    Context mContext;
    DatabaseHandler db;
    public ParseOrderList(Context mContext){
      this.mContext=mContext;
      this.db = new DatabaseHandler(mContext);
    }
ParseOrderList objParseOrderList=
                            new ParseOrderList(YOUR_ACTIVITY_CLASS_NAME.this);