I have a Spinner
and i am populating it with custom SimpleCursorAdapter
. Spinner item layout contains two TextView
s, One TextView for item id and it is not visible other is for item name. I want to get this item id on button click event then insert it to Sqlite database. I get the id on setOnItemSelectedListener
of Spinner
as
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) {
}
});
and Spinner Item Layout
<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>
But couldnt make it on button click. Any help would be appreciated.
I think you are searching for this
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) {
}
});
And Inside some Button's click event, do like this
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}
}
});
You have adapter associated with your Spinner and I suppose you have some data structure associated with Adapter. You can get data you need onItemSelected (something like below)
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
myData = myArrayList.get(i);
//OR
myData = myAdapter.getItem(i);
}
myData can be Activity field and you can use later onClick callback assosiated with your button.
You may try this
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) {
}
});
Where, companyList
is ArrayList which you are passing to Spinner Adapter. Hope this works.
Modify your xml
to single TextView
and use tag
for 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>
Now use setText
to set the spinner row value and use setTag
to set the ID of row and then get the ID like this.
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) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.