SQLite 更新时更新微调器

发布于 2024-11-07 00:25:46 字数 1090 浏览 2 评论 0原文

我有一个应用程序,用户可以在 EditText 字段中输入艺术家的姓名,并将其添加到 sqlite 表中。这很好用,但我在相同的活动/布局上也有一个微调器,我想用这些新的更改来更新它。我的设置方式如下:

我有一个 sqlite 类,它将插入新名称和方法,该名称和方法将查询表并返回“ArtistList”数组。

在我的活动中,我可以在首次创建时使用数组适配器将 ArtistList 放入微调器中。

用户在 EditText 中输入新名称,更新 sqlite 表,成功...

我尝试通过运行 sqlite 方法 ArtistList 来重新加载 ArrayList 和 ArrayAdapter 但微调器仍然没有更新,它仍然具有之前相同的旧列表更新。我知道我可以执行artistList.add(),但这会使列表乱序。我希望按字母顺序排列此列表,这就是为什么我从 sqlite 类中检索 ArrayList,sqlite 正在为我组织数据。

我尝试使用 setNotifyOnChange,我尝试了一个新的(和清除的)数组列表和适配器......没有任何效果。

这是在Create上运行的:

sqlclass helper = new sqlclass(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        artistList = helper.getArtistList(db, tableName);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, artistList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.setNotifyOnChange(true);

感谢您的帮助!

没关系... 我最终制作了一个新的适配器......并添加了 spinner.setAdapter(适配器)。不确定它是否应该在那里,因为微调器在更新之前一直显示查找,但它现在正在工作......

I have an application where a user can enter in a EditText field an artist's name and it will be added to the sqlite table. That works great, but I also have a spinner on the same activity/layout that I want to update with these new changes. Here's how I have it setup:

I have a sqlite class that will insert the new name and method that will query the table and return an 'ArtistList' array.

In my activity, I'm able to get the ArtistList into the spinner using an Array Adapter when first created.

The user enters a new name in the EditText, updates the sqlite table, success...

I tried to reload the ArrayList and ArrayAdapter by running the sqlite method ArtistList but the spinner still doesn't update, it still has the same old list before the update. I know I can just doa artistList.add(), but that will make the list out of order. I would like to have this list in Alphabetical order which is why I'm retrieving the ArrayList from the sqlite class, the sqlite is organizing the data for me.

I tried using setNotifyOnChange, I tried a new (and clearing) arraylist and adapter...nothing is working.

This is ran onCreate:

sqlclass helper = new sqlclass(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        artistList = helper.getArtistList(db, tableName);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, artistList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.setNotifyOnChange(true);

Thanks for your help!

Nevermind...
I ended up making a new adapter...and added spinner.setAdapter(adapter). Not sure if that was supposed to be there because the spinner was displaying find until the update, but it's working now...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2024-11-14 00:25:46

我对此含糊表示歉意,但请考虑使用 CursorAdapter ...我认为它将允许您重新查询和更新数据集。

I apologize for this being vague but look into using a CursorAdapter ... I think it will allow you to re-query and update the data set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文