gpt4 book ai didi

android - 无法从sqlite中检索并分配给android中的ListView

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:08 27 4
gpt4 key购买 nike

我正在开发一个简单的应用程序,用户将只添加他 friend 的名字,这个名字将存储在 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()用于在数据库中添加 friend 和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 中添加一个 friend ,但是当我重新启动应用程序时列表是空的,我想要 ListView应该显示添加到 SQLite Database 的记录,请告诉如何做到这一点。谢谢...

最佳答案

在您的 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();
}

当您插入新数据以刷新/更新 ListView 时,在您的适配器上调用 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().

关于android - 无法从sqlite中检索并分配给android中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17357882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com