I am using spinner and listview concurrently, I have some logic that when i scroll on list i have have a data indication that tells which values to set on spinner, and i m using
spinner.setSelection(somePosition);
and when I click on Spinner it has also some data which indicate that to set the position of
listView.setSelection(somePosition);
Problem is that when i m scrolling on listView and in my adapter i need to change the postion of spinner selected item it calls the method
spinnerSurah.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
listView.setSelection(positionSelection);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Help me, I need to just change the postion of spinner without calling its listener.
You could set a condition in your listener that bypasses the selection logic if the ListView selection is called from the listener.
Declare a boolean in your class:
boolean skip_listener = false;
and change your listener to
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
if(skip_listener){skip_listener = !skip_listener;return;}
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
skip_listener = true;
listView.setSelection(positionSelection);
}
}
Related
I have RelativeLayout with ScrollView as the root element. In RecyclerView I have spinner and TextView. Now I am getting data from JSON like RollNumber and Marks. Now in TextView, I need to show RollNumber and in Spinner Marks should be displayed. I already got the data in ArrayList<Progress> and what should be the code will look like for this situation;
Do I have to repeat the RelativeLayout in for loop?
OnCreate
for (int i = 0; i < progress.size(); i++) {
progress_addmore_spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
loadspinner
}
private void loadSpinners()
{
if (progress != null || progress.size() > 0) {
ArrayList<String> progresscard = new ArrayList<>();
for (int i = 0; i < progress.size(); i++) {
progresscard.add(progress.get(i).toString());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
progresscard);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
progress_addmore_spinner.setAdapter(dataAdapter);
if (progress.get(0).getMarks() != null && !progress.get(0).getMarks().isEmpty()) {
product_addmore_spinner.setSelection(progresscard.indexOf(toppingtypelist.get(0).getMarks()));
}
}
}
You can set the spinner including the adapter, the default value in the onBindViewHolder of the recyclerview adapter. Also set the position as the tag of the spinner in the adapter like: vh.view.setTag(position);
Now implement the OnItemSelectedListener in the activity or the fragment and pass it onto the recyclerview adapter. Since you have already set the position of the spinner as the tag, you can easily extract the index of the progress object in the ArrayList.
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int index = (int) parent.getTag();
parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
String[] idArray = {"1","2","3","4"};
String[] nameArray = {"Abraham","Abhi","John","Joseph"};
final ArrayAdapter<String> adapterTextView = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray);
etItemName.setAdapter(adapterTextView);
etItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String id = idArray[position];
}
});
This is my code. In this, when I type "Jo", John and Jospeh are populating on AutoCompleteTextView and when I selected John I get String id as 0. But actually I need is 2. So how can I get 2 instead of 0?
This is not the perfect solution but you can take it as the last option:
When you pass the selected string just loop through your nameArray to find its position:
int position=-1;
for(int i=0;i<nameArray.length;i++){
if(selectedItem.equalsIgnoreCase(nameArray[i])){
position=i;
}
}
#Shaheer Palollathil n9153's suggestion would work. I'm just changing his answer according to your code.
AutoCompleteTextView t;
t.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = -1;
for(int i = 0; i < nameArray.length; i++){
if(((AutoCompleteTextView)view).getText().toString().equals(nameArray[position]))
pos = position;
break;
}
String id = idArray[pos]; // should be your desired number
}
});
In my home activity. I have listview with id list and textview with id count.
Here's the code:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int count = 0;
for (int i = 0; i <= list.getLastVisiblePosition(); i++) {
if (list.getChildAt(i) != null) {
count++;
counter.setText(count + "");
}
}
I want to display in textview of rows in listview
Do you want to display how many items is in ListView or visible on screen?
For first option:
If you use ListView, then you have to use some ListAdapter (for example descendant of BaseAdapter or ArrayAdapter) which has method
getCount()
This returns, how many items is in adapter and also in ListView.
edit:
TextView countTextView; // here you want to set number of items
ListView lv; // your ListView
BaseAdapter adapter; // your adapter, that is used with ListView
// call this method in some listener
private void showNumberOfItems() {
int count = adapter.getCount();
countTextView.setText(String.valueOf(count));
}
You can use getLastVisiblePosition() method to count the number of rows,
int count=0;
for(int i = 0; i <= list.getLastVisiblePosition(); i++)
{
if(list.getChildAt(i)!= null)
{
count++; // saying that view that counts is the one that is not null, because sometimes you have partially visible items....
}
}
and then set the Text,
counter.setText(count + "");
Edit-
try something like this,
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int count = 0;
for (int i = 0; i <= list.getLastVisiblePosition(); i++) {
if (list.getChildAt(i) != null) {
count++;
}
}
counter.setText(count + "");
This is the code to populate my spinner.
The code:
String[] rcs = new String[review_cycles.length];
for (int i = 0; i < review_cycles.length; i++)
rcs[i] = review_cycles[i].ReviewCycleName;
ArrayAdapter<String> rc_spinnerAdapter = new ArrayAdapter<String>(
ActivityManagementReview.this,
R.layout.simple_spinner_item,
rcs);
sp_review_cycle.setAdapter(rc_spinnerAdapter);
sp_review_cycle.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
DtoMrReviewCycleVersion selected_review_cycle_version = review_cycles[position];
if (selected_review_cycle_version != latest_review_cycle_version) {
latest_review_cycle_version = selected_review_cycle_version;
int mr_version_id = latest_review_cycle_version.MrdVersionId;
_URL_version = _url_base + "MrVersion/" + Integer.toString(mr_version_id);
new GetMrVersionInfoAsyncTask().execute();
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
When I select a value from the spinner, the code is executed twice. Once with the selection I made and then right afterward, the first item in the list is executed again bringing me back to where I started.
How can I prevent this from happening?
Thanks.
When I select a value from the spinner, the code is executed twice.
As I reminded you in the comments, Spinners call onItemSelected() when they load the default value. This feature is useful when you know about it, but since it is not what the average developer expects it is problematic as well.
The Spinner will also call onItemSelected() when the user re-selects the current value... When I want to avoid both of these false alarms I use something like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
int previous = -1;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(previous != position && previous < -1) {
Log.v("Example", "Selected: " + position);
// Do something
}
previous = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});