i want to update textview when spinner item selection in adapter class but it show updation in wrong item's price . i have lost many time to solve this here is my code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_dropdown_item_1line, arraylist.get(position).getProductUnit1());
// Setting the array adapter containing country list to the spinner widget
holder.tvproductBrandName.setAdapter(adapter);
OnItemSelectedListener countrySelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position2, long id) {
i =Float.parseFloat(arraylist.get(position).getProductMRP1()[position2]);
holder.tvProductMRP.setText((arraylist.get(position).getProductMRP1()[position2]));
holder.tvProductOriginalPrice
.setText("Rs: "
+ ((i - Float.valueOf(arraylist.get(position)
.getOfferValue()))*(Float.valueOf(arraylist.get(position).getProductQTY()))) + "");
OriginaiPrice =String.valueOf (i - Float.valueOf(arraylist.get(position)
.getOfferValue()));
appSession.setProductOriginal(OriginaiPrice);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
// Setting ItemClick Handler for Spinner Widget
holder.tvproductBrandName.setOnItemSelectedListener(countrySelectedListener);
you can use hash map to check which position or value in spinner is selected and second hashMap to check textView value correspondence to spinner.
private final HashMap<Integer, Integer> selectedSpinnerItems;
private final HashMap<Integer, String> textViewValue;
Related
I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);
I have this private method to render a spinner in a fragment:
private void renderSpinner() {
List<String> spinnerArray = new ArrayList<String>();
for (int i = 0; i<mPromotionList.size(); i++) {
ModelPromotion modelPromotion = (ModelPromotion) mPromotionList.get(i);
String name_promotion = modelPromotion.getName();
int id_promotion = modelPromotion.getIdPromotion();
spinnerArray.add(name_promotion);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mBaseApp, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerPromotion.setAdapter(adapter);
mSpinnerPromotion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String name_promotion_selected = parent.getItemAtPosition(pos).toString();
//TODO REMOVE
Log.d(LOGTAG, "Il nome della promo selezionata รจ " + name_promotion_selected);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
As you can see, I have "ready" the id_promotion (It's the real value that I need)... And I know that I'm not adding (for now) at the List.
How I can get it, inside the onItemSelected?
Thank you very much
You can use the position of the selected item and use that to get the ID from the original list.
((ModelPromotion) mPromotionList.get(pos)).getIdPromotion()
Another option is to create a custom ArrayAdapter subclass that will accept ModelPromotion instead of String and override getItemId to return the idPromotion
An example about custom ArrayAdapter on Github.
Spinner item initially text value should be empty so i added empty vaue in spinner item but it shows the first spinner item is empty .please rectify my mistake which i have done
Required
Initially spinner should be empty
when selected spinner it should show only two items
spinner.java
LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add("");
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);
You have to create a costum SpinnerAdapter to achieve this.
Try this
You can set the first spinneritem empty: " ". But in this simple solution the user is still able to select the displayed empty spinneritem.
You can add a kind of "title" into your spinner and ignore it if the user select it. It's not exactly an empty line but the title will act as an hint to guide the user.
private static final String TITLE ="Select the action";
...
ArrayList<String> LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add(TITLE);
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);
spnrLognStatus.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selection = (String)spnrLognStatus.getSelectedItem();
if(TITLE.equalsIgnoreCase(selection)){
// Ignore the selected title
}else{
// Do want you want here
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
My activity contains a button and a ListView. The ListView contains a Spinner and an EditText view. I use the button each time I want to insert a new row entry in my Activity's ListView.
I have followed the instructions of previous stackoverflow threads like this one here: Android Listview with spinner and a checkbox on how to populate ListViews with focusable objects like Spinners.
My problem is that each time I dynamically add a new ListView entry in the ListView, the Spinner value of the previous ListView entry is lost (actuall the Spinner returns to its default setting). Say for simplicity that my Spinners are populated with the following data:
String spinner_data[] = {"apple", "banana", "pear", "watermelon", "strawberry"};
For example, if I select my first ListView's Spinner value to be "pear" and then I add a new ListView entry with my Button, the "pear" entry disappears from the 1st ListView Spinner and the default value "apple" appears).
Any help is appreciated!
This is my activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
da = new DataAdapter(this, new ArrayList<RawData>());
lv.setAdapter(da);
btn_new = (Button)findViewById(R.id.btn_new);
btn_new.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
da.add(new RawData(this));
da.notifyDataSetChanged();
}
});
}
The RawData class is this one:
public class RawData {
private int selected_position;
private ArrayAdapter<CharSequence> adapter;
public RawData(Context context)
{
adapter = ArrayAdapter.createFromResource(context, R.array.data, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter()
{
return adapter;
}
/**
* get adapter's item text from selected position
* #return
*/
public String getText()
{
return (String) adapter.getItem(selected_position);
}
public int getSelected()
{
return selected_position;
}
public void setSelected(int selected)
{
this.selected_position = selected;
}
}
The DataArrayAdapter is the following:
public class DataArrayAdapter extends ArrayAdapter<RawData> {
private Activity myContext;
//private final List<RawData> list;
public DataArrayAdapter(Activity context, List<RawData> list)
{
super(context, R.layout.row_view, list);
myContext = context;
}
static class ViewHolder
{
protected RawData data;
protected Spinner spn;
protected EditText edt;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if ( convertView == null )
{
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.row_view, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.edt = (EditText)view.findViewById(R.id.edt);
viewHolder.data = new RawData(myContext);
viewHolder.spn = (Spinner)view.findViewById(R.id.spn);
viewHolder.spn.setAdapter(viewHolder.data.getAdapter());
viewHolder.spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2_position, long arg3) {
// TODO Auto-generated method stub
viewHolder.data.setSelected(arg2_position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// Update the TextView to reflect what's in the Spinner
view.setTag(viewHolder);
}
else
{
view = convertView;
}
// This is what gets called every time the ListView refreshes
ViewHolder holder = (ViewHolder) view.getTag();
holder.spn.setSelection(getItem(position).getSelected());
return view;
}
}
You're not handling the situation when getView gets a non-null convertView. In your example, after you add an item, ListView refreshes itself, and position that should display 'pear' gets an existing convertView (the one that was used previously to display 'apple') and you just pass it along to ListView without setting the data for current position. You cannot rely on ListView items to store any data, you should always set correct contents for position in getView method of your adapter.
Just to be clear, I see that your code sets the selected position in the end of getView but the issue is that whatever is tagged to your convertView when it is passed to getView by recycler mechanism in ListView is random and can be associated with any position it used to display before.
To make your application work you'll have to create array of selectedItem values for all your spinners, and attach it as a member to your adapter. You'll have to update the corresponding value on each OnItemSelected event and you'll add a new value for each "add" button click. And when you prepare your view in getView you'll just set the selected spinners index to corresponding value in your array.
I'm developing a Listview dynamically filled by a function that returns an ArrayAdapter which recieves a String parameter. This string parameter is the selected item of another dynamically filled Spinner.
When the function returns an ArrayAdapter with a number of items > 0, the Listview is sucessfully refreshed with the new items, but when the function returns 0 items on the ArrayAdapter, the listview doesn't clear the previous items. Here's the code I'm working on:
ManifiestoSpinner = (Spinner) findViewById(R.id.spnManifiesto);
FacturasListview = (ListView) findViewById(R.id.lvwFacturas);
ManifiestoSpinner = (Spinner)
ManifiestoSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String EstadoID = EstadoSpinner.getSelectedItem().toString();
ArrayList<String> ListManifiestos = (ArrayList<String>)Factura.GetManifiestosByEstado(EstadoID);
ActualizarManifiestSpinner(manifiesto);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
private void ActualizarManifiestSpinner (ArrayList<String> manifiesto)
{
String[] datos = new String[manifiesto.size()];
ArrayAdapter<String> AdapterManifiesto = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item, manifiesto.toArray(datos));
ManifiestoSpinner.setAdapter(AdapterManifiesto);
}
ManifiestoSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String Manifiesto = ManifiestoSpinner.getSelectedItem().toString();
String Estado = EstadoSpinner.getSelectedItem().toString();
fillData(Estado, Manifiesto);
return;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
private void fillData(String EstadoID, String ManifiestoID) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) FacturasListview.getAdapter();
if(adapter!= null)
{
adapter.clear();
FacturasListview.setAdapter(adapter);
FacturasListview.invalidateViews();
}
List<String> from = Factura.GetListClientesByEstadoManifiesto(EstadoID, ManifiestoID);
adapter = new ArrayAdapter<String>(this,R.layout.factura_row, R.id.text1 ,from);
FacturasListview.setAdapter(adapter);
}
When the List called from has from.size() = 0, the items previously show on the Listview are not cleared.
Thanks Nammari. I followed your recommendation but the FacturasListview doesn't have a method called notifyDatasetChanged(), the adapter does, so i got this code:
ArrayAdapter<String> adapter = (ArrayAdapter<String>) mFacturasListview.getAdapter();
if(adapter!= null)
{
adapter.clear();
adapter.notifyDataSetChanged();
}
Still the Listview won't refresh the items. I'm looking for a method that will clear all the items of a ListView that recieves an ArrayAdapter. I'm uploading two pictures to show what I'm working on.
"Imagen 1"
In this case, the first spinner has a selecteditem().toString() = "POR RECIBIR", and based on that parameter, the second spinner is dynamically filled with the codes of all the documents having (document.State = "POR RECIBIR"). After the second spinner is filled, a third method called "fillData" fills the ListView with all the Customer's name included on the document. This is the code:
private void fillData(String EstadoID, String ManifiestoID) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) mFacturasListview.getAdapter();
if(adapter!= null)
{
adapter.clear();
adapter.notifyDataSetChanged();
}
List<String> from = Factura.GetListClientesByEstadoManifiesto(EstadoID, ManifiestoID);
adapter = new ArrayAdapter<String>(this,R.layout.factura_row, R.id.text1 ,from);
FacturasListview.setAdapter(adapter);
}
The problem occurs when the first spinner has a selecteditem().toString() = "POR ENTREGAR", and based on that parameter, we find 0 documents having (document.State = "POR ENTREGAR"). Here, the third method called "fillData" should clear all ListView items, but it doesn't. This is the image:
"Imagen 2"
In your fillData method
if(adapter!= null)
{
adapter.clear();
FacturasListview.setAdapter(adapter);// !!!!!!!
FacturasListview.invalidateViews();
}
after calling adapter.clear(); setting the adapter of the ListView won't change anything
just call FacturasListview.notifyDatasetChanged();
like this
if(adapter!= null)
{
adapter.clear();
FacturasListview.notifyDatasetChanged();
}
this should clear all views in the list
in case you want to change the adapter of ListView i would make a custom array adapter with a method that takes the List and change the previous reference of the List in the adapter and after that call notifyDatasetChanged();
like this
class MyAdpater {
List<String> list;
.....
.....
public void setAdpaterList(List<String>list){
this.list= list;
}
}
then after calling this method call notifyDatasetChange() on your adapter