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
}
});
Related
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);
}
}
I have an ListView which I have populate with a dynamic ArrayList and I used an OnItemClickListener to make it does something when i click on them (Code bellow).
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ) {
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
Now comes the tricky part, I want, somehow to be able to read what is actually written lets say on the position [0], the actual String. Is there any way to achieve that?
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if (position == 0) {
String yourString = txtOnlyList.get(position);// use this
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote", "1 .txt");
startActivity(intent);
}
}
Your ArrayAdapter<String> has a getItem() method that returns the String for a given position.
I have used the getItemAtPosition(position).
Here is the entire code:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ){
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
String stringFromArray=txtOnlyList.getItemAtPosition(position).toString(); // I added this
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
The View type parameter being passed in your OnItemClick function is the list item's view at that position.
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv);
String text = tv.getText().toString(); //The required text is available in this variable
}
}
Im try to build auto Complete textview in android, and its work fine, but my problem is I need to get a value was related for item was selected, I mean
we have schools
abc, bbt, ccce, ddde
abc have id = 1, and number_of_students = 30
bbt have id = 2, and number_of_students = 20 students
ccce have id = 3, and number_of_students = 50 students
ddde have id = 4, and number_of_students = 40 students
when user write in text view abc, and select it, I need to get id and number_of_students, not only abc text,
I mean in html we have <option value="1">text<option>, when select it, we get a 1, not a text, is there option in android to set text and I get a value not a text
thanks a lot.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.schoolsAutoComp);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
//pos is the position of the selected item
Toast toast = Toast.makeText(getApplicationContext(), COUNTRIES[pos], Toast.LENGTH_LONG);
toast.show();
}
});
Final Solution:
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
String selection = (String) parent.getItemAtPosition(pos);
int pos2 = -1;
for (int i = 0; i < COUNTRIES.length; i++) {
if (COUNTRIES[i].equals(selection)) {
pos2 = i;
break;
}
}
System.out.println("Position " + pos2); //check it now in Logcat
}
});
setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id)
{
//pos is the position of the selected item
}
});
I have an array (array1 with States) and AutoCompleteTextview in which I'm filling it with array1. When I select the value from AutocompleteTextView I select a state from AutoCompleteTextView Dropdown
What I want is to get the position of the item from array1 which I've selected.
What I've tried is OnClickEvent of AutocompelteTextView.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
String num = selection;
}
});
It ss giving me the value which I selected from dropdown but i want to get the position of the value in array1. For Example I have an array of size 4, like array1 = {A,B,C,D}, and if I select B it should return me B position in Array i.e 2.
I hope I made it clear. Thanks in advance.
Use the position variable in the onItemClick.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
int pos = -1;
for (int i = 0; i < yourarray.length; i++) {
if (yourarray[i].equals(selection)) {
pos = i;
break;
}
}
System.out.println("Position " + pos); //check it now in Logcat
}
});
Use List instead of Array. Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item.
actvCity.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = cityNames.indexOf(actvCity.getText().toString());
// Do Whatever you want to do ;)
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView txtvw=(TextView) view;
String str=txtvw.getText().toString();
int index = contactNames.indexOf(str);
}
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//// id contains item if from database
ItemNoSelected = id;
}
});
id is from database, we can use that
You can find the parent string in the clickItem listener.
OSchools in this case is the custom ArrayList.
Object item = parent.getItemAtPosition(position);
for(OSchools d : data){
Log.e("name",d.getName());
String name = d.getName();
if(d.getName() != null && name.contains(item.toString())){
ID = d.getID();
}
is possible get the value of a single row.
In partycular i wont get in a string the value of id in my row of mt list with adapter.
In each row there are value id and label
Thaks a lot and sorry for my english
ListView list;
list = (ListView) findViewById(R.id.listView1);
mSchedule = new SimpleAdapter(getApplicationContext(), result ,R.layout.row_meteo,
new String[]{"id", "label"}, new int[] {R.id.textView1,R.id.textView2});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String idPlace = mSchedule.getItem(position).toString();
Toast.makeText(getApplicationContext()," Cliccato sul luogo :"+idPlace,Toast.LENGTH_LONG).show();
}
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.textView1); //<---------
String str = tv.getText();
}
Replace
String idPlace = mSchedule.getItem(position).toString();
with
String idPlace =((TextView)view.findViewById(R.id.textView1)).getText().toString();