我试图在我的加载按钮的 onclicklistener 中使用来自 sqlite 的数据填充我的列表视图。
但是,有些东西在我的代码中不起作用。这是我的代码。
public class TotalResults extends ListActivity {
DatabaseManager db = new DatabaseManager(this);
List<PlayerData> contacts;
String name;
int score;
TextView result;
TextView saved;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_total_results);
result = (TextView) findViewById (R.id.textResult);
saved = (TextView) findViewById (R.id.textLoad);
Button save = (Button) findViewById (R.id.buttonSave);
Button load = (Button) findViewById (R.id.buttonLoad);
Button delete = (Button) findViewById (R.id.buttonDelete);
contacts = db.getAllContacts();
final ArrayAdapter<PlayerData> adapter = new ArrayAdapter<PlayerData>(this,
android.R.layout.simple_list_item_1, contacts);
setListAdapter(adapter);
Intent intent = getIntent();
name = intent.getExtras().getString("name");
score = intent.getExtras().getInt("score");
result.setText("Name: "+name+" , Score: "+score );
save.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
db.addContact(new PlayerData(name,score));
Log.d("Inserting: ", name);
Log.d("Inserting: ", Integer.toString(score));
}
});
load.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//check to ensure there are users
if(contacts.size()==0) {
String text = "No players stored.";
Toast.makeText(TotalResults.this, 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));
adapter.add(player);
saved.setText("Player Name: "+showName+" Player Score: "+showScore);
}
}
});
delete.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 players to delete.";
Toast.makeText(TotalResults.this, text, Toast.LENGTH_SHORT).show();
} else {
PlayerData player = contacts.get(0);
String showName = player.getName();
int showScore = player.getscore();
Log.d("Deleting:", showName);
Log.d("Deleting:", Integer.toString(showScore));
db.deleteContact(player);
}
}
});
}
我已经应用了 setListAdapter 的概念。但是,logcat 应该是我从数据库中获取名称和分数,但没有获取数据的迹象。但是插入的数据可以删除。加载按钮也不会在列表视图上显示任何内容。请帮忙。提前致谢。:)