输入数据库时​​Android吐司消息

时间:2013-05-24 15:54:09

标签: java android eclipse toast

我正在开发一个Android应用程序。我将在同时插入数据库时​​显示toast消息。但不幸的是,吐司的消息没有出现。这是我的代码:

        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();
        Cursor c = mySQLiteAdapter.getAllContent();
        c.moveToLast();
        int count1=c.getCount();
            if(count1==0)
            {
                //value not present
                Toast.makeText(this, "Now Loading...", Toast.LENGTH_LONG).show();
                insertData();
            }
            else
            {
              //value present
            }
        mySQLiteAdapter.close();

insertData()是将数据插入数据库的方法。有人知道我的代码有什么问题吗?

这是我班级的完整代码:

package com.example.anagramslayer;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener  {

 private SQLiteAdapter mySQLiteAdapter;
 ImageButton LevelModule;
 ImageButton ExitModule;
 ImageButton HelpModule;
 ImageButton CreditModule;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //TextView listContent = (TextView)findViewById(R.id.listview); //buat nampilin listview

      //init buttons
        LevelModule =  (ImageButton) findViewById(R.id.play_button);
        LevelModule.setOnClickListener(this);
        ExitModule =  (ImageButton) findViewById(R.id.exit_button);
        ExitModule.setOnClickListener(this);
        HelpModule =  (ImageButton) findViewById(R.id.help_button);
        HelpModule.setOnClickListener(this);
        CreditModule =  (ImageButton) findViewById(R.id.credit_button);
        CreditModule.setOnClickListener(this);


        mySQLiteAdapter = new SQLiteAdapter(this);
        AppPreferences AppPref = new AppPreferences(this);

        //int x = AppPref.getScore();
        //TextView t=(TextView)findViewById(R.id.score);
        //t.setText(x);

        //checking values exists
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();
        Cursor c = mySQLiteAdapter.getAllContent();
        c.moveToLast();
        int count1=c.getCount();
            if(count1==0)
            {
                //value not present
                Toast.makeText(this, "Now Loading...", Toast.LENGTH_LONG).show();
                insertData();
            }
            else
            {
              //value present
            }
        mySQLiteAdapter.close();
        //end of checking value exists

        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();
        //String contentRead = mySQLiteAdapter.queueAll(); //buat nampilin listview
        mySQLiteAdapter.close();  
        //listContent.setText(contentRead);                //buat nampilin listview


    }

    @Override
    public void onClick(View v) {
         switch(v.getId()){
            case R.id.play_button:
                startActivity(new Intent(Main.this, LevelList.class));
                finish();
                break;
            case R.id.exit_button:
                 finish();
                 System.exit(0);
                break;
            case R.id.help_button:
                startActivity(new Intent(Main.this, Help.class));
                finish();
                break;
            case R.id.credit_button:
                startActivity(new Intent(Main.this, Credit.class));
                finish();
                break;
             }
         }
        // TODO Auto-generated method stub

    private void insertData() {
        //insert record
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();

        mySQLiteAdapter.insert("apel",      "Peal",         "the competitor of samsung", "0", "1", "com.example.anagramslayer:drawable/hint1_1");
        mySQLiteAdapter.insert("semangka",  "Game Sank",    "it's big and green", "0", "1", "com.example.anagramslayer:drawable/hint1_2");
        mySQLiteAdapter.insert("anggur",    "Run Gag",      "ferment it, it will become wine anggur run gag ferment it, it will become wine","0", "1", "com.example.anagramslayer:drawable/hint1_3");
        mySQLiteAdapter.insert("pisang",    "Gasp In",      "it's yellow and loved by minion", "0", "1", "com.example.anagramslayer:drawable/hint1_4");
        mySQLiteAdapter.insert("melon",     "Lemon",        "it's round, quite big, and green", "0", "1", "com.example.anagramslayer:drawable/hint1_5");
        mySQLiteAdapter.insert("alpukat",   "A Talk Up",    "it tastes plain. Usually, people make juice or ice drink from it", "0", "1", "com.example.anagramslayer:drawable/hint1_6");
        mySQLiteAdapter.insert("mangga",    "Gag Man",      "it's green. Sometimes, it tastes sweet or sour", "0", "1", "com.example.anagramslayer:drawable/hint1_7");
        mySQLiteAdapter.insert("cokelat",   "A Locket",     "its name is the same with its color", "0", "1", "com.example.anagramslayer:drawable/hint1_8");
        mySQLiteAdapter.insert("persik",    "Pikers",       "in japan, people call it momo fruit", "0", "1", "com.example.anagramslayer:drawable/hint1_9");
        mySQLiteAdapter.insert("salak",     "Ask La",       "its skin is like a snake skin", "0", "1", "com.example.anagramslayer:drawable/hint1_10");
        mySQLiteAdapter.insert("kurma",     "A Murk",       "you can find this fruit in california or midle east", "0", "1", "com.example.anagramslayer:drawable/hint1_11");
        mySQLiteAdapter.insert("durian",    "Run Aid",      "spike skinned fruit and it smells bad", "0", "1", "com.example.anagramslayer:drawable/hint1_12");
        mySQLiteAdapter.insert("cempedak",  "Packed Me",    "this fruit looks like a jackfruit", "0", "1", "com.example.anagramslayer:drawable/hint1_13");
        mySQLiteAdapter.insert("jambu",     "Um Jab",       "it cures scarlet fever", "0", "1", "com.example.anagramslayer:drawable/hint1_14");
        mySQLiteAdapter.insert("kelapa",    "Lake Pa",      "you usually will find this fruit in a beach", "0", "1", "com.example.anagramslayer:drawable/hint1_15");


        //end of database

        mySQLiteAdapter.close();
        //end of insert record

    }

}

1 个答案:

答案 0 :(得分:0)

我建议你在AsyncTask类中进行数据插入,在这种情况下,当你尝试显示Toast消息时,数据Insert方法不会占用资源