0

我有一个网站数据(ftp 地址、用户名、密码、端口、homedir、url 等)的 SQLite 数据库。我可以将记录添加到表中,但似乎无法更新它们。

我创建了一个 SiteManager Activity,它加载每一行并从每一行创建一个 WebSite 对象。网站的属性被加载到 EditTexts。该人可以编辑属性,然后更新按钮应该更新表格行,但它不会。Logcat 没有给出任何错误,所以我完全不知所措,不知道从哪里开始。

public class SiteManager extends Activity {
    private DBAdapter myDb;

    private EditText siteManFTPAddress;
    private EditText siteManFTPUsername;
    private EditText siteManFTPPassword;
    private EditText siteManFTPPort;
    private EditText siteManURL;
    private EditText siteManHome;
    private ImageView favIcon;
    public ListView site_list;
    private Button openBtn;
    private Button siteManUpdateBtn;
    private int _rowId;
    private String _name;
    private String _remoteHomeDir;
    private int _isLive;
    private String _address;
    private String _username;
    private String _password;
    private int _port;
    private String _url;
    private boolean _status = false;
    private String siteFolder;
    private List<WebSite> model = new ArrayList<WebSite>();
    private ArrayAdapter<WebSite> adapter;



    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.site_manager);
        site_list = (ListView) findViewById(R.id.siteList);
        adapter = new SiteAdapter(this, R.id.ftpsitename, R.layout.siterow,
                model);
        site_list.setAdapter(adapter);

        addListeners();
        openDb();
        displayRecords();
    }

    public void addListeners() {
        siteManFTPAddress = (EditText) findViewById(R.id.siteManFTPAdd);
        siteManFTPUsername = (EditText) findViewById(R.id.siteManFTPUser);
        siteManFTPPassword = (EditText) findViewById(R.id.siteManFTPPass);
        siteManFTPPort = (EditText) findViewById(R.id.siteManFTPPort);
        siteManURL = (EditText) findViewById(R.id.siteManURL);
        siteManHome = (EditText) findViewById(R.id.siteManHome);

        site_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {
                File rootDir = new File(Environment
                        .getExternalStorageDirectory() + "/My Webs");
                final WebSite item = (WebSite) parent
                        .getItemAtPosition(position);
                _name = item.getName();
                siteFolder = rootDir.toString() + "/" + _name;
                _remoteHomeDir = item.getHomeDir();
                _isLive = item.isLive();

                String tmpaddress = item.getAddress();
                _address = tmpaddress;
                siteManFTPAddress.setText(_address);

                String tmpuser = item.getUsername();
                _username = tmpuser;
                siteManFTPUsername.setText(_username);

                String tmppass = item.getPassword();
                _password = tmppass;
                siteManFTPPassword.setText(_password);

                int tmpport = item.getPort();
                _port = tmpport;
                String portString = Integer.toString(tmpport);
                siteManFTPPort.setText(portString);

                String tmpURL = item.getUrl();
                _url = tmpURL;
                siteManURL.setText(_url);

                String tmpHome = item.getHomeDir();
                _remoteHomeDir = tmpHome;
                siteManURL.setText(_remoteHomeDir);

            }

        });
openBtn = (Button) findViewById(R.id.openSiteBtn);
openBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent returnResult = new Intent();

        returnResult.putExtra("siteopen", "siteopen");
        returnResult.putExtra("sitename", _name);
        returnResult.putExtra("sitehome", siteFolder);
        returnResult.putExtra("sitelive", _isLive);
        returnResult.putExtra("siteremotehome", _remoteHomeDir);
        returnResult.putExtra("siteaddress", _address);
        returnResult.putExtra("siteusername", _username);
        returnResult.putExtra("sitepassword", _password);
        returnResult.putExtra("siteport", _port);
        returnResult.putExtra("url", _url);

        setResult(2, returnResult);
        finish();

    }
});
siteManUpdateBtn = (Button)findViewById(R.id.siteManFTPUpdate);
siteManUpdateBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        _address = siteManFTPAddress.getText().toString();
        _username = siteManFTPUsername.getText().toString();
        _password = siteManFTPPassword.getText().toString();
        String port = siteManFTPPort.getText().toString();
        _port = Integer.parseInt(port);
    Toast.makeText(SiteManager.this, "Update", Toast.LENGTH_LONG).show();

    myDb.updateRow(_rowId, _name,  _name, _isLive, _address, _username, _password, _port, _url);
    model.clear();
    adapter.notifyDataSetChanged();
    displayRecords();
    }
});
    }

    private void openDb() {
        myDb = new DBAdapter(this);
        myDb.open();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        closeDb();
    }

    private void closeDb() {
        myDb.close();
    }

    public void displayRecords() {
        Cursor cursor = myDb.getAllRows();
        displayRecordSet(cursor);
    }

    protected void displayRecordSet(Cursor c) {

        if (c.moveToFirst()) {
            do {
                int rowId = c.getInt(c.getColumnIndex(DBAdapter.KEY_ROWID));
                _rowId = c.getInt(rowId);

                int keyNameIndex = c.getColumnIndex(DBAdapter.KEY_NAME);
                _name = c.getString(keyNameIndex);

                int keyHomeIndex = c.getColumnIndex(DBAdapter.KEY_HOME);
                _remoteHomeDir = c.getString(keyHomeIndex);

                int keyLiveIndex = c.getColumnIndex(DBAdapter.KEY_LIVE);
                _isLive = c.getInt(keyLiveIndex);

                int keyAddressIndex = c.getColumnIndex(DBAdapter.KEY_ADDRESS);
                _address = c.getString(keyAddressIndex);

                int keyUsernameIndex = c.getColumnIndex(DBAdapter.KEY_USERNAME);
                _username = c.getString(keyUsernameIndex);

                int keyPassIndex = c.getColumnIndex(DBAdapter.KEY_PASSWORD);
                _password = c.getString(keyPassIndex);

                int keyPortIndex = c.getColumnIndex(DBAdapter.KEY_PORT);
                _port = c.getInt(keyPortIndex);

                int keyUrlIndex = c.getColumnIndexOrThrow(DBAdapter.KEY_URL);
                _url = c.getString(keyUrlIndex);
                WebSite sitesFromDB = new WebSite(_rowId, _name, _remoteHomeDir,
                        _isLive, _address, _username, _password, _port, _url);
                model.add(sitesFromDB);
                adapter.notifyDataSetChanged();

                if(adapter.isEmpty()){

                }
            } while (c.moveToNext());
        }
        c.close();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    class SiteAdapter extends ArrayAdapter<WebSite> {
        private final List<WebSite> objects;
        private final Context context;

        public SiteAdapter(Context context, int resource,
                int textViewResourceId, List<WebSite> objects) {
            super(context, R.id.sitename, R.layout.siterow, objects);
            this.context = context;
            this.objects = objects;
        }

        /** @return The number of items in the */
        public int getCount() {
            return objects.size();
        }

        public boolean areAllItemsSelectable() {
            return false;
        }

        /** Use the array index as a unique id. */
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.siterow, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.sitename);

            textView.setText(objects.get(position).getName());

            return (rowView);
        }

    }

DBAdapter.java

public boolean updateRow(long rowId, String name, String homedir,
            int islive, String address, String username, String password,
            int port, String url) {
        String where = KEY_ROWID + "=" + rowId;

        /*
         * CHANGE 4:
         */
        // TODO: Update data in the row with new fields.
        // TODO: Also change the function's arguments to be what you need!
        // Create row's data:
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NAME, name);
        newValues.put(KEY_HOME, homedir);
        newValues.put(KEY_LIVE, islive);
        newValues.put(KEY_ADDRESS, address);
        newValues.put(KEY_USERNAME, username);
        newValues.put(KEY_PASSWORD, password);
        newValues.put(KEY_PORT, port);
        newValues.put(KEY_URL, url);
        // newValues.put(KEY_PASSIVE, passive);
        // Insert it into the database.
        return db.update(DATABASE_TABLE, newValues, where, null) != 0;
    }
4

2 回答 2

1

为了诊断这样的问题,我通常将调试日志添加到应用程序中。你可以在你的 logcat 中看到这些。Log.d("tag", "there is something happening here: " + value);

于 2013-07-08T23:54:36.607 回答
1

该值_rowIddisplayRecordSet在您遍历数据库结果并设置的方法内设置_rowId

int rowId = c.getInt(c.getColumnIndex(DBAdapter.KEY_ROWID));
_rowId = c.getInt(rowId);

这段代码对我来说似乎相当随机。首先你得到columnIndexfor rowId,接下来你得到这个特定行的索引,然后你得到索引列的值,rowId然后从这个值设置_rowId字段。

我不知道如果指定列中没有任何值,SQLite 数据库是否会如此讨厌,以至于只返回 0,但这肯定是问题所在。

因此,每次获取_rowId集合时,它可能只是设置为 0,当您尝试更新rowId = 0没有发生任何事情的行时,因为数据库中的任何索引都不能为 0。

请参阅有关getInt(columnIndex).

于 2013-07-09T00:07:00.377 回答