2

我正在开发一个简单的应用程序,其中用户只会添加他朋友的名字,并且该名称将存储在这些名称中,SQLite database并且SQLite应该显示在ListView. 我已经这样做了:

public Friend addFriend(Friend friend) {

    ContentValues values = new ContentValues();

    values.put(FriendOpenHelper.COL_NAME, friend.getName());

    long insertId = database.insert(FriendOpenHelper.FRIEND_TABLE, null, values);
    friend.setId(insertId);

    return friend;

}

public List<Friend> findAll(){
    List<Friend> friends = new ArrayList<Friend>();

    Cursor cursor = database.query(FriendOpenHelper.FRIEND_TABLE, columns, null, null, null, null, null);

    Log.e(FriendOpenHelper.FRIEND_TAG, "Returned " + cursor.getCount() + " rows");

    if(cursor.getCount() > 0){
        while (cursor.moveToNext()) {
            Friend friend = new Friend();

            friend.setId(cursor.getLong(cursor.getColumnIndex(FriendOpenHelper.COL_ID)));
            friend.setName(cursor.getString(cursor.getColumnIndex(FriendOpenHelper.COL_NAME)));
        }
    }

    return friends;
} 

addFriend()用于在数据库中添加朋友并用于getAll()检索数据List<>,现在MainActivity我正在执行以下操作以从数据库中添加和填充列表,如下所示:

EDIT

    protected List<Friend> friends;
protected ArrayAdapter<Friend> adapter;
protected FriendDataSource dataSource = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    dataSource = new FriendDataSource(MainActivity.this);
    dataSource.dbOpen();
    ///////////////////////////////////////////////////////
    dataSource = new FriendDataSource(MainActivity.this); 
    dataSource.dbOpen(); 
    friends = dataSource.findAll(); 
    if(friends.size()>0) // check if list contains items. 
    { 
    ListView lv = (ListView)findViewById(R.id.listView1); 
    ArrayAdapter<Friend> arrayAdapter = new ArrayAdapter<Friend>(MainActivity.this,android.R.layout.simple_list_item_1, friends); 
    lv.setAdapter(arrayAdapter); 
    } 
    else 
    { 
    Toast.makeText(MainActivity.this,"No items to display",1000).show(); 
    }
    ///////////////////////////////////////////////////////
    /*friends = dataSource.findAll();

    adapter = new ArrayAdapter<Friend>(MainActivity.this, android.R.layout.simple_list_item_1, friends);
    adapter.notifyDataSetChanged();
    */View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(friends.size() == 0){
                insertData();
                friends = dataSource.findAll(); 
                adapter.notifyDataSetChanged();
                //adapter = new ArrayAdapter<Friend>(MainActivity.this, android.R.layout.simple_list_item_1, friends);                  
            }

        }
    };

    UIHelper.clickHelper(MainActivity.this, R.id.button1, listener);
    //UIHelper.listHelper(MainActivity.this, R.id.listView1, adapter);
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    dataSource.dbOpen();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    dataSource.dbClose();
}

protected void insertData() {
    // TODO Auto-generated method stub
    Friend friend = new Friend();

    String name = UIHelper.getText(MainActivity.this, R.id.editText1);

    friend.setName(name);
    friend = dataSource.addFriend(friend);
    Log.i(FriendOpenHelper.FRIEND_TAG, "Friend created with id " + friend.getId());

    UIHelper.clearText(MainActivity.this, R.id.editText1);
    adapter.notifyDataSetChanged();
    //adapter.add(friend);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Log Cat

06-28 11:33:05.363: E/AndroidRuntime(1670): FATAL EXCEPTION: main

06-28 11:33:05.363: E/AndroidRuntime(1670): java.lang.NullPointerException
06-28 11:33:05.363: E/AndroidRuntime(1670):     at som.example.insertadnretrievedata.MainActivity.insertData(MainActivity.java:94)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at som.example.insertadnretrievedata.MainActivity$1.onClick(MainActivity.java:56)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.view.View.performClick(View.java:4204)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.view.View$PerformClick.run(View.java:17355)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.os.Handler.handleCallback(Handler.java:725)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.os.Looper.loop(Looper.java:137)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at java.lang.reflect.Method.invoke(Method.java:511)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-28 11:33:05.363: E/AndroidRuntime(1670):     at dalvik.system.NativeStart.main(Native Method)

编辑

 `UIHelper.listHelper(MainActivity.this, R.id.listView1, adapter);` this the helper method and I'm assigning `adapter` as :


static public void listHelper(Activity activity, int id, ArrayAdapter<?> adapter){
    ListView listView = (ListView) activity.findViewById(id);
    listView.setAdapter(adapter);
}

此时我可以在数据库和我的 ListView 中添加一个朋友,但是当我重新启动应用程序时列表是空的,我希望它ListView应该显示添加到的记录SQLite Database,请告诉如何做到这一点. 谢谢...

4

1 回答 1

3

在你的OnCreate.

dataSource = new FriendDataSource(MainActivity.this);
dataSource.dbOpen();
friends = dataSource.findAll();
if(friends.size()>0) // check if list contains items.
{    
   ListView lv = (ListView)findViewById(R.id.listView1);
   ArrayAdapter<Friends> arrayAdapter = new ArrayAdapter<Friends>(MainActivity.this,android.R.layout.simple_list_item_1, friends);
   lv.setAdapter(arrayAdapter);
} 
else
{
   Toast.makeText(MainActivity.this,"No items to display",1000).show();
}

当您插入新数据以刷新/更新notifyDatasetChanged()适配器上的列表视图调用时。

 String name = ed1.getText().toString();
 Friend friend = new Friend();
 friend.setName(name);
 dataSource = new Handler(MainActivity.this); 
 friend = dataSource.addFriend(friend);
 Log.i("new data",friend.getName());
 friends.add(friend);
 arrayAdapter.notifyDataSetChanged().
于 2013-06-28T08:13:21.027 回答