0

好吧……我受够了。

我彻底沮丧了。

所以我宁愿寻求帮助而不是新的显示器。

...而且这些在这里非常昂贵。

长话短说...我有一个数据库。还有一张桌子。

private String DEFINE_PROP_TYPES = "CREATE TABLE " + TABLE_PROP_TYPES + "("
        + TABLE_ID + " INTEGER PRIMARY KEY, "
        + TABLE_PROP_TYPE_NAME + " TEXT NOT NULL"
        + ")";

使用一个“适配器”类来管理它。

public abstract class DBAdapter 
{
    static public final String C_COLUMN_ID = "_id";

    protected Context context;
    protected DBHelper dbHelper;
    protected SQLiteDatabase db;
    protected String managedTable;
    protected String[] columns;

    public String getTableManaged() 
    {
        return managedTable;
    }

    public void setTableManaged(String managedTable) 
    {
        this.managedTable = managedTable;
    }

    public void setColumns(String[] columns)
    {
        this.columns = columns;
    }

    public DBAdapter(Context context)
    {
        this.context = context;
    }

    public void close()
    {
        dbHelper.close();
    }

    public DBAdapter open() throws SQLException
    {
        dbHelper = new DBHelper(context);
        db = dbHelper.getWritableDatabase();
        return this;
    }

    public Cursor getList()
    {
        Cursor c = db.query(true, managedTable, columns, null, null, null, null, null, null);

        return c;       
    }

    public long insert(ContentValues reg)
    {
        return 0;
    }
}
public class PropTypesDBAdapter extends DBAdapter
{
    static public final String C_TABLE_PROP_TYPES = "PROP_TYPES";

    static public final String C_COLUMN_ID = "_id",
        C_COLUMN_PROP_TYPES_NAME = "re_prop_type";

    public PropTypesDBAdapter(Context context)
    {
        super(context);
        this.setTableManaged(C_TABLE_PROP_TYPES);
        this.setColumns(new String[] { C_COLUMN_ID,
            C_COLUMN_PROP_TYPES_NAME });
    }

    public long insert(ContentValues reg)
    {
        if (db == null)
        {
            open();
        }

        return db.insert(C_TABLE_PROP_TYPES, null, reg);
    }
}

除了这堆可爱的我还有一个活动课。

带旋转器。

public class PropDetailActivity extends Activity implements LocationListener
{
    // insert here some blah-blah constants not needed by spinners

    private PropDBAdapter mHouses;
    private RatingsDBAdapter mRatings;
    private PropTypesDBAdapter mPropTypes;
    private Cursor mCursorHouses, 
        mCursorRatings,
        mCursorPropTypes;

    long mPropType;

    private long mPropId;

    private Spinner spinnerRating, spinnerType;
    AdapterView.OnItemSelectedListener spnLstPropType, spnLstRating;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_detail);

        Intent intent = getIntent();
        Bundle extra = intent.getExtras();

        if (extra == null)
        {
            return;
        }

        // Figure all view widgets being retrieved here, including...

        spinnerRating = (Spinner) findViewById(R.id.spinnerRating);
        spinnerType = (Spinner) findViewById(R.id.spinnerType);

        // Create adapter and cursor-y things here

        mHouses = new PropDBAdapter(this);
        mHouses.open();

        // And now, for the juicy, deliciously irritating stuff:

        String[] from = new String[] { PropTypesDBAdapter.C_COLUMN_PROP_TYPES_NAME };

        int[] to = new int[] { android.R.id.text1 };

        mPropTypes = new PropTypesDBAdapter(this);
        mPropTypes.open();

        mCursorPropTypes = mPropTypes.getList();

        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapterPropTypes = new SimpleCursorAdapter(this, 
                android.R.layout.simple_spinner_item, 
                mCursorPropTypes, 
                from,       /*new String[] { RatingsDBAdapter.C_COLUMN_RATING_NAME }, */
                to);        /*new int[] { android.R.id.text1 } */

        adapterPropTypes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinnerType.setAdapter(adapterPropTypes);

        spinnerRating.setSelection(pos);

        spnLstPropType = new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) 
            {
                mPropType = id;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) { }
        };
        spinnerType.setOnItemSelectedListener(spnLstPropType);

    private int getItemPositionById(Cursor c, long id, DBAdapter adapter)
    {
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        {
            if (c.getLong(c.getColumnIndex(DBAdapter.C_COLUMN_ID)) == id)
            {
                return c.getPosition();
            }
        }

        return 0;
    } 

    private void query(long id) 
    {
        mCursorHouses = mHouses.getRecord(id);

        // Figure values being retrieved and set on their widgets instead of this comment... and now...

        mPropType = mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID));

        spinnerType.setSelection(
            getItemPositionById(
                    mCursorRatings, 
                    mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID),
                    mPropTypes
                )
            );

    private void save() 
    {
        ContentValues reg = new ContentValues();

        // Read: values being put into 'reg'... eventually it should reach this:

        reg.put(PropDBAdapter.C_PROP_TYPE_ID, mPropType);

        try
        {
            if (mFormMode == PropListActivity.C_CREATE)
            {
                mHouses.insert(reg);
                Toast.makeText(PropDetailActivity.this, R.string.house_create_notice, Toast.LENGTH_LONG).show();
            }
            else if (mFormMode == PropListActivity.C_EDIT)
            {
                Toast.makeText(PropDetailActivity.this, R.string.house_edit_notice, Toast.LENGTH_LONG).show();

                reg.put(PropDBAdapter.C_COLUMN_ID, mPropId);

                long resultCode = mHouses.update(reg);
                Log.i(this.getClass().toString(), "Database operation result code: " + resultCode);         
            }
        }
        catch(SQLException e)
        {
            Log.i(this.getClass().toString(), e.getMessage());
        }

        setResult(RESULT_OK);
        finish();
    }
}   

纺纱厂是坏孩子。最重要的是懒惰的坏男孩。

他们确实加载了要显示的数据——房地产类型列表。

经过一番打屁股,就是这样。

但是,希望他们将您选择的值保存到 SQLite?并在从数据库中取回东西时显示那个确切的值?

哦,不,没办法,怎么办。

他们顽固地坚持在活动启动时始终显示相同的值。

所以...拜托...我必须利用你们的集体智慧来挽救我对一个项目的抱歉的借口...

拜托拜托?:)

如果您想深入了解整个未删减的代码,这里有一个 GIT 存储库供您使用:https ://github.com/CruxMDQ/Quoterv3 )

4

2 回答 2

2

query检查你的代码,我想我发现了问题,在你的方法中更改以下行PopDetailActivity.java
对于spinnerRating做:

spinnerRating.setSelection(
    getItemPositionById(
        mCursorRatings, 
        mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_RATING_ID)),
        mRatings
    )
);

并且spinnerType做:

spinnerType.setSelection(
    getItemPositionById(
        mCursorPropTypes, 
        mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID)),
        mPropTypes
    )
);

编辑:

mPropTypeId在您的查询方法中,您使用调用来初始化,getItemPositionById()但在该调用中,第一个参数应该mCursorPropTypesmCursorHouses

于 2013-08-27T21:33:17.157 回答
0

一些东西:

SQLite(1) 在您实际创建数据库或使用SQLiteOpenHelper类访问该数据的地方,我真的看不到任何地方。看看这个教程。它使用一个简单的单表设置来存储数据。创建数据库后,它应该很容易读取和写入。验证您是否确实创建了一个数据库。

(2) 您的查询在哪里SQL返回您正在寻找的数据?即使正在添加数据,您也需要确保在Cursor完成后获得正确的数据。如果您每次都获得相同的值,那么您可能只是每次都添加新数据并使用光标检索相同的值 - 即您没有告诉光标获取新添加的数据,因为您一直在抓取相同的索引?

如果您需要替换那里的数据,您应该使用更新查询而不是插入。

于 2013-08-27T17:33:18.167 回答