Can you explain this method's parameters? I did not understand them.
http://developer.android.com/reference/android/app/ListActivity.html#setListAdapter(android.widget.ListAdapter)
onListItemClick(ListView l, View v, int position, long id)
ListView l: defines the ListView on which the click event is happening?
View v: defines the view that was clicked within the ListView? So for a ListView, that would be the xml containing the ListView, and if its a custom ListView the xml containing the row?
int position: The position of the view in the list. Can it be retrieved using below function? getListView().getPositionForView((LinearLayout)v.getParent())
long id: The row id of the item that was clicked. Is this not same as position? Or do we provide R.id.Textview?
Its simple.
ListView l --> Is the name of the listview object on which the click is happening. (Simple)
Like
ListView l = (ListView) findViewById(R.id.list);
View v ---> There can be multiple view inside a list view. So this tells that which of the view has been clicked and takes action accordingly.
Position --> The position of the view in the list. (VIEW POSITION)
id ---> Row id of the ITEM . (ITEM ID)
A listview consists of small items/rows. While using a custom listView. Each row/item can have different functionality and layout. so for each row/item we must use different functionality..
For example in a listview at the fourth row there is a button while on all other items there is only text. So in order to put your event there you must get that particular row/item contents. the below code gives you the access to single item..
onListItemClick(ListView l, View v, int position, long id)
first parameter is parent where the click happened
second parameter is single View(child/row) of a ListView(Parent)
third parameter is the position of child view.
fourth parameter is the id of the child view.
1-first one ListView Object that one you define by :
ListView foo = (ListView) findViewbyId(R.id.foo);
2-second Row item it self (View v) one is simple that's object you click on (you can obtain the view that has been clicked), you can change color or content or what ever you want
3-position index of (View v) object in ListView start from 0
4- Row id of item (i never have been used)
Related
I have a listview. What I've implemented in that listview is that when user clicks a list item a 2 button view is inflated to replace the content of that list item like this:
This works fine but what I want is when I click the second list item the first one should come back to its original layout. Currently, it is like this:
This is my code implemented in onClick method of listview:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView planName = view.findViewById(R.id.planNameText);
TextView planDate = view.findViewById(R.id.planDateText);
ImageView planImage = view.findViewById(R.id.homePlanImageView);
planName.setVisibility(View.INVISIBLE);
planDate.setVisibility(View.INVISIBLE);
planImage.setVisibility(View.INVISIBLE);
RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate);
View child = getLayoutInflater().inflate(R.layout.inflate, null);
rl_inflate.addView(child);
}
});
Thanks.
Maybe you need to initialice a boolean variable to check if is clicked or not and refresh all the views. I really recommend you to use a RecyclerView and use 2 viewHolders. If you want information about this check this. If you implement a recycler with 2 viewholder it will be easier than the way that you want to implement it, and you can use notifyDataSetChanged to refresh the recycler. Whatever, you will need anyways a boolean to check if is clicked or not.
In my grid view there are 9 items. When user clicks an item I want to send user to another activity with a list view. But List view items are populated from a SQLite DB and it depends on the item which user clicks.
Ex: If user click the 1st item on the grid view , list view should populate with according to the 1st item .
As an example query should be like following code.
SELECT organization_name from TABLE_NAME where CATEGORY_ID=ItemIdOfGridView
Where do I put this query in onItemClick of GridView ?
You can use gridview's setOnItemClickListener to get position.
gridView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intet=new Intent(FirstActivity.this,second.class);
intent.putExtra("id", id);
startActivity(intent);
}
});
Then in second class get the extra and do as you need.
edit:
The position specify the clicked item position of grid view.Then you can get your necessary data from the list or arraylist of gridview which is used for populating gridview using this position.
i.e.
data=yourList.get(position).getData();
I have a onListItemClick that returns the text from item in the listview with:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
TextView tv = (TextView)findViewById(R.id.item_title);
Toast.makeText(getApplicationContext(), (tv).getText(),
Toast.LENGTH_SHORT).show();
}
The problem is the data it returns is from the item at the top of the current listview no the actual list item that is clicked. To illustrate if I had a list that was like this
A
----- (Start of Viewable area)
B
C
D
------(End of viewable area)
E
If I click on item D the toast will return the Title for item B. Any ideas how I can fix this?
That method is being called from within your custom ListView, correct? From what I can tell, "findViewById" is searching the entire ListView for a view with the id "R.id.item_title". Since you have multiple list items on the screen, it's pulling the top one. Instead, call
// Call on the child view provided as a parameter, instead of on the ListView
TextView tv = (TextView)v.findViewById(R.id.item_title);
so that it searches child views of the View object passed as parameter "v".
I have a ListView with a CheckBox.
What I am trying to implement is an onItemClick (in my activity) that check/select my CheckBox if I click on the row, as you can see below:
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyItem row = ListofRows.get(position);
row.setSelected(true);
myAdapter.notifyDataSetChanged();
}
When I click in the row, the ListView is refreshed because of the method notifyDataSetChanged()
THE PROBLEM IS: this method refreshes the whole ListView and as a result, the ScroolBar goes to the beginning (the user is driven to the header of the list) even if the user is at the bottom of the list.
WHAT I NEED IS: Refresh just this single clicked row so the user won't be driven to the beginning of the list.
Are there any solutions to this problem?
use the selected index
View v = getListView.getChildAt(index);
Now update the view with the new values.
I have a custom listview. I have to select some items from this listview and display it in the next layout listview when a button click event is clicked.
lv5=(ListView)findViewById(R.id.ListView05);
lv5.setAdapter(new ArrayAdapter<String>(this,R.layout.productselecttext,R.id.pstext,arr));
lv5.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.findViewById(R.id.oi).getVisibility()==ImageView.VISIBLE){
ImageView icon = (ImageView)view.findViewById(R.id.oi);
icon.setImageResource(R.drawable.vi);
}
}
});
In this way I have selected using an imageview. How can I get only the selected items and display in another listview?
It all depends on what you call a selected item. We need some more code to understand how you "selected" items in the first listview.
But generally speaking, all you have to do is to build a new Adapter for the second list view that just lists the selected item. You could even recylcle the first adapter and the first view, just eliminating the items that are not selected from the adapter's list and calling
notifyDatasetChanged()
on your first adapter to update your first list.
Regards,
stéphane