在我的设备中查看数据错误

时间:2013-03-19 02:15:56

标签: java android sqlite

由于我还无法发布图片,请耐心等待。我在设备中查看db的存储数据时遇到问题。他们只是不和我打字的东西搭档。

例如,我在下面键入了这个:

fullname > jane doe
gender > female
age > 21
address > usa
contactnumber > 123456789
height(inches) > 53
weight(pounds) > 153

当我已经查看上述内容时,会发生这种情况:

fullname >  
gender > 21
age > 
address > 123456789
contactnumber > 53
height > 15
weight > 38.

这是我的ViewClient.java代码

public class DatabaseViewClient extends Activity {
    private EditText fullname;
    private EditText address_client;
    private EditText age_client;
    private EditText gender;
    private EditText contactNum_client;
    private EditText height_client;
    private EditText weight_client;
    private Button btnSaveCustomer;    
    int current_client_id;
    String current_client_fullname;
    String current_client_address;    
    String current_client_contactnum;
    String current_client_height;
    String current_client_weight;
    String current_client_gender;
    int current_client_age;
    String current_client_bmi, current_client_status;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addclient);

        fullname = (EditText) findViewById(R.id.fullname);
        gender = (EditText) findViewById(R.id.gender);
        age_client = (EditText) findViewById(R.id.age_client);
        address_client = (EditText) findViewById(R.id.address_client);
        contactNum_client = (EditText) findViewById(R.id.contactNum_client);
        height_client = (EditText) findViewById(R.id.height_client);
        weight_client = (EditText) findViewById(R.id.weight_client);

        btnSaveCustomer = (Button) findViewById(R.id.save);

        Bundle extras = getIntent().getExtras();
        //current_client_id = extras.getInt("CLIENT_ID");
        current_client_fullname = extras.getString("FULLNAME");
        current_client_gender = extras.getString("GENDER");
        current_client_age = extras.getInt("AGE");
        current_client_address = extras.getString("ADDRESS");
        current_client_contactnum = extras.getString("CONTACT_NUM");
        current_client_height = extras.getString("HEIGHT");
        current_client_weight = extras.getString("WEIGHT");
        current_client_bmi = extras.getString("BMI");
        current_client_status = extras.getString("STATUS");

        fullname.setText(current_client_fullname);
        gender.setText(current_client_gender);
        address_client.setText(current_client_address);
        contactNum_client.setText(current_client_contactnum);
        height_client.setText(current_client_height);
        weight_client.setText(current_client_weight);

        fullname.setKeyListener(null);
        gender.setKeyListener(null);
        age_client.setKeyListener(null);
        address_client.setKeyListener(null);
        contactNum_client.setKeyListener(null);
        height_client.setKeyListener(null);
        weight_client.setKeyListener(null);

        btnSaveCustomer.setVisibility(View.GONE);
    }
}

以下是DBMainActivity的代码

public class DatabaseActivity extends ListActivity {
    public static final String TABLE_NAME = "client";
    private SQLiteDatabase db;
    private ClientDBHelper dh;
    int[] client_id;
    String[] full_name;
    int[] age;
    String[] gender;
    String[] address;
    String[] contact_num;
    String[] height;
    String[] weight;
    String[] bmi;
    String[] status;
    private int counter_number;
    private int position_id;    
    public static final int ADD_CLIENT_NAME_ID = Menu.FIRST + 1;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.client_main_options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.itemAddClient:
                Intent intent = new Intent(DatabaseActivity.this,
                        DatabaseAddClient.class);
                //setContentView(R.layout.addclient);
                startActivity(intent);
                break;
            //Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)
                menuInfo;
        menu.setHeaderTitle(full_name[info.position]);
        position_id = info.position;
        menu.add(0, v.getId(), 0, "Edit Client");
        menu.add(0, v.getId(), 0, "Delete Client");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle() == "Edit Client") {

            int current_client_id = client_id[position_id];
            String current_client_fullname = full_name[position_id];
            String current_client_gender = gender[position_id];
            int current_client_age = age[position_id];
            String current_client_address = address[position_id];
            String current_client_contactnum = contact_num[position_id];
            String current_client_height = height[position_id];
            String current_client_weight = weight[position_id];
            String current_client_bmi = bmi[position_id];
            String current_client_status = status[position_id];

            Intent intent = new Intent(this, DatabaseEditClient.class);
            Bundle bundle = new Bundle();

            bundle.putInt("CLIENT_ID", current_client_id);
            bundle.putString("LASTNAME", current_client_fullname);
            bundle.putString("GENDER", current_client_gender);
            bundle.putInt("AGE", current_client_age);
            bundle.putString("ADDRESS", current_client_address);
            bundle.putString("CONTACT_NUM", current_client_contactnum);
            bundle.putString("HEIGHT", current_client_height);
            bundle.putString("WEIGHT", current_client_weight);
            bundle.putString("BMI", current_client_bmi);
            bundle.putString("STATUS", current_client_status);
            intent.putExtras(bundle);
            startActivity(intent);
        } else if (item.getTitle() == "Delete Client") {

            //Add prompt if sure to delete
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(DatabaseActivity.this);
            alt_bld.setMessage("Are you sure you want to delete the selected customer?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // Action for 'Yes' Button
                            deleteRow(client_id[position_id]);
                        }

                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alt_bld.create();
            alert.setTitle("Delete Client");
            alert.setIcon(R.drawable.warning);
            alert.show();
        } else {
            return false;
        }
        return true;
    }

    public void deleteRow(int rowId) {
        db = dh.getWritableDatabase();
        db.delete(TABLE_NAME, "client_id=" + rowId, null);
        db.close();
        getItems();
    }

    public void onResume() {
        super.onResume();
        getItems();

    }

    @SuppressWarnings("deprecation")
    private Cursor getItems() {
        db = dh.getWritableDatabase();

        db.close();

        db = dh.getReadableDatabase();
        Cursor cursor = db.query(ClientDBHelper.TABLE_NAME, null, null,
                null, null, null, null, null);

        client_id = new int[cursor.getCount()];
        full_name = new String[cursor.getCount()];
        age = new int[cursor.getCount()];
        gender = new String[cursor.getCount()];
        address = new String[cursor.getCount()];
        contact_num = new String[cursor.getCount()];
        height = new String[cursor.getCount()];
        weight = new String[cursor.getCount()];
        bmi = new String[cursor.getCount()];
        status = new String[cursor.getCount()];

        counter_number = 0;

        startManagingCursor(cursor);
        cursor.moveToFirst();

        if (cursor.getCount() != 0) {
            while (cursor.isAfterLast() == false) {
                client_id[counter_number] = cursor.getInt(0);
                full_name[counter_number] = cursor.getString(1);
                gender[counter_number] = cursor.getString(2);
                age[counter_number] = cursor.getInt(3);
                gender[counter_number] = cursor.getString(4);
                address[counter_number] = cursor.getString(5);
                contact_num[counter_number] = cursor.getString(6);
                height[counter_number] = cursor.getString(7);
                weight[counter_number] = cursor.getString(8);
                //  bmi[counter_number]=cursor.getString(9);
                //  status[counter_number]=cursor.getString(10);

                counter_number++;

                cursor.moveToNext();
            }
        }
        cursor.close();

        //Toast.makeText(RSSViewWebsite.this, website_name[counter_number].toString(), Toast.LENGTH_LONG).show();

        this.setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, full_name));
        registerForContextMenu(getListView());

        db.close();

        return cursor;
    }


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);

        dh = new ClientDBHelper(this);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        position_id = position;

        //int current_client_id = client_id[position_id];
        String current_client_name = full_name[position_id];
        int current_client_age = age[position_id];
        String current_client_address = address[position_id];
        String current_client_gender = gender[position_id];
        String current_client_contactnum = contact_num[position_id];
        String current_client_height = height[position_id];
        String current_client_weight = weight[position_id];
        //  String  current_client_bmi = bmi[position_id];
        //String current_client_status = status[position_id];

        Intent intent = new Intent(DatabaseActivity.this, DatabaseViewClient.class);
        Bundle bundle = new Bundle();
        //bundle.putInt("CLIENT_ID", current_client_id);
        bundle.putString("FULLNAME", current_client_name);
        bundle.putInt("AGE", current_client_age);
        bundle.putString("GENDER", current_client_gender);
        bundle.putString("ADDRESS", current_client_address);
        bundle.putString("CONTACT_NUM", current_client_contactnum);
        bundle.putString("HEIGHT", current_client_height);
        bundle.putString("WEIGHT", current_client_weight);
        //bundle.putString("BMI", current_client_bmi);
        //bundle.putString("STATUS", current_client_status);

        intent.putExtras(bundle);
        startActivity(intent);
    }
}

0 个答案:

没有答案