0

我在避免强制停止我的应用程序方面遇到了一些问题。当我单击加载按钮时,它应该加载数据库中存在的所有数据。但是,如果我的数据库中没有数据,我想显示一个 toast,表示数据库中没有要获取的数据。

      save.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                db.addContact(new PlayerData(name,score));
                Log.d("Insert: ", "Inserting .."); 

            } 
      });



          load.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {

                    List<PlayerData> contacts = db.getAllContacts();
                    //check to ensure there are users
                    if(contacts.size()==0)
                        try {
                            throw new Exception();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    PlayerData player = contacts.get(0);
                    String showName = player.getName();
                    int showScore = player.getscore();
                    Log.d("Getting:", showName);
                    Log.d("Getting:", Integer.toString(showScore));
                    saved.setText("Player Name: "+showName+" Player Score: "+showScore);

                } 
          });
4

2 回答 2

1

你的问题在这里:

if(contacts.size()==0)
    try {
        throw new Exception();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您的代码忽略了没有联系人的事实,并尝试对所述不存在的联系人执行操作。相反,您应该在此 i​​f 语句中显示 Toast,并在 else 块中执行其余代码:

load.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {

            List<PlayerData> contacts = db.getAllContacts();
            //check to ensure there are users
            if(contacts.size()==0) {
                String text = "No contacts in db";
                Toast.makeText(text, Toast.LENGTH_SHORT).show();
            } else {
                PlayerData player = contacts.get(0);
                String showName = player.getName();
                int showScore = player.getscore();
                Log.d("Getting:", showName);
                Log.d("Getting:", Integer.toString(showScore));
                saved.setText("Player Name: "+showName+" Player Score: "+showScore);
            }
        } 
  });
于 2013-09-01T03:23:20.913 回答
0

这是您可以执行的操作:

  1. 实例化你的List<PlayerData>

  2. 从数据库加载数据

  3. 检查您的List<PlayerData>. 是检查null还是检查size()==0取决于您实现的逻辑:)

  4. 如果为空,则显示Toast没有可玩的玩家。否则,随心所欲。


代码:

load.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {

        List<PlayerData> contacts = db.getAllContacts();
        //check to ensure there are users
        if(contacts == null || contacts.size() == 0){
            // do something.
        }else{
            // start the game
            PlayerData player = contacts.get(0);
            String showName = player.getName();
            int showScore = player.getscore();
            Log.d("Getting:", showName);
            Log.d("Getting:", Integer.toString(showScore));
            saved.setText("Player Name: "+showName+" Player Score: "+showScore);
        }
    } 
});
于 2013-09-01T03:25:14.677 回答