无法使用sqlite从数据库中填充ListView

时间:2013-09-01 16:20:40

标签: android sqlite listview

我试图用我的加载按钮的onclicklistener中的sqlite数据填充我的listview。

然而,我的代码中有些东西不起作用。这是我的代码。

    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应该是我从数据库中获取名称和分数,但没有获取数据的迹象。但是可以删除插入的数据。加载按钮也不会在列表视图上显示任何内容。请帮忙。提前致谢。 :)

1 个答案:

答案 0 :(得分:0)

如果您的数据模型是sqlite db,最明显的方法是使用CursorAdapter或其子类之一(例如SimpleCursorAdapter),而不是ArrayAdapter