从SQLite数据库删除对象不能使用Android

时间:2014-04-26 17:48:47

标签: android sqlite object android-intent sql-delete

我不知道为什么,但如果我尝试从我的数据库中删除一个对象,它就什么都不做。 有时它起作用,这就是为什么我这么困惑。

我正在做的是创建GoogleCards并从我的数据库中的每个Objekt向他们提供信息。在OnClickListener上,我从卡中获取profileID并将其设置在我的Intent中。

希望你能帮助我! 如果您有疑问,请询问。

这是我的MainActivity:

public class MainActivity extends Activity  {

private DBTools tools = new DBTools(this);
protected ArrayList<Profile> profiles;
protected final static String PROFILE_ID = "profileId";


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

    //Erstelle Array und f??ge dummies hinzu
    this.profiles = tools.getAllProfiles();

    ArrayList<Card> cards = new ArrayList<Card>();
    for (int i=0;i<profiles.size();i++){
        Profile profile = profiles.get(i);
        GooglePlaySmallCard card = new GooglePlaySmallCard(this);
        card.setCardId(i);
        card.setTitle(profile.getCompany());
        card.setSecondaryTitle(profile.getStr());
        card.setThirdTitle(profile.getPlz());
        card.setFourthTitle(profile.getCity());
        card.setImgResource(R.drawable.foto1);
        card.setColor(profile.getColor());
        card.setOnClickListener(new OnCardClickListener(){

            @Override
            public void onClick(Card arg0, View arg1) {
                GooglePlaySmallCard card = (GooglePlaySmallCard) arg0;
                Toast.makeText(getApplicationContext(), String.valueOf(card.getCardId()), Toast.LENGTH_SHORT).show();
                Intent cardIntent = new Intent(getApplicationContext(),showProfile.class);
                cardIntent.putExtra(PROFILE_ID, card.getCardId());
                startActivity(cardIntent);

            }

        });

这是我的ShowProfile类:

public class showProfile extends Activity {

protected TextView company,str,plz,city,gehalt;
protected ImageView companyImage;
private DBTools tools = new DBTools(this);
protected final static String PROFILE_ID = "profileId";
protected int profileId;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    ArrayList<Profile> profiles = tools.getAllProfiles();

    Intent theIntent = getIntent();
    this.profileId =theIntent.getExtras().getInt(PROFILE_ID);

    Profile profile = profiles.get(profileId);
    this.company = (TextView) findViewById(R.id.textView_company_dynamic);
    this.str = (TextView) findViewById(R.id.textView_str_dynamic);
    this.plz = (TextView) findViewById(R.id.textView_plz_dynamic);
    this.city = (TextView) findViewById(R.id.textView_city_dynamic);
    this.gehalt = (TextView) findViewById(R.id.textView_gehalt_dynamic);
    this.company.setText(profile.getCompany());
    this.str.setText(profile.getStr());
    this.plz.setText(profile.getPlz());
    this.city.setText(profile.getCity());
    this.gehalt.setText(profile.getWage());
    this.companyImage = (ImageView) findViewById(R.id.picture_company);

    }

最后是我的数据库方法deleteProfile()

        public void deleteProfile(int id){

        //erhalte schreibrechte auf die datenbanktabelle

        SQLiteDatabase database = this.getWritableDatabase();


        // erstelle SQLite Statement
        String deleteQuery = "DELETE FROM profiles WHERE profileId='" + id + "'";


        //versende SQLite Statement

        database.execSQL(deleteQuery);

    }

0 个答案:

没有答案