I have successfully populated a ListView with an onItemClickListener that takes the user to a new Activity when clicked.
But I'd like to pass on the name of the ListView item that was clicked as a variable (so I can use it in an SQLite query).
I understand that I need to pass this variable as an extra in an Intent, but how can I capture that variable in the first place?
Here's my current code:
authorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// String author = captured ListView item
}
});
authorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String author = authorsListView.getItemAtPosition(position).toString();
}
});
The list you are passing in the adapter, get the reference of that and get the object of the list using get method/ and use 'i' variable as an argument for get method. It will return the object of click position. And done. Use this object get the require property and pass that with intent to new activity.
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 am having a problem with ListViews and ArrayLists.
I have an ArrayList of items. Each item has info such as ID, Title, Price etc. I also have another ArrayList of allocations. These allocations contain an "ItemId" variable - so I plan to link this to item. (Without the use of a database.)
This is how I think I will do it. First of all I will convert my ArrayList of items into a ListView using AndroidStudio. When the user clicks on a specific item on this ListView, I will run an if statement (e.g if the Id of the selected item is 2, display the allocation with that itemId)
However there is a problem with this. I can't check the Id in this if statement because my ArrayList has already been converted to a ListView, which I can not search for specific data. Can anyone help me?
Here is the code I have at the moment:
public class ViewItems extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_items_layout);
ListAdapter itemAdapter = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, Item.itemArrayList);
ListView itemListView = (ListView) findViewById(R.id.itemListView);
itemListView.setAdapter(itemAdapter);
itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String itemPicked = ????;
IF STATEMENT GOES HERE;
}});
}
}
You don't 'convert' your ArrayList into a ListView. The ListView merely uses the ArrayList to show your items - the ArrayList is still valid.
More precise, you add your items to the ArrayAdapter, which uses your exact ArrayList. You can use:
itemAdapter.getItem(i);
Complete example:
final ArrayAdapter<Item> itemAdapter = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, Item.itemArrayList);
ListView itemListView = (ListView) findViewById(R.id.itemListView);
itemListView.setAdapter(itemAdapter);
itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Item itemPicked = itemAdapter.getItem(i);
IF STATEMENT GOES HERE;
}
});
Note that since you're putting instances of Item in your ArrayList, itemAdapter.getItem(i) will return an Item, not a String.
Use getItem of your ArrayAdapter
itemAdapter.getItem (i);
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 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();
I have a spinner that opens programaticly, and when the user chose an option from the spinner, it closes... is there a way to be notified, or a listener that tells you, when the user chose his choice?
the onItemSelected gets the default item that is chosen automaticly when the spinner is open.
set setOnItemSelectedListener to your spinner...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object obj = (Object) parent.getSelectedItem();
//get clicked position from position
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//this method is call when nothing choosed by you
}
});
Add an OnItemSelectedListener to your Spinner.
I am not sure if I understand your question correctly but let me try to answer it. The normal and straight forward way would be to add an OnItemSelectedListener to the spinner i.e
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Do whatever you want here
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
but this is such a basic thing that I feel like a fool for pointing it out. Anyway here is a tutorial on spinner from Android Developer resources. It creates the listener in step 5 and adds it to spinner on step 6.