Java Android:点击按钮获取微调器的Textview值
我有一个Java Android:点击按钮获取微调器的Textview值,java,android,android-spinner,Java,Android,Android Spinner,我有一个微调器,我正在用自定义SimpleCursorAdapter填充它。微调器项目布局包含两个TextViews,一个TextView用于项目id,另一个不可见,用于项目名称。我想在ButtonClick事件中获取此项id,然后将其插入Sqlite数据库。我在微调器的setOnItemSelectedListener上获取id,如下所示 companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListene
微调器
,我正在用自定义SimpleCursorAdapter
填充它。微调器项目布局包含两个TextView
s,一个TextView用于项目id,另一个不可见,用于项目名称。我想在ButtonClick事件中获取此项id,然后将其插入Sqlite数据库。我在微调器的setOnItemSelectedListener
上获取id,如下所示
companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Get selected row data to show on screen
String companyId = ((TextView) view.findViewById(R.id.spinnerItemIdTv)).getText().toString();
Toast.makeText(getActivity(), companyId, Toast.LENGTH_LONG).show();
Log.w(TAG, "companyId:" + companyId);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
companySpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
//获取要在屏幕上显示的选定行数据
字符串companyId=((TextView)view.findViewById(R.id.spinnerItemIdTv)).getText().toString();
Toast.makeText(getActivity(),companyId,Toast.LENGTH_LONG).show();
Log.w(标签“companyId:+companyId”);
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
和微调器项布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/spinnerItemIdTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#888"
android:textSize="20sp"
android:visibility="gone"/>
<TextView
android:id="@+id/spinnerItemNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#888"
android:textSize="20sp" />
</LinearLayout>
但在点击按钮时无法完成。任何帮助都将不胜感激。将您的xml
修改为singleTextView
,并使用tag
作为ID
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/spinnerItemNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="yourId"
android:textColor="#888"
android:textSize="20sp" />
</LinearLayout>
您有一个与微调器关联的适配器,我想您有一些与适配器关联的数据结构。您可以选择需要的数据(如下所示)
public void未选中(AdapterView AdapterView、视图视图、int i、long l){
myData=myArrayList.get(i);
//或
myData=myAdapter.getItem(i);
}
“我的数据”可以是“活动”字段,您可以稍后使用与按钮关联的单击回调。我想您正在搜索此字段
View selectedView = null; //Declare it as a class level variable so that you don't need to make it final
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedView = view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
你可以试试这个
companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinComp = (Spinner) view.findViewById(R.id.spinner_company);
int selCompIndex = spinComp .getSelectedItemPosition();
String compID = companyList.get(selCompIndex).toString();
Toast.makeText(getActivity(), compID, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
companySpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Spinner spinComp=(Spinner)view.findViewById(R.id.Spinner\u company);
int selCompIndex=spinComp.getSelectedItemPosition();
字符串compID=companyList.get(selCompIndex.toString();
Toast.makeText(getActivity(),compID,Toast.LENGTH_LONG).show();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
其中,companyList
是要传递给微调器适配器的ArrayList。希望这能起作用。为什么不在textview中使用标记,而不是在ID中使用单独的textview?您指的是单击哪个按钮?你能说清楚吗?你说的标签是什么意思?我是一个机器人noob@ShreeKrishna假设我在一个活动中有一个表单。有一个我上面提到的微调器,编辑文本等和一个按钮。有意义吗?你能发布你的xml代码吗?兄弟,我想你没有读过我的问题。我已经通过使用标签或不使用标签来完成这项工作。这不是重点。重点是获取按钮点击事件的值。不,我不能,因为我已经能够完成您发布的内容。谢谢。@Shree Krishna给出的解决方案&将标签与单文本视图结合使用将更加灵活。谢谢。但是,您认为有更好的解决方案来实现像HTML下拉列表这样的键值微调器吗?@Tartar Bro很抱歉,我不太熟悉自定义下拉列表或HTML下拉列表。另外需要注意的是,您可以在onNothingSelected
中将selectedView
再次设置为null。我理解,谢谢您的回答,这真的很有帮助。
View selectedView = null; //Declare it as a class level variable so that you don't need to make it final
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedView = view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedView!=null){
String companyId = ((TextView) selectedView.findViewById(R.id.spinnerItemIdTv)).getText().toString();
}
else{//Something}
}
});
companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinComp = (Spinner) view.findViewById(R.id.spinner_company);
int selCompIndex = spinComp .getSelectedItemPosition();
String compID = companyList.get(selCompIndex).toString();
Toast.makeText(getActivity(), compID, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});