How to remove ""[], from list - android

I have
["A","B","C","D"]
I want to bind this to the android spinner. How can I remove []"", from ["A","B","C","D"] so only characters will display in the spinner.
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String asd = "";
arrayList3.clear();
asd= arrayList2.get(position);
String[] items = asd.split(",");
List<String> list = Arrays.asList(items);
for(String item : list)
{
arrayList3.add(item);
}
ArrayAdapter<String> dataAdapter1 =
new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, arrayList3);
sp2.setAdapter(dataAdapter1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

just replace this code with foreach loop in your code
for(String item : list)
{
item = item.replace("[","");
item = item.replace("]","");
item = item.replace("\"","");
arrayList3.add(item);
}

Related

how to invisible recyclerview item getting value from spinner

I want when user click on "قرآن" my program invisible else things mean tarjuma and tafsir,
same like when he clicks on "مع ترجمہ" my program visible Quran and tarjuma and hide the tafsir.
I am using spinner it works fine but after refresh my recyclerview but I want without refreshing the recyclerview, mean is that when the user selects any Spinner value recyclerview change the view.
refresh mean going to another activity.
anybody who help me!!
here is my RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final Quran quran = quranic.get(position);
if (Quran.spinner.equals("قرآن")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.reference.setText(quran.getReferences());
holder.tarjuma.setVisibility(View.INVISIBLE);
holder.tafsir.setVisibility(View.INVISIBLE);
} else if (Quran.spinner.equals("مع ترجمہ")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.reference.setText(quran.getReferences());
holder.tafsir.setVisibility(View.INVISIBLE);
} else {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.tafsir.setText(Html.fromHtml(quran.getTafsir()));
holder.reference.setText(quran.getReferences());
}
}
my MainActivity
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Quran.java
public static String spinner = "";
You just need to call notifyDataSetChanged(); on your recycler view adapter each time you select an item from your spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Even better if you keep a reference to your recycler view adapter, then replace:
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
by:
adapter.notifyDataSetChanged();
}
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
//add this line
yourAdapterObject.notifyDataSetChanged();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Clear all List in spinner items

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

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) {
}
});

Loading data to Chain of Spinners according to the previous selected value

I have 3 spinners in my activity and I'm loading data for these 3 spinners from json response.
1st spinner(crustSP) gets data straight forward from the json response.
2nd spinner's (SizeSP) data depends on what selected from 1st spinner and
3rd spinner's (extraDescriptionOneSP) data depends on what select from 2nd spinner.
My question is it crashes inside 2nd SP because I haven't selected any value inside that when activity starting (I have shown that inside the code as a comment), how can I overcome that? where should I have that piece of code. It goes on without crashing when I hard code the position of sizeSp.
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultDescription = getFilteredDescriptions(
crustSelectedItem, sizeDescription);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString(); // it crashes here since I haven't selected any value
List<String> resultTopping = getFilteredToppings(
sizeSelectedItem, topDescription);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultDescription);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
This is how I filter the items,
List<String> getFilteredDescriptions(String crustSelectedItem,
List<String> sizeDescription) {
List<String> resultDescription = new ArrayList<String>();
crustSelectedItem = crustSP.getSelectedItem().toString();
if (sizeDescription == null || sizeDescription.isEmpty())
return resultDescription;
for (int i = 0; i < sizeDescription.size(); i++) {
sizeDescription = Utils.removeDuplicatesFromList(sizeDescription);
if (!sizeDescription.get(i).contains(crustSelectedItem))
continue;
resultDescription.add(sizeDescription.get(i));
}
return resultDescription;
}
List<String> getFilteredToppings(String sizeSelectedItem,
List<String> topDescription) {
List<String> resultTopping = new ArrayList<String>();
sizeSelectedItem = sizeSP.getSelectedItem().toString();
if (topDescription == null || topDescription.isEmpty())
return resultTopping;
for (int i = 0; i < topDescription.size(); i++) {
topDescription = Utils.removeDuplicatesFromList(topDescription);
if (!topDescription.get(i).contains(sizeSelectedItem))
continue;
resultTopping.add(topDescription.get(i));
}
return resultTopping;
}
You have to call onItemSelectedListner inside the other one, Like,,
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Arraylist2.add((String)spinner1.getItemAtPosition(position));
//Some Code/Ligic here
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position2, long id) {
// TODO Auto-generated method stub
Arraylist3.add((String) spinner2.getItemAtPosition(position2));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub111
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Hope it helps...
just give it try..
put this code before setting onItemSelectedListner..
spin1.setSelection(0, false);

Android Dynamic ArrayAdapter Spinner

I have two spinners, A and B. The spinner options for B changes based on the option A selected.
How I am doing it:
final ObjA[] ObjAArray = // db call
SpinAdapter<ObjA> sObjAAdapter = new SpinAdapter<ObjA>(this, android.R.layout.simple_spinner_item, ObjAArray);
sObjA.setAdapter(sObjAAdapter);
sObjA.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// Here you get the current item (a User object) that is selected by its position
ObjA ObjA = ObjAArray[position];
final ObjB[] ObjBArray = // db call
SpinAdapter<ObjB> sObjBAdapter = new SpinAdapter<ObjB>(Activity.this,
android.R.layout.simple_spinner_item, ObjBArray);
sObjB.setAdapter(sObjBAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> adapter) { }
});
This works fine but I need to select A and B to a specific value in the beginning.
ObjB oldObjB = // db call
ObjA oldObjA = // db call
int ObjAIndexSpinner = Arrays.asList(ObjAArray).indexOf(oldObjA);
sObjA.setSelection(ObjAIndexSpinner);
final ObjB[] ObjBArray = DBObjB.getAllObjB(Activity.this, oldObjA.getID());
int ObjBIndexSpinner = Arrays.asList(ObjBArray).indexOf(oldObjB);
sObjB.setSelection(1);
The first spinner gets set properly but the second one doesn't. It defaults to the 0th index. How would I solve this?
Thanks!
I just made a test program, to find out what's going on ... The layout just contains two spinners sp1 and sp2 :
private final int START_SELECTION_SPINNER1 = 0;
private final int START_SELECTION_SPINNER2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner sp1 = (Spinner)findViewById(R.id.sp1);
final Spinner sp2 = (Spinner)findViewById(R.id.sp2);
List<String> sp1List = new ArrayList<String>();
sp1List.add("Selection 1 Choice 1");
sp1List.add("Selection 1 Choice 2");
final Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
map.put(0, new ArrayList<String>() {{
add("SubSelection 1 Choice 1");
add("SubSelection 1 Choice 2");
}});
map.put(1, new ArrayList<String>() {{
add("SubSelection 2 Choice 1");
add("SubSelection 2 Choice 2");
}});
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,sp1List);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,new ArrayList<String>());
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp2.setAdapter(adapter2);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
adapter2.clear();
adapter2.addAll(map.get(position));
adapter2.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//DO YOUR STUFF
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
sp1.setSelection(START_SELECTION_SPINNER1);
handler.post(new Runnable() {
#Override
public void run() {
sp2.setSelection(START_SELECTION_SPINNER2);
}
});
}
});
}
The important thing are the last few lines where I have the handler post the setSelection(). Now it works :)

Categories

Resources