How to retrieve the right view? - android

My adapter contain the following Views:
private static class ViewHolder {
ImageView p_Image;
TextView p_Name;
TextView p_Price;
TextView p_Psave;
}
This Is how I implement onItemClick :
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
});
How can I know which view from the above 4 was actually pressed (while running OnItemClickListener via GridView) ?
Thanks

The second parameter to onItemClick, View v is a reference to the view that was clicked. You can compare this to your views to see which one it was.

Related

Get id / position of ListView row

I have an Android ListView with a bunch of rows. Each row contains an image and some text. If I click on the image of a row, I want to perform action A, if I click on any other place in the row, I want to perform action B. Both of these actions need the id of the ListView item to work.
So far, for action B, I simply used listview's onItemClick, where I got the id from the parameter. However, as far as I know, the only way to find out on which view in the row itself the user clicked, is to add an onClick event handler to the ImageView that triggers action A. In that case, I only get the View that was clicked, though - not the id of the row that the ImageView is in.
My question is - how do I go from a View object to getting the id of the parent row within the ListView?
The onItemClickListener method has an onClick interface already with the view and position passed into it: You can get check which view has been clicked by doing a switch statement and performing a dedicated action when the specific view has been clicked.
E.g.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});
as shown in How to create listview onItemclicklistener
You'll see that in the onItemClick interface it has the View view as a parameter and int position as a parameter, perform the action inside the listener using both of those parameters. Like i mentioned before you can use a switch statement.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
switch(view.getId()) {
case R.id.imageView:
//perform action for imageView using the data from Item (if you have used an MVC pattern)
break;
// you can do this for any widgets you wish to listen for click actions
}
}
});
Update: All credit for this goes to wwfloyd in How to know which view inside a specific ListView item that was clicked
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
in your adapter apply a click listener to the element and then trigger the interface method, so that you can identify the registered widget that has been clicked and then in your onItemClick do:
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
switch (viewId) {
case R.id.someName:
//widget has been clicked
break;
default:
//row has been clicked
break;
}
}
if you aren't using tag, then you can
int itemId=...;
OnClickListener customOnClick=...;
imageView.setTag(itemId);
imageView.setOnClickListener(customOnClick);
and inside your custom OnClickListener
int itemId = (Integer) view.getTag();
note that you can set "any" Object as tag, so you may even set your whole custom object, not only id
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id)
in this method View arg1 gives you Item view and you can find the item content by this arg1.findViewById(R.id.content);. Here position gives you the item position of list view
You can set click listeners for each view in a row from the getView() method in the adapter.
You can impliment it in getView() method of adapter of listview.

Getting views of a custom view in android

Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?
Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example

Get the position of Button in List View

I have one problem.
I want to get the position of Button in List View.
In below image when I clicked in Edit or Delete button i just want its position in List View.
How it possible?
This is my Office Management Screen. All the data getting in ListView run-time from my web-service.
I get the data in List View by using this code-
ListAdapter adapter = new SimpleAdapter(Office_Mgmt.this, officeList, R.layout.office_mgmt_list_model, new String[] { "office_name" }, new int[] { R.id.label});
setListAdapter(adapter);
You can create your custom adapter, passing the context of your app as argument to set the click:
listView.setAdapter(new PesquisaAdapter(this, anunciantescidades, this);
Then, in the constructor of your adapter you will have a OnClickListener to recieve the argument this you passed:
public PesquisaAdapter(Context context, List<Anunciante> anunciantes, OnClickListener onClick1)
In the getView method of the adapter, you can set onClickListener of the button:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_layout, null);
Button btn = (Button) v.findViewById(R.id.yourbutton);
btn.setOnClickListener(onClick1);
}
In your java, you can implements onClickListener, and then do what you want with your button:
#Override
public void onClick(View v) {
if(yourbutton.getId() == v.getId()){
final int position = listView.getPositionForView((LinearLayout)v.getParent());
}
}
So, you will have the position in the list that you clicked, and you can manage the click of the edit and delete separately.
Hope it helps!
implement onItemClick(AdapterView<?> parent, View view, int position, long id) for the listview.
Position and view can be identified from the handler. You can set additional info using setTag while creating the view inside the Adapter's getView method and retrieve in the handler and then verify in the handler if required.

How can I handle onListItemClick(...) based on what view inside the row was clicked?

Question
I have a listView inside a DialogFragment and I want to fire certain callbacks only when certain particular items inside a row are fired. How can I do that?
Basically, I want to do something like this
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int viewId = view.getId();
if ((viewId == R.id.textView1) || (viewId == R.id.textView2)) {
// do something...
}
which I can't. Read further if you don't know why.
What I tried
I tried to look into the documentation, but the OnItemClickListener callback doesn't offer as a parameter the exact clicked view (the View you can see in the signature is the whole row).
Also, I tried to set a simple onClick callback on the single view in the adapter, but this overrides the listSelector and other behavior a list should have. Reading in the documentation, I found it's explicitly written that we should set callbacks via the onListItemClick(...) method (not via onClick(...)), so I'm looking for a way to do that, using this method, not to override any default list behavior.
I was trying to get this done by working on the xml. To my surprise, I found that if I set a view android:clickable property to true, the onListItemClick callback won't fire (I thought it was the opposite),
so a partial solution would be to set to android:clickable=true every view in the row apart from the one I want to fire the callback, but that is not a solution because if the user clicks where there is padding or white space, the callback will fire. Also, I found that if I set the parent of the row's view to android:clickable=true and the child views I want to handle with the callback to android:clickable=false, this won't work, because apparently the property is not overwritten.
EDIT Sorry for the really bad title this question had before, I didn't even noticed I submitted the question.
new Answer, hope I understood now :)
In your adapters getView, attach an OnClickListener to any view in your layout you want to fire. (more pseudocode)
public class Adapter extends ArrayAdapter<XYZ> {
private int resource;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.resource, parent, false);
((Button)convertView.findViewById(R.id.YOUR_BUTTON_IN_LAYOUT)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DOSTUFF();
}
});
return convertView ;
}
}
old Answer:
The position indicates where you are in the list (pseudocode).
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position,long arg3) {
YOUR_ITEM_BACKED_BY_ADAPTER item = listView.getItemAtPosition(position);
if(item==THE_FIRST_ITEM_IN_LIST) doSomething();
else if(item == THE_LAST_ITEM_IN_LIST) doSomethingElse();
}
});
You can set listeners for other views inside the adapter's getView
public class MyAdapter extends ArrayAdapter<MyItem> implements View.OnClickListener {
public View getView(int position, View convertView, ViewGroup parent) {
// setup the converView inflating it, for simplicity I've removed that code
MyItem item = getItem(position);
text1 = (TextView)convertView.findViewById(R.id.text1);
text2 = (TextView)convertView.findViewById(R.id.text2);
text1.setOnClickListener(this);
// pass the item to use when clicked
text1.setTag(item);
text2.setOnClickListener(this);
text2.setTag(item);
}
public void onClick(View v) {
MyItem item = v.getTag();
switch (v.getId()) {
case R.id.text1:
download(item);
break;
case R.id.text2:
upload(item);
break;
}
}
}
Instead of hardcoding action (eg download) inside the adapter you can pass to it an interface and for example the calling activity can implement that interface

Android: How to extract data from GridView?

Hi all
I have a GridView of FrameLayout made of an image and textview. I want to get the corresponding image and the text when I click a cell of Grid. Mentioned that textview is top of image.
Does anybody have idea how to do it?
First register item click listener of grid view
e.g. gridview.setOnItemClickListener(gridItemClickListener);
Then
OnItemClickListener gridItemClickListener = new OnItemClickListner()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// here you will get your clicked cell of grid and you can find the image and text.
ImageView image = view.findViewById(R.id.grid_raw_image);
TextView textView = view.findViewById(R.id.grid_raw_text);
}
};
If you use the viewHolder technique as recommended by google, use:
gridView.setOnItemClickListener(new OnItemClickListner()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ViewHolder holder=(ViewHolder)view.getTag();
//use the views you hold in the viewHolder
...
}
});

Categories

Resources