How to get the spinner's id on the onItemSelected method? - java

My problem is : I want to have 3 different spinners, that displays the same type of object, and I want to be able to identify from which spinner I get the data, for example :
In spinner 1, the user selected "potato"
In spinner 2, the user selected "tomato"
In spinner 3, the user selected "fries"
But I can only get "the user selected [...]", since I don't know how to tell from which spinner I got the data.
I was wondering if there was a way to do that on the onItemSelected(AdapterView parent, View view, int position, long id) method ?

The View view is most likely the spinner that made the selection. If you set the .tag = 1 of each spinner to different numbers at the start of your program, then you will be able to look at what tag value is passed to the onItemSelected method:
if (view.tag == 1)...

Probably you have set a common listener for all the spinners, so you can distinguish which spinner was selected by checking parent.getId():
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.spinner1:
//your code here
break;
case R.id.spinner2:
//your code here
break;
case R.id.spinner3:
//your code here
break;
}
}

Related

error while selecting a list item when list view is large in android?

Currently I am making an app in Android which is a todo list, it has a edit text, button and a listview, when button is clicked the text is added to listview, everything works fine but when i add too many items to list view and try to select a list item my app stops working and I get a error saying
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Does anyone know a fix?
this is my view data method whcih displays items in listview
public void viewdata(String tablename)
{
db = new mydbhandler(this);
lvitemslist = findViewById(R.id.lvitemlist);
ArrayList mylist = new ArrayList();
Cursor c = db.getdata(tablename);
while(c.moveToNext())
{
mylist.add(c.getString(1));
}
ListAdapter mylistadapter = new ArrayAdapter(this,R.layout.custom_item_row,R.id.ctv,mylist);
lvitemslist.setAdapter(mylistadapter);
}
and this is the onitemclicklistener
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
View v = lvitemslist.getChildAt(i);
CheckedTextView ctv = v.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
Take a look at this line from your OnItemClickListener:
View v = lvitemslist.getChildAt(i);
I guess you want to assign the ListView row which was clicked to the variable v. But the ViewGroup method getChildAt(int) (ListView is a kind of ViewGroup) will give you
the view at the specified position or null if the position does not exist within the group
(quoted from the ViewGroup documentation)
Like you said, this works for the first few items but for higher positions the app crashes. This is because the ListView has a fixed number of child Views: about as many as fit on the screen simultaneously plus some more to enable smooth scrolling. For every clicked row beyond that fixed number, lvitemslist.getChildAt(i) will return null.
What can you do to fix your code?
Luckily you do not have to do much work to get the clicked View, it is handed to you as one of the parameters of onItemClick(): the second parameter is the clicked ListView row.
So you just need to write
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
CheckedTextView ctv = view.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
}
}
This will help you to avoid the NullPointerException.
Another thing: you change the appearance of a ListView row from outside of the Adapter by calling
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
This is dangerous (there are lots of questions on this site where people had weird behaviour when scrolling as a consequence). It may not be a problem in your case because you refresh the whole ListView soon afterwards. But generally one should provide the Adapter with all the data necessary to decide what each row should look like. Then one can change the data list and after that of course call notifyDatasetChanged() on the Adapter.

How to get the value of a TextView that is part of a ListView row in all checked items

As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});

Android: Get Value from Grid View

Hi I currently have a Grid View, Which I Inserted an Array through an Adapter onto the Grid View.
I am using this method currently:
gridview.setOnItemClickListener(new OnItemClickListener() {
}
As demonstrated within the Android Documentation, Specifically I need to find the value set within the field of the grid that the user touch/clicked. Is this done through using the position variable within on item click e.g?
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// DO something
}
Or is it done manually ? Kind of at logger head's with this. Any help would be really appreciated.
You can try something like:
String value = (String)adapter.getItemAtPosition(position);
inside your onItemClick where adapter is the AdadpterView
The v argument in your onItemClick() method is a reference to the View that was clicked.
Simply cast that into a TextView and get the text:
String text = ((TextView)v).getText().toString();

Three spinners in one Activity

I have three spinners in one Activity , so I want to choose from one spinner then getting some specific values to the second spinner and after choose one of the second then getting some specific values to third spinner .
i have tired with using loops and switch case but i think it takes much time and long code .
is there is simple way to make this three spinner dependent to each other ?
Disable spinner 2 in your layout, and then enable and populate it when spinner 1 item is selected. Then do the same for spinner 3 based on spinner 2.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Enable Spinner 2
// Set spinner 2 adapter
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// Disable spinner 2
// Set spinner 2 adapter to empty list
}
});

Android: onItemClick listener of ListView - how to identify the items?

I have created a custom ArrayAdapter that fills a ListView. The data in the adapter is a list of POJOs which I get from a server. Each POJO has a unique id given by the server, I have no influence on that id. The id is only for internal use, means it isn't displayed to the user on the ListView.
My intention: if the user clicks an item in the list, I want to pass the id back to the server. The first step would be to create an onItemClickListener:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
// call the server in a new thread
// BUT: how do I get the POJO id from this position?
}
});
My problem is: I don't know how to obtain the id of the POJO. Since it is not displayed to the user, is is not in the TextView I get passed in the listener. The other parameters - position and id - do not seem to be what I need also. Description:
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
I hope you got what I mean, otherwise tell me. Thank you!
the parent.getItemIdAtPosition(int position) will give you for pojo object at that index.
That would depend on how your adapter work. The id parameter will have a correspondence to one of the items the adapter is adapting. Based on that id, and how your adapter works, you can retrieve the original logical item that was represented by the view that was clicked.
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getAdapter().getItem(position);
}

Categories