I'm trying to get the position of an array using the onItemClickListner.
With the following code, I am able to pass the text of the item clicked but not the position of the array eg 0, 1, 2 etc.
String[] menuItems = new String[]{"Hello", "Is it me", "Youre", "Looking for"};
ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.menulists, R.id.menulistsTextView1, menuItems);
ListView listView = (ListView) findViewById(R.id.mainListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String selected = (String) l.getItemAtPosition(position);
}
Any tips on whether it is possible to do that, I tried changing String to int and also to use the getItemIDAtPosition function but would not work, would just close the application.
Cheers
EDIT
I have just taken a different approach to achieving what I want to do using if statements for the string of the item clicked. Thanks for the input
You can get the position from method signature :
public void onItemClick(AdapterView<?> l, View v, int position, long id)
position give you index of selected item.
you may try this :
String itemClicked = menuItems[position];
As I understand Your problem - You want to get item on clicked position. Do do so use getItem(position) method of adapter. Code:
String[] menuItems = new String[]{"Hello", "Is it me", "Youre", "Looking for"};
//VERY IMPORTANT
//adapter must be declared final to use it in onItemClick
//or adapter should be object parameter
final ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.menulists, R.id.menulistsTextView1, menuItems);
ListView listView = (ListView) findViewById(R.id.mainListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String selected = <String>adapter.getItem(position);
}
});
Related
Please tell me how can I hide the item at index 0 from the dropdown in the spinner in Android Studio? I am using this code, it works, but when I open the list, it shows at the bottom. That is, it is focused on the elements below. what do i need to change?
SpinnerName = (Spinner) v.findViewById(R.id.spinner1);
ArrayList<String> names = new ArrayList<>();
names.add(0, "SELECT");
names.add(1, "Name1");
names.add(2, "Name2");
final int listsize = names.size()-1;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, names){
#Override
public int getCount() {
return(listsize);
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerName.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_list);
SpinnerName.setSelection(listsize);
SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
....
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
SpinnerName.setSelection(listsize);
your problem is here! you are passing the total list size number and
that where you are doing wrong, because setSelection method use to
show default index of spinner.
you can simply do that SpinnerName.setSelection(1); that would give you the fist spinner item unless showing the names.add(0, "SELECT"); item
I want to change the value of the second spinner when the value of the first spinner is changed I tried the solutions but did not get an exact solution so anyone have a solution for that so please suggest or any question then please ask
ArrayAdapter<String> deptArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Province));
deptArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvinceEyeTest.setAdapter(deptArrayAdapter);
deptArrayAdapter.notifyDataSetChanged();
Set a OnItemSelectedListener() first.
spinnerProvinceEyeTest.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString(); //this is your selected item
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
In onItemSelected() of above Listener, place your condition and set resource for second Spinner as:
If(selectedItem == "YourText"){
//Here, you're initalizing the second `Spinner`
ArrayAdapter<String> secondArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.YourList));
secondArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
secondSpinner.setAdapter(secondArrayAdapter);
}
else{
//Use any other list with arrayAdapter in case of other item selected.
//Use can use If-else-if or Switch to select different resource for different item of first spinner
}
Reference - Official Documentations.
I have a ListView implementing onItemClickListener and on its first click, I am highlighting the list item and adding it to a arrayList (Which I am sending it to next activity). What I want is to remove the ListItem from the arraylist when I click on the listitem second time, That is when deselecting the list item. This is my code that I implemented for adding. Please guide how can I remove elements.
`listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present =listView.getItemAtPosition(position).toString();
presentStudent.add(present);
}
});`
You need to check if the list contains the object, add it if not and remove it if it's in.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present = listView.getItemAtPosition(position).toString();
if (presentStudent.contains(present)) {
presentStudent.remove(present);
} else {
presentStudent.add(present);
}
}
});
How to remove the seperator line in footerLayout? I have a footerLayout below the listView, used to display the totalAmount as shown below. If I click the seperator line in footerLayout, my app crashed.
My MainActivity
AllAdapter obj = new AllAdapter(getApplication(), search, listview,imageView,text,button);
footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.under_listview, null);
totalAmount = (TextView) footerLayout.findViewById(R.id.amount);
LogCat error
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.example.tony.monthlyexpenses.adapter.AllAdapter.getItem(AllAdapter.java:61)
at com.example.tony.monthlyexpenses.QuickExpenses$1.onItemClick(QuickExpenses.java:88)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
The error pointed to listView onClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
mClickedPosition = position;
Expenses o = (Expenses) obj.getItem(position);
String day = o.getDate();
}
});
AllAdapter
public Expenses getItem(int position) {
return search.get(position);
}
The footerLayout is supposed to be displayed outside the listView, not inside. How can I get rid of this ?
I also have activity_main.xml, AllAdapter class, all_adapter.xml for ListView and also under_listview.xml for the footerLayout.
activity_main
AllAdapter
under_listview
How to move the footerLayout out from the ListView ?
I add android:footerDividersEnabled="false" now become like this
But still clickable !!!
The footerLayout is supposed to be displayed outside the listView, not
inside.
Actually, the footer is also part of the ListView and it adds up to the number of items the list has. Neverhteless, there are a few approaches how to ignore the click events on a footer view.
One option is instead of adding your view using:
addFooterView(footerLayout);,
use:
addFooterView(footerLayout, null, false);
The third parameter specifies whether the footer view should be selectable or not.
Another option would be to ignore the click when the position parameter is bigger than the size of your adapter dataset:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
if(position < adapter.getCount()){
Expenses expenses = (Expenses) adapter.getItem(position);
String day = expenses.getDate();
}
}
});
Try to set the following line in your listview.
android:footerDividersEnabled="false"
I change this code and it works fine now.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
Expenses o = (Expenses) listView.getAdapter().getItem(position);
if(o != null){
mClickedPosition = position;
//Expenses o = (Expenses) obj.getItem(position);
String day = o.getDate();
}
}
});
just a quick tutorial, if you are new to custom ListViewAdapters.
I would have an Object Expense like this:
class Expense {
Date date = new Date();
Double spendMoney = 5.0;
Bitmap image;
...
/** Continue with POJO **/
}
and an expenses_list_item.xml layout. (if you need help with that, i can add it later...)
In my MainActivity I would have an ArrayList<Expense> expensesList
and my custom ArrayListAdapter would look something like this:
class expenseListAdapter extends ArrayAdapter<Expense> {
expenseListAdapter() {
super(context, R.layout.expenses_list_item, expensesList);
}
#Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.list_item_series, parent, false);
}
ShowListItem current = expensesList.get(pos);
TextView expens = (TextView) view.findViewById(R.id.expenseText);
expens.setText(current.getSpendMoney());
... (all other needed values of your Expense object)
return view;
}
}
And finally, I could just set
ArrayAdapter<Expense> adapter = new expenseListAdapter();
expenseListView.setAdapter(adapter);
That would be it. Maybe a little bit complicated on the first look, but this method is very powerfull. If you need help, don't be afraid to ask ;)
Greets
Malik
I have created an AutoCompleteTextView drop-down which contains multiple words in a row like -
row 1 - Infosys Limited
row 2 - State Bank of India
when I'm entering Infosys, it works fine when I enter space after the drop-down goes off.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, GreenDaoUtils.getSSSymbolNamesArray(this));
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setLines(8);
autoCompleteTextView.setDropDownBackgroundDrawable(getResources().getDrawable(R.drawable.cust_rect_border));
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
});
}
});
Use FilterWithSpaceAdapter instead of ArrayAdapter
u get from FilterWithSpaceAdapter here
FilterWithSpaceAdapter<String> adapter1;
adapter1 = new FilterWithSpaceAdapter<String>(this, android.R.layout.simple_list_item_1, GreenDaoUtils.getSSSymbolNamesArray(this));