带有微调器和复选框的 Android 列表视图
我是android开发的新手。我正在尝试创建一个列表,其中有一个微调器、一个编辑文本和一个复选框。微调器和复选框的数据来自数据库。我有以下文件。
NewTransac class which extends ListActivity {
private PayDbAdapter mDbHelper;
private Spinner paySpinner;
private CheckBox mCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_transac_listview);
mDbHelper = new PayDbAdapter(this);
mDbHelper.open();
populatedata();
}
private void populatedata() {
paySpinner = (Spinner)findViewById(R.id.payerspinner);
mCheckBox = (CheckBox)findViewById(R.id.paidforcheckboxname);
Cursor mCursor = mDbHelper.fetchAllTransactionValue();
startManagingCursor(mCursor);
// Create an array to specify the fields we want to display in the list.
String[] from = new String[]{PayDbAdapter.KEY_NAME};
int[] to = new int[]{android.R.id.text1};
int[] cbto = new int[]{R.id.paidforcheckboxname};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, mCursor, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
paySpinner.setAdapter(adapter);
SimpleCursorAdapter cbAdapter =
new SimpleCursorAdapter(this, R.layout.show_new_transac_data, mCursor, from, cbto );
setListAdapter(cbAdapter);
}
列表视图 xml
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:textSize="14sp"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_friends"
android:textSize="14sp"
/>
<Button android:id="@+id/confirmpay"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal|center">
</Button>
列表视图填充 xml
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:text="@string/listSeparatorPay"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
/>
<Spinner android:id="@+id/payerspinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/selectpayer"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/paytext"
/>
<EditText android:id="@+id/payamount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
/>
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:text="@string/listSeparatorPayedFor"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
/>
<CheckBox android:id="@+id/paidforcheckboxname"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText android:id="@+id/paidforamount"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
问题
我根据数据库中的字段数量获得多个微调器、复选框和编辑文本。我发现我们无法像我为微调器设置的那样为复选框设置适配器。 我只需要获得一个带有一个编辑文本和多个复选框(数据库行总数)的微调器。请帮忙!
I am a newbie to android development. I am trying to create a List which has a spinner, a edit text and a check box. The data for spinner and check box come from data base. I have the following files.
NewTransac class which extends ListActivity {
private PayDbAdapter mDbHelper;
private Spinner paySpinner;
private CheckBox mCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_transac_listview);
mDbHelper = new PayDbAdapter(this);
mDbHelper.open();
populatedata();
}
private void populatedata() {
paySpinner = (Spinner)findViewById(R.id.payerspinner);
mCheckBox = (CheckBox)findViewById(R.id.paidforcheckboxname);
Cursor mCursor = mDbHelper.fetchAllTransactionValue();
startManagingCursor(mCursor);
// Create an array to specify the fields we want to display in the list.
String[] from = new String[]{PayDbAdapter.KEY_NAME};
int[] to = new int[]{android.R.id.text1};
int[] cbto = new int[]{R.id.paidforcheckboxname};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, mCursor, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
paySpinner.setAdapter(adapter);
SimpleCursorAdapter cbAdapter =
new SimpleCursorAdapter(this, R.layout.show_new_transac_data, mCursor, from, cbto );
setListAdapter(cbAdapter);
}
The list view xml
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:textSize="14sp"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_friends"
android:textSize="14sp"
/>
<Button android:id="@+id/confirmpay"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal|center">
</Button>
list view filled xml
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:text="@string/listSeparatorPay"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
/>
<Spinner android:id="@+id/payerspinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/selectpayer"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/paytext"
/>
<EditText android:id="@+id/payamount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
/>
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:text="@string/listSeparatorPayedFor"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
/>
<CheckBox android:id="@+id/paidforcheckboxname"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText android:id="@+id/paidforamount"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
Problem
I get multiple spinners, checkboxes and edittext based on the number of fields in the database. I see that we cannot set the adapter for the checkbox as i set for the spinner.
I need to get only one spinner with one edit text and multiple checkboxes(total number of database rows). please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑 - 请参阅评论,这个解决方案可能不正确
我知道这个问题很古老,但这是 Google 上的第一个结果,我正在开发一个在 ListView 中使用 Spinners 的应用程序。我使用了此处中的一些示例代码来开始。我希望这个例子能回答你的问题。我没有实现复选框,但它们与 Spinner 非常相似 - 事实上,更容易。此示例有一个带有 TextView 和 Spinner 的 ListView。每当用户更改微调器中的选择时,TextView 都会发生更改以反映这一情况。
我将该项目分为 3 个类:
我还修改/创建了 3 个关键的 Android XML 文件:
strings.xml -修改 从下往上看,这个 main.xml 文件只包含一个 ListView,没有其他内容:
这是 rowview.xml。请记住,此视图对于 ListView 中的每一行都是重复的:
strings.xml 文件。我添加的只是一个用于旋转器内容的数组:
现在来看看有趣的东西。 ListViewActivity 类:
非常简单,您只需获取列表,创建一个新适配器,然后将 ListView 的适配器设置为您创建的适配器。这是 DataHolder 类:
DataHolder 类所做的全部工作就是保存 Spinner 的适配器以及您可能想要为 ListView 中的每个条目存储的任何其他信息(例如,您可能想要存储它是否被选中)。最后是应用程序的真正“核心”,即 DataAdapter 类:
这是最终应用程序的屏幕截图(不是很漂亮,但确实有效):
就是这样!我希望我回答了你的问题并帮助了像我一样偶然发现这个问题的其他人。如果要动态更改列表中的数据,请使用 DataAdapter 的
add()
、remove()
、get()
和 <代码>set() 方法。 要更改每个单独微调器的数据,您需要修改 DataHolder 类。 SpinnerAdapter 是在那里创建的,因此您只需根据数据库响应动态生成适配器即可。EDIT - please see comments, this solution may not be correct
I know this question ancient, but it is the first result on Google and I am working on an application that uses Spinners in a ListView as well. I used some sample code from here to get started. I hope this example answers your question. I didn't implement the CheckBoxes but they're very similar to the Spinner - much easier, in fact. This example has a ListView with a TextView and a Spinner. Whenever the user changes a selection in the spinner, the TextView changes to reflect this.
I divided this project up into 3 classes:
There are also 3 key Android XML files I modified / created:
To start from the bottom up, this main.xml file only contains a single ListView, and nothing else:
And here is the rowview.xml. Remember that this view is duplicated for each row in the ListView:
The strings.xml file. All I added was an array for the contents of the spinner:
Now for the fun stuff. The ListViewActivity class:
It's pretty simple, you just get the list, make a new adapter, and set the ListView's adapter to the one you made. This is the DataHolder class:
All the DataHolder class does is hold the Spinner's adapter and whatever other information you might want to store for each entry in the ListView (you may want to store whether it is checked or not, for example). And finally the real "meat" of the app, the DataAdapter class:
Here's a screenshot of the final app (it's not very pretty, but it does work):
And that's it! I hope I answered your question and helped anyone else who stumbled upon it like I did. If you want to dynamically change the data in the list, use the DataAdapter's
add()
,remove()
,get()
, andset()
methods. To change the data for each individual spinner, you need to modify the DataHolder class. The SpinnerAdapter is created there, so you just need to dynamically generate the adapters depending on the database response.为了使类似的事情起作用,必须将所有数据存储在适配器中,而不是存储在
ListView
项中。任何表示ListView
项的视图都可以重复使用来显示另一个项,从而使存储在其自身中的任何数据变得无关紧要,直到在getView
中再次设置正确的数据为止。In order to make things like that work one has to store all data in adapter, not in
ListView
items. Any view that represents aListView
item could be reused to display another item, making any data stored in itself irrelevant until the proper data is set again ingetView
.