Android: 获取按钮点击时 Spinner 的 Textview 值
Android: Get Textview value of Spinner on Button click
我有一个 Spinner
,我正在用自定义 SimpleCursorAdapter
填充它。微调项目布局包含两个 TextView
,一个 TextView 用于项目 ID,它不可见,另一个用于项目名称。我想在按钮单击事件中获取此项目 ID,然后将其插入到 Sqlite 数据库中。我在 Spinner
的 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) {
}
});
和微调项布局
<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
为单人TextView
并使用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>
现在使用 setText
设置微调器行值并使用 setTag
设置行的 ID,然后像这样获取 ID。
companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String companyId = ((TextView) view.findViewById(R.id.spinnerItemNameTv)).getTag().toString(); //use parent instead.
Toast.makeText(getActivity(), companyId, Toast.LENGTH_LONG).show();
Log.w(TAG, "companyId:" + companyId);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
您有与微调器相关联的适配器,我想您有一些与适配器相关联的数据结构。您可以在 ItemSelected 上获取所需的数据(如下所示)
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
myData = myArrayList.get(i);
//OR
myData = myAdapter.getItem(i);
}
myData 可以是 Activity 字段,您稍后可以使用与您的按钮关联的 onClick 回调。
我想你正在搜索这个
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) {
}
});
在某些 Button 的点击事件中,这样做
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) {
}
});
其中,companyList
是您要传递给 Spinner Adapter 的 ArrayList。希望这有效。
我有一个 Spinner
,我正在用自定义 SimpleCursorAdapter
填充它。微调项目布局包含两个 TextView
,一个 TextView 用于项目 ID,它不可见,另一个用于项目名称。我想在按钮单击事件中获取此项目 ID,然后将其插入到 Sqlite 数据库中。我在 Spinner
的 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) {
}
});
和微调项布局
<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
为单人TextView
并使用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>
现在使用 setText
设置微调器行值并使用 setTag
设置行的 ID,然后像这样获取 ID。
companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String companyId = ((TextView) view.findViewById(R.id.spinnerItemNameTv)).getTag().toString(); //use parent instead.
Toast.makeText(getActivity(), companyId, Toast.LENGTH_LONG).show();
Log.w(TAG, "companyId:" + companyId);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
您有与微调器相关联的适配器,我想您有一些与适配器相关联的数据结构。您可以在 ItemSelected 上获取所需的数据(如下所示)
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
myData = myArrayList.get(i);
//OR
myData = myAdapter.getItem(i);
}
myData 可以是 Activity 字段,您稍后可以使用与您的按钮关联的 onClick 回调。
我想你正在搜索这个
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) {
}
});
在某些 Button 的点击事件中,这样做
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) {
}
});
其中,companyList
是您要传递给 Spinner Adapter 的 ArrayList。希望这有效。