How to avoid force close if there is no data to fetch in db
I'm having some problem in avoiding force stop of my application. When I
click my load button it is supposed to load all data present in my
database. However, if there is no data present in my database, I want to
display a toast saying that there is no data to fetch in the db.
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);
}
});
No comments:
Post a Comment