i have a multi column listview that has been populated with json data. now on clicking a list item i need the datas of that particular list item to be displayed in the textView.
In onItemClickListener of listview get the item from list which is set to the listview adapter using position variable of onItemClickListener method. Then set it to the textview.
Something like this:
items = getListView();
items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Display content in Textview
}
});
Related
I am making a list of contacts. The contact number is a sum item in the list. If user clicks the phone button on that item then the number is called.
How to get the number? It is a sub-item in the list.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
String selectedItem = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"item is"+selectedItem,Toast.LENGTH_SHORT).show();
}
});
I am only able to get the Item Value. Not the subItem
Try to implement a method on the adapter to return the subItem from the item List
without the code it is hard to give more details.
I have list of items in gridview. On selecting item, position of the selected item should change to 0. Every time i choose item , it should move to first position.
How can i do this?
got it
temp = mFilteredBrandsList.get(position);
mFilteredBrandsList.remove(position);
mFilteredBrandsList.add(0, temp);
notifyDataSetChanged();
Try this inside your onItemClickListener.
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//assuming your grid is a list of string elements
String item= (String)parent.getItemAtPosition(position);
mList.remove(position);
mList.add(0,item);
notifyDataSetChanged();
}
Assuming mList is the main list consisting of elements inflated in grid's row.
If this isn't clear enough. SHare your code will modify the same.
I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view
Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.
You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.
That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.
I think you could use it like this.
adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what ever you want to give on item click
}
});
here by checking the position inside onItemClick you can assign specific tasks.
Is listview has any attribute (e.g. productId, like hidden field of html form) can be set for later use? The following is to build a listview by using an array
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, catlist);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
The listview will display the text inside of the catlist, i want to pass the productid which corresponding the category text to another activity when users click on every item.
listview.setOnItemClickListener(new OnItemClickListner()) {
}
You can do something like this:
You will have two arrays catlist and catIds, catlist will have name to show in ListView and catIds will have ids at same indices, now when the user click any item, you will get its position and grab the id from catIds at that position like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> pr, View v, int position, long id) {
String id = catIds.get(position);
//use this id for any purpose.
}
});
how to put selected items from a custom listview with an imageview and textview into an array? Actually my imageview changes when selected.
how to store those selected items in Array?
Better store the items in arraylist
ArrayList<View> arr = new ArrayList<View>();// to store list of selected views
ArrayList<Integer> positionArr = new ArrayList<Integer>();// to store list of selected positions
use setOnItemClickListener in listview.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
positionArr.add(position);
arr.add(arg1);
}
});