从错误的数据库中检索数据

时间:2016-01-03 19:39:14

标签: java android android-sqlite

我的应用程序中有两个SQLiteDatabases。一个从DataEntryHome类中检索用户的输入。另一个从GarmentEntry类中检索用户的输入。我还有两个活动以ListView的形式显示用户的输入。这些显示在RecapPage和RecapOrderDetails活动中。

为了让自己作为一个新的java程序员更简单,我为单独的数据库使用了单独的dbHelper,DataProvider和ListDataAdapter类。

我的问题是在RecapOrderDetails类中,ListView使用DataEntryHome而不是GarmentEntry中的内容填充。 RecapPage中的ListView可以正常工作。

以下是我认为相关的所有代码:

DataEntryHome:

public class DataEntryHome extends AppCompatActivity implements TextWatcher{

private static Button DataEntryButtonN, SaveDataButton, PreviewButton;
Context context = this;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
EditText ContactName,ContactSurname,ContactEmail,ContactPhone,ContactAddInfo;
Button saveDetails;
public static ArrayList<String> CUSTOMERS = new ArrayList<String>();
String customers[];

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

    final EditText surnameArray = (EditText) findViewById(R.id.customerSurnameEntry);

    saveDetails = (Button) findViewById(R.id.saveDetailsButton);
    saveDetails.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String erm=surnameArray.getText().toString().trim();
            if(erm.length() != 0){
                CUSTOMERS.add(erm);
                surnameArray.setText("");
            }
            Intent arrayItems = new Intent(DataEntryHome.this, RecapPage.class);
            Bundle arrayItemsBundle = new Bundle();

        }
    });

}

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

    ContactName = (EditText) findViewById(R.id.customerFirstNameEntry);
    ContactName.addTextChangedListener(this);
    ContactSurname = (EditText) findViewById(R.id.customerSurnameEntry);
    ContactSurname.addTextChangedListener(this);
    ContactEmail = (EditText) findViewById(R.id.customerEmail);
    ContactEmail.addTextChangedListener(this);
    ContactPhone = (EditText) findViewById(R.id.customerNumber);
    ContactPhone.addTextChangedListener(this);
    ContactAddInfo = (EditText) findViewById(R.id.addInfo1);

    setupSaveDataButton();


}

public void addContact(View view) {

    String name = ContactName.getText().toString();
    String surname = ContactSurname.getText().toString();
    String email = ContactEmail.getText().toString();
    String phone = ContactPhone.getText().toString();
    String add_info = ContactAddInfo.getText().toString();

    userDbHelper = new UserDbHelper(context);
    sqLiteDatabase = userDbHelper.getWritableDatabase();
    userDbHelper.addInformation(name,surname,email,phone,add_info,sqLiteDatabase);
    Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_SHORT).show();
    userDbHelper.close();
}

RecapPage:

public class RecapPage extends AppCompatActivity{

ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
Button goButtonAction;

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

    goButtonAction = (Button) findViewById(R.id.goButton);
    listView = (ListView) findViewById(R.id.list_view);
    listView.setClickable(true);
    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
    listView.setAdapter(listDataAdapter);
    userDbHelper = new UserDbHelper(getApplicationContext());
    sqLiteDatabase = userDbHelper.getReadableDatabase();
    cursor = userDbHelper.getInformation(sqLiteDatabase);
    if(cursor.moveToFirst()) {
        do {

            String first_name, surname, email, phone, add_info;
            first_name = cursor.getString(0);
            surname = cursor.getString(1);
            email = cursor.getString(2);
            phone = cursor.getString(3);
            add_info = cursor.getString(4);
            DataProvider dataProvider = new DataProvider(first_name,surname,email,phone,add_info);
            listDataAdapter.add(dataProvider);

        } while (cursor.moveToNext());
    }

    Intent arrayItems = getIntent();
    Bundle arrayItemsBundle = arrayItems.getExtras();

}

GarmentEntry:

public class GarmentEntry extends AppCompatActivity {

Spinner tcshenspinner, backprintoptionsspinner, tcbackhenspinner, cosspinner, ppspinner;
ArrayAdapter<CharSequence> tcshenspinneradapter, backprintoptionsspinneradapter, tcbackhenspinneradapter,
        cosspinneradapter, ppspinneradapter;
Button nextButton1;
public static ImageView imagePreview;
public static final String IMAGE_RES_ID_1 = "image_res_id_1";
Context contextOrder = this;
OrderDbHelper userDbHelperOrder;
SQLiteDatabase sqLiteDatabaseOrder;
EditText OrderNoOfShirts, OrderFrontText,OrderShirt1;
Spinner OrderColourOfShirts, OrderPrintPosition, OrderColourOfText, OrderBackPrint, OrderBackColour;
Button saveOrderDetails;
public static ArrayList<String> ORDERINFO = new ArrayList<>();
String orderinfo[];


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.garment_entry);

    final EditText shirtArray = (EditText)findViewById(R.id.noofshirts);

    saveOrderDetails = (Button)findViewById(R.id.saveOrderDetails);
    saveOrderDetails.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String erm=shirtArray.getText().toString().trim();
            if (erm.length() != 0){
                ORDERINFO.add(erm);
                shirtArray.setText("");
            }
            Intent arrayItemsOrder = new Intent(GarmentEntry.this, RecapOrderDetails.class);
            Bundle arrayItemsOrderBundle = new Bundle();
        }
    });

    OrderNoOfShirts = (EditText)findViewById(R.id.noofshirts);
    OrderColourOfShirts = (Spinner)findViewById(R.id.cosspinner);
    OrderFrontText = (EditText)findViewById(R.id.fronttexthint);
    OrderPrintPosition = (Spinner)findViewById(R.id.ppspinner);
    OrderColourOfText = (Spinner)findViewById(R.id.tcshenspinner);
    OrderBackPrint = (Spinner)findViewById(R.id.backprintoptionsspinner);
    OrderBackColour = (Spinner)findViewById(R.id.tcbackhenspinner);
    OrderShirt1 = (EditText)findViewById(R.id.nnsshirt1);

}

public void addOrder(View view){

    String no_of_shirts = OrderNoOfShirts.getText().toString();
    String colour_of_shirts = OrderColourOfShirts.getSelectedItem().toString();
    String front_text = OrderFrontText.getText().toString();
    String print_position = OrderPrintPosition.getSelectedItem().toString();
    String colour_of_text = OrderColourOfText.getSelectedItem().toString();
    String back_print = OrderBackPrint.getSelectedItem().toString();
    String back_colour = OrderBackColour.getSelectedItem().toString();
    String shirt_1 = OrderShirt1.getText().toString();

    userDbHelperOrder = new OrderDbHelper(contextOrder);
    sqLiteDatabaseOrder = userDbHelperOrder.getWritableDatabase();
    userDbHelperOrder.addInformationOrder(no_of_shirts,colour_of_shirts,front_text,print_position,colour_of_text,back_print,
            back_colour,shirt_1, null,null,null,null,null,null,null,null,null,null,sqLiteDatabaseOrder);
    Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_SHORT).show();
    userDbHelperOrder.close();

}

RecapOrderDetails:

public class RecapOrderDetails extends AppCompatActivity {

ListView listViewOrder;
SQLiteDatabase sqLiteDatabaseOrder;
UserDbHelper userDbHelperOrder;
Cursor cursorOrder;
ListDataAdapterOrder listDataAdapterOrder;

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

    listViewOrder = (ListView)findViewById(R.id.list_view_order);
    listViewOrder.setClickable(true);
    listDataAdapterOrder = new ListDataAdapterOrder(getApplicationContext(),R.layout.order_layout);
    listViewOrder.setAdapter(listDataAdapterOrder);
    userDbHelperOrder = new UserDbHelper(getApplicationContext());
    sqLiteDatabaseOrder = userDbHelperOrder.getReadableDatabase();
    cursorOrder = userDbHelperOrder.getInformation(sqLiteDatabaseOrder);
    if (cursorOrder.moveToFirst()){
        do {

            String no_of_shirts, colour_of_shirts, front_text, print_position, text_colour, back_print, back_colour, shirt1;
            no_of_shirts = cursorOrder.getString(0);
            colour_of_shirts = cursorOrder.getString(1);
            front_text = cursorOrder.getString(2);
            print_position = cursorOrder.getString(3);
            text_colour = cursorOrder.getString(4);
            /*back_print = cursorOrder.getString(5);
            back_colour = cursorOrder.getString(6);
            shirt1 = cursorOrder.getString(7);*/
            DataProviderOrder dataProviderOrder = new DataProviderOrder(no_of_shirts,colour_of_shirts,front_text,print_position,
                    text_colour,null,null,null);
            listDataAdapterOrder.add(dataProviderOrder);

        }while (cursorOrder.moveToNext());
    }

    Intent arrayItems = getIntent();
    Bundle arrayItemsBundle = arrayItems.getExtras();
}

我认为错误数据传递到第二个数据库的原因是:

if (cursorOrder.moveToFirst()){
        do {

            String no_of_shirts, colour_of_shirts, front_text, print_position, text_colour, back_print, back_colour, shirt1;
            no_of_shirts = cursorOrder.getString(0);
            colour_of_shirts = cursorOrder.getString(1);
            front_text = cursorOrder.getString(2);
            print_position = cursorOrder.getString(3);
            text_colour = cursorOrder.getString(4);
            /*back_print = cursorOrder.getString(5);
            back_colour = cursorOrder.getString(6);
            shirt1 = cursorOrder.getString(7);*/
            DataProviderOrder dataProviderOrder = new DataProviderOrder(no_of_shirts,colour_of_shirts,front_text,print_position,
                    text_colour,null,null,null);
            listDataAdapterOrder.add(dataProviderOrder);

是否为第一个数据库(对于DataEntryHome)保留了值0-4?

我也认为:

userDbHelperOrder = new UserDbHelper(getApplicationContext());
sqLiteDatabaseOrder = userDbHelperOrder.getReadableDatabase();
cursorOrder = userDbHelperOrder.getInformation(sqLiteDatabaseOrder);

与此事有关。

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。

关于活动RecapOrderDetails,这个:

userDbHelperOrder = new UserDbHelper(getApplicationContext());

是这个问题。

要解决它,我必须执行以下操作:

OrderDbHelper = orderDbHelperOrder

在主要方法中。

然后替换

userDbHelperOrder = new UserDbHelper(getApplicationContext());

orderDbHelperOrder = new OrderDbHelper(getApplicationContext());