Clear all List in spinner items - android

I have try this code but one array list will be clear but second data not clear please give me solution in 1st spinner clear spinner 2 data but spinner 2 cant clear 3rd spinner data please tell me solution
my array list
private ArrayList<String> students;
private ArrayList<String> teachers;
private ArrayList<String> CastName;
private ArrayList<String> SubCastName;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerreligiontext.setText(getNames(position));
stst = spinnerreligiontext.getText().toString();
System.out.println(getNames(position));
SubCastName.clear();
CastName.clear();
CastName();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
aaa = getNamess(position);
System.out.println("spin2"+getNamess(position));
SubCastName.clear();
SubCastName();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}); spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
aaa = getSubNamess(position);
System.out.println("spin3"+getSubNamess(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
set adapter code
for (int i = 0; i < j.length(); i++) {
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
students.add(json.getString("AllInOne"));
} catch (JSONException e) {
e.printStackTrace();
}
}
spinner1.setAdapter(new ArrayAdapter<String>(RagistarActivity.this, android.R.layout.simple_spinner_dropdown_item, teachers));

Since you are using four array to populate the spinner you can clear it by .clear() method, so
students.clear();
teachers.clear();
CastName.clear();
SubCastName.clear();
else you can set the adapter to null at the point were you need to clear spinner items

Related

Mapping items in list view to number of clicks

I have a ListView populated with a custom Array adapter. I want to get the total number of clicks on each item in the ListView .How can i map the listview item id and number of clicks on that item for multiple items?
Use this code.. exact.. hardcoded the whole code for you :p
HashMap<Integer,Integer> map_positions_and_count = new HashMap<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i<listView.getAdapter().getCount()-1; i++)
{
if (i == position)
{
if (map_positions_and_count .get(position) !=null)
{
map_positions_and_count .put(position,map_positions_and_count .get(position)+1);
}
else {
map_positions_and_count .put(position,1);
}
}
}
// map.put(position,)
}
});
You can use this in your mainActivity ..
You can try this in your activity,
ArrayList<Integer> arrayList=new ArrayList<>();//make this as global
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position)!=null) {
int a = arrayList.get(position);
arrayList.set(position, a+1);
}
else
arrayList.set(position,1);
}
});
Try this code ->
final HashMap<Integer, Integer> listItemCount = new HashMap<Integer, Integer>();
//Set an onItemClickListener
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listItemCount.containsKey(position)){
listItemCount.put(position, listItemCount.get(position)+1);
}else {
listItemCount.put(position, 1);
}
}
});

Invoking a Spinner onitemselect listener

I'm really close to getting my spinner to do something when an item is selected. I just can't figure out how to set up the onitemselectedlistener correctly. I think my problem is not knowing how to pass the arguments to my onItemSelected method. Help to get the final part of this working is very much appreciated.
Here is the code:
public class MainActivity extends AppCompatActivity {
Spinner spinner;
String[][] testScoreList = new String[3][3];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load test scores into arraylist
nameArrayListMethod();
spinner.setOnItemSelectedListener(onItemSelected());
}
//This method loads test scores into an array and populates spinner
public void nameArrayListMethod (){
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
AssetManager am = getAssets();
String[] SpinnerNames = new String[3];
try {
InputSR = new InputStreamReader(am.open("scoresdata/test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
//System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
testScoreList[i][2] = parts[2];
SpinnerNames[i] = testScoreList[i][0]; //Extract first column for spinner
i = i +1;
}
BufferedRdr.close();
InputSR.close();
} catch (Exception e) {
e.printStackTrace();
}
spinner= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, SpinnerNames);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
}
You can add data Tow ways
Method 1
if you have a simple one line data the use this method.
step 1
Add string array in string.xml file
<string-array name="ArrayName">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
</string-array>
add android:entries="#string/ArrayName" in your xml spinner component
and get selected value using String text = spinner.getSelectedItem().toString();
Spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText(topThis, "Value = "+spinner.getSelectedItem().toString();, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
Method 2
for using custom adapter HERE is complete tutorial
It looks like:
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
});
Try this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Update Spinner from another spinner dynamically

I have a fragment that creates controls at runtime, first create a spinner and then the second, the detail is in the second spinner depends on having the first selection.
As I can update the data of the second spinner depending on the selection of the first?
final Spinner Combo2 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
final Spinner Combo1 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list = new ArrayList<String>();
list.add("TERRESTRE");
list.add("FLUVIAL");
list.add("AEREO");
final ArrayAdapter<String> adapterCombo = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item,list);
List<String> list2 = new ArrayList<String>();
list2.add("OP1");
list2.add("OP2");
final ArrayAdapter<String> adapterCombo2 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list2);
if(listFormato.get(posicion).cSeccion.trim().equals("M"))
{
adapterCombo.setDropDownViewResource(R.layout.spinner);
Combo1.setAdapter(adapterCombo);
Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo1);
Combo1.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
}
else if(listFormato.get(posicion).cSeccion.trim().equals("U")){
final Spinner Combo0 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list3 =new ArrayList<String>();
list3.add("SI");
list3.add("NO");
ArrayAdapter<String> adapterCombo3 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list3);
adapterCombo3.setDropDownViewResource(R.layout.spinner);
Combo0.setAdapter(adapterCombo3);
llTempZZ.addView(Combo0);
llTempXX.addView(llTempZZ);
//llTempXX.addView(llTempAA);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
Combo0.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if (pos == 0) {
pll2.setVisibility(View.VISIBLE);
}
else if(pos==1) {
pll2.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo0.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
else if(listFormato.get(posicion).cSeccion.trim().equals("T"))
{
adapterCombo2.setDropDownViewResource(R.layout.spinner);
Combo2.setAdapter(adapterCombo2);
//Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo2);
Combo2.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
//////TEXTOS
Combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if(Combo2.getSelectedItemPosition()==0)
{
}
if (pos ==12) {
llTempAA.setVisibility(View.VISIBLE);
} else {
llTempAA.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo2.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
You can do something like this
Spinner firstSpinner = (Spinner) findViewById(R.id.first_spinner);
// You can set your first spinner values here if not set already, and then
firstSpinner.setOnItemSelectedListener(onFirstSpinnerChange(firstSpinner));
And the further add this
AdapterView.OnItemSelectedListener onFirstSpinnerChange(Spinner mySpinner) {
return new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// You can add this method to set the values of the second spinner in your activity
// You can use the position, or alternatively selectedItemView for this purpose to identify the selected item
setSecondSpinnerBasedOnFirstOnePositionSelected();
// Or you can just set your spinner in this block
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
};
}

How can I pick only once a value from spinner in android

I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable

Spinner return null value

when i am selected spinner value then some time it return a true value and some time it return null value
My spinner code is
list.add("Please Select Approver Name");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setSelection(0);
Item Selected Listener
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
I am working on it past two day
Please Help Me how i can fix this problem
Use the Toast inside the onITemSelected method as below.
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Categories

Resources