java—使用sqlite使imageview消失

oxcyiej7  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(130)

所以我用androidstudio制作了一个android应用程序,它使用sqlite数据库并更新数据库中的值。数据库中有两列:技能和真相。ui中有一个可单击的图像视图。如果真值为0,imageview将显示40%。以下是imageview的xml代码:

<ImageView
android:id="@+id/imageView4"
android:layout_width="192dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:onClick="DishWashing"
app:srcCompat="@drawable/dish_washing" />

我的java代码如下:

public class EmployeeActivity extends AppCompatActivity {
    public SQLiteDatabase db;
    public final String increment= "update work set truth = 1 where skill = ";
    public final String decrement = "update work set truth = 0 where skill = ";
    ImageView dishWashing = (ImageView) findViewById(R.id.imageView4);

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

        try{
            db = this.openOrCreateDatabase("Work", MODE_PRIVATE, null);
            db.execSQL("create table if not exists work (skill varchar, truth int(1))");
            db.execSQL("delete from work"); // so that I have a fresh table every time I start
            db.execSQL("insert into work (skill, truth) values ('dish washing', 0)");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public int GetTruthFromCursor(int index){
        Cursor c  = db.rawQuery("select * from work", null);
        c.moveToFirst();
        int i = 0;
        while(i < index){
            c.moveToNext();
        }
        int truth = c.getInt(1);
        c.close();
        return truth;
    }

    public int IncrementAndDecrement(int index, String skill){
        int initial_truth = GetTruthFromCursor(index);
        if(initial_truth == 0) {
            db.execSQL(increment + skill);
        }
        else{
            db.execSQL(decrement + skill);
        }
        int final_truth = GetTruthFromCursor(index);
        return final_truth;
    }
    public void DishWashing(View v){
        int t = IncrementAndDecrement(1, "'dish washing");
        if(t == 0){
            cleaningHouse.setAlpha(0.4f);
        }
    }

所以,当我点击imageview时,它的真值应该增加到1,当我再次点击时,它应该变成0,然后是40%可见。当我运行程序时,我的应用程序说它没有响应。我没有任何错误。任何帮助都会很好。谢谢您。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题