I have a POJO, which describes some model (Item?) and some custom Adapter.
I am setting my adapter for ListView and then, in onItemClick() I want to get value of one of variables which I had added Into the Item.
How I can reach this?
In my code I am doing something like:
private List<SomeItem> items = new ArrayList();
items.add(new SomeItem(firstValueString, secondValueBitmap, thirdValueString));
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
...
#Override
public void onItemClick(){
//How to reach for example firstValueString value of currently clicked item??
}
)
Use android.widget.AdapterView.OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
+ id);
SomeItem item = items.get(position); // specific item
}
});
Based on Android SDK documentation:
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
If you implement your adapter completely then you can get item like bellow:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = (Item)parent.getItemAtPosition(position);
}
});
Related
I have created a ListView using a custom adapter that extends ArrayAdapter<String>. I need to add the item name to an ArrayList when it clicks on the + button.
You can do it exactly as usual (like in MainActivity), just link it in GetView(...)
Inside your ArrayAdapter.java , add this:
private OnItemClickListener mListener;
public interface OnItemClickListener
{
void onAddClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener)
{
mListener = listener;
}
Inside your Holder Function on same file, make this:
AddButtonVariable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null)
{
int position = getAdapterPosition();
if(position != ListView.NO_POSITION)
{
listener.onAddClick(position);
}
}
}
});
Inside in your onResponse function of Activity.java file add this, after setAdapter:
listAdapter.setOnItemClickListener(new ArrayAdapter.OnItemClickListener() {
#Override
public void onAddClick(int position){
functiontoAdd(String.valueOf(position));
}
});
Now, at last, inside your Activity.Java file, at the end (before closing bracket of class), add this:
private void functiontoAdd(String position)
{
// rest of the code to add item in cart
}
Look, it is simpler with using Recycler view instead of list view. But it is ok if you already created.
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long id)
{
//I am assuming the ArrayAdapter is having list of dish name as string if the list is of Custom class please change the below code as per it.
String dish = (String)adapter.getItemAtPosition(position);
}
});
adapter AdapterView: The AdapterView where the click happened.
view View: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position int: The position of the view in the adapter.
id long: The row id of the item that was clicked.
I want to get data from selected item in GridView.
My app should work like this: open activity where is GridView => GridView load data from server (its working) => when I click an item, app get & send data to server.
Here is Code:
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UniqueID = UNIQUEID.getText().toString();
POints = POINTS.getText().toString();
DEscription = DESCRIPTION.getText().toString();
AddPoints = ADDPOINTS.getText().toString();
//these two strings are displayed on gridview
Operations(UniqueID, POints, AddPoints, DEscription);
}
});
I have found solution:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ADDPOINTS = (TextView)view.findViewById(R.id.tv_points_grid);
DESCRIPTION = (TextView)view.findViewById(R.id.tv_points_describtion);
UniqueID = UNIQUEID.getText().toString();
POints = POINTS.getText().toString();
DEscription = DESCRIPTION.getText().toString();
AddPoints = ADDPOINTS.getText().toString();
Operations(UniqueID, POints, AddPoints, DEscription);
}
});
Inside the onItemClick() method,
Try parent.getItem(position). Or parent.getItemAtPosition(position)
Hope this was I you were looking for!
I would like to call an action every time my spinner selects a new item. The current setup is that an action is called when a button is pressed.
My approach:
Create a new function: public void onSpinnerItemSelection(View V){...}
Then in content_home.xml I would add onSpinnerItemSelection to onClick for Spinner.
This isn't working. I'm not familiar with Android errors and I'm struggling to interpret these errors.
Unfortunately, myApp has stopped
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
Step by step:
1ªDeclare variables from the layoutSpinner sp; the arraylist in this case ArrayList listTeams; and the adapter Adapter adapter;
2ªinstances
private void instances() {
sp = (Spinner) findViewById(R.id.spinner);
listaTeam = new ArrayList();
3º add items for the adapter
private void listAdapter() {
listaTeam.add(new Equipo(R.string.Atleti));
adaptador = new Adaptador(MainActivity.this, listaEquipo);
sp.setAdapter(adapter);
now we make actions, use onItemSelected.
public void onItemSelected(AdapterView parent, View view, int position, long id) {
Toast.makeText("this is my team", Toast.LENGTH_LONG).show();
}
https://www.youtube.com/watch?v=H3W0-Nv664I
Here is an example:
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText("log", "position = " position, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText("log", "onNothingSelected", Toast.LENGTH_LONG).show();
}
});
This question already has answers here:
Android - How to create clickable listview?
(5 answers)
Closed 8 years ago.
I currently have a ListView id=listview, which I am populating with an ArrayList id=myarraylist. Here is how I do it:
final ListView listview = (ListView) findViewById(android.R.id.list);
myarraylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
myarraylist.add(...And then some other stuff I add...);
//This is the part I want to focus on
myarraylist.add("Reset High Scores");
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Now, I want the entry Reset High Scores in my ListView to be clickable, and when it is clicked, I want it to do Some Stuff. I know for buttons all you do is set an onClickListener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
..Some Stuff..
}
});
My question is, how can I make Some Stuff happen when the element Reset High Scores is clicked in the ListView
Edit: This is what I have so far
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Clear High Scores")) {
//Stuff
}
}
});
ListView, just like Button, has the ability to set a click listener, but for ListView, the name of the method is setOnItemClickListener(). Instead of using OnClickListener, though, it uses a class called OnItemClickListener, which is more advantageous for using with ListView. The method looks as follows:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
// some stuff
}
}
)
Now the advantage of this is that you can check what's contained at the position you've pressed, so the way you could tackle your problem could look something like this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Reset High Scores"))
{
// Do what you want
}
}
});
/**
* Creates a new instance
*/
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
super.onListItemClick(l, v, position, id);
//Do stuff
}
that should get ya started... Andrew is right though.
Hi i have the following ListView code:
{
// Populate the wordsList with the String values
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
}
I would like a listener which when it finds the string "start" from wordsList it does a if statement, I have tried for the past many hours but cannot get anywhere.
Do you mean something like that?
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (myList.getAdapter().getItem(position).equals("Start")) {
//Do your stuff here
}
}
});
In that case, the listener will be called when the user clicks on the item which text is "Start". Is that what you want?