点击ListView项时返回错误值

时间:2019-03-09 05:48:26

标签: android

在我的应用中,我有一个ListView。我在行上使用了FrameLayout,以便可以根据用户在行的左半部或右半部上的点击来进行新的活动。我需要将用户点击的行中的TextView值之一传递给Intent。

我正在使用以下方法来处理行右侧的水龙头:

public void goToCaptureBonus (View View) {
        String tappedBonus = ((TextView) findViewById(R.id.bonusListCode)).getText().toString();
        Log.e(TAG,"goToCaptureBonus, tapped bonus = " + tappedBonus);
        Intent goToCaptureBonus = new Intent(this,captureBonus.class);
        goToCaptureBonus.putExtra("codeTapped",tappedBonus);
        startActivity(goToCaptureBonus);
    }

但是,我没有获得正确的价值。例如,我可以看到每页7行,但是它们都从第一行返回值。如果我向下滚动并再次点击,我会得到一个不同的值,但它也不是来自正确的行。

我在做什么错了?

编辑:我的适配器(根据要求):

public class AppDataDBHelper extends SQLiteOpenHelper {

    private static String TAG = "AppDataDBHelper"; // Tag just for the LogCat window
    private static String DB_NAME ="appdata.db";
    private static String DB_TABLE = "Bonus_Data";
    private static int DB_VERSION = 1;

    private SQLiteDatabase mDataBase;
    private final Context mContext;

    public AppDataDBHelper(Context context) {
        super(context, DB_NAME, null,DB_VERSION);
        this.mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void createDataBase() throws IOException {
        Log.e(TAG,"Entered createDatabase");
        boolean mDataBaseExist = checkDataBase();
        if(!mDataBaseExist) {
            this.getReadableDatabase();
            this.close();
            try {
                //Copy the database from assets
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                throw new Error("ErrorCopyingDataBase");
            }
        }
    }

    //See if DB exists in userspace
    private boolean checkDataBase() {
        Log.e(TAG,"entered checkDatabase");
        File dbFile = new File(mContext.getDatabasePath(DB_NAME).getPath());
        if (dbFile.exists()) return true;
        Log.e(TAG,"dbFile = true");
        File dbDir = dbFile.getParentFile();
        if (!dbDir.exists()) {
            Log.e(TAG,"dbFile = false");
            dbDir.mkdirs();
        }
        return false;
    }

    //Copy the database from assets to userspace
    private void copyDataBase() throws IOException {
        InputStream mInput;
        OutputStream mOutput;
        String outFileName = mContext.getDatabasePath(DB_NAME).getPath();

        try {
            mInput = mContext.getAssets().open(DB_NAME);
            mOutput = new FileOutputStream(outFileName);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = mInput.read(buffer)) > 0) {
                mOutput.write(buffer, 0, length);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
        } catch (IOException ie) {
            throw new Error("copyDatabase() Error");
        }
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_NAME;
        //Log.v("mPath", mPath);
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

    public Cursor getAppDataFilteredBonuses() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT hMy as _id,* FROM " + DB_TABLE + " WHERE sState = 'CA'", null);
        return data;
    }

    public Cursor getAppDataTrophyRuns() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT hMy as _id,* FROM " + DB_TABLE + " WHERE sTrophyGroup IS NOT NULL GROUP BY sTrophyGroup", null);
        return data;
    }

    public Cursor getAppDataAllBonuses() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT hMy as _id,* FROM " + DB_TABLE + " ORDER BY sCode", null);
        return data;
    }

    public Cursor getAppDataSelectedBonus(String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String [] args={id};
        Cursor data = db.rawQuery("SELECT hMy as _id,* FROM " + DB_TABLE + " WHERE sCode = ? ORDER BY sCode", args);
        return data;
    }

    public Cursor getAppDataListContents() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT hMy as _id,* FROM " + DB_TABLE, null);
        return data;
    }

    @Override
    public synchronized void close()
    {
        if(mDataBase != null)
            mDataBase.close();
        super.close();
    }
}

编辑2:这是我填充目标活动的方式:

// Handle the appData DB.
        appDBHelper = new AppDataDBHelper(this);
        tappedBonusID = getIntent().getStringExtra("codeTapped");

        // Fetch Bonus Data from DB
        Cursor data = appDBHelper.getAppDataSelectedBonus(tappedBonusID);
        data.moveToFirst();
        String sCode = data.getString(data.getColumnIndex("sCode"));
        String sName = data.getString(data.getColumnIndex("sName"));
        String sCategory = data.getString(data.getColumnIndex("sCategory"));

        // Populate the view data values
        bonusCode = findViewById(R.id.bonusCode);
        bonusCode.setText(sCode);
        bonusName = findViewById(R.id.bonusName);
        bonusName.setText(sName);
        bonusCategory = findViewById(R.id.bonusCategory);
        bonusCategory.setText(sCategory);

0 个答案:

没有答案