I want to know which column user pressed in one TableRow object. It is header of table and i want to set sorting of items by chosen column.
I am able to use onClick listener in this case.
Problem is simmilar to How to click an specific TableRow within a TableLayout
You can do it like:
set Tag to each tablerow as follow:
tablerow.setTag(pass object of item which you want to set on this tablerow)
Now, set Listener:
tablerow.setListener(listener);
Listener should be pass from fragment/activity to Adapter.
So, when you click on any row, you will be able to listen it in fragment as:
listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag(); here you will get object which you set above.
}
}
So, using the tag object, you can able to find which row it is.
i would suggest using different layout for showing the data. GridView would be a nice solution. GridView docs There you should have the correct event, you are looking for On item click listener
Related
I am trying to create a dynamic survey app, using a RecyclerView. I have no problem on initializing the primary questions. The issue is when I try to insert a row in the recyclerView.It is not placed where it is expected to be. I wanted the row to be inserted next to the current row. I tried several methods like getAdapterPosition, getLayoutPosition for viewholder but I cant get the selected correct position of the view. Furthermore, the RecyclerView does stores the old location of a view when something is inserted. Could someone help me to figure this out or is there any easier approach. Furthermore, Thank you guys, I'm a newbie programmer.. Here is a snippet of my Adapter where I create and initialize dynamic views.
What i always do and works fine is to use tag.
for example you have a radio button and you have an item ( Question , single choice etc... ) .
before you assign a clicklistener to your view just put that item as tag
MYVIEW.setTag(MyItem);
so in your clicklistener just do this :
new View.OnClickListener() {
#Override
public void onClick(View v) {
ITEM item = (ITEM)v.getTag();
int alwaysTruePosition = items.indexOf(item);
...
}
}
Important note is that you should define equal method in your ITEM class and there should be a unique identifier which helps you to define are two objects the same or not. (for example a unique id or unique name )
I am writing an android app where I am using a grid view to display some items. I want to give users a configurable view where they can change the no of columns on the activity by clicking floating action button. I am changing the column no using
gridView.setNumColumns(selectedColumnNo);
This is working fine But the problem is if a user changes no of column after some scrolling the First Visible Position is set to the first item of the array list, so the user has to scroll the view again. Can someone please tell me where I am doing wrong. Or Is this the proper way to do this or should I use a different approach.
A code snippets will be helpful
Thanks.
Update::
currently I am using the bellow snippets
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int firstPosition = gv.getFirstVisiblePosition();
if(gv.getNumColumns()==2)
{
gv.setNumColumns(1);
gv.setSelection(firstPosition);
}
else {
gv.setNumColumns(2);
gv.setSelection(firstPosition);
}
}
});
Now the problem is on every 4th switch grid view is showing the first element of the arraylist
Right before you call setNumColumns(), save the GridView's first visible position:
int firstPosition = gridView.getFirstVisiblePosition();
Then, after you change the number of columns, pass that integer to setSelection():
gridView.setSelection(firstPosition);
"Selection", counter-intuitively, is not the same thing as "activation". It will ensure that the view is on-screen, but not visibly affect it in any other way.
I have been experiencing a problem with my custom adapter and have located the problem solution. However I do not know how to go about it. I have researched and seen a few examples of a listview custom adapter at which buttons are given tags like viewHolder.button.setTag(Tag) and I understand what the tag does but I am unsure as to how to use it. My questions are: When I set the tag on a button, how does the application differentiate my buttons from another if all the tags are set the same? Also, say I have an onClick method in my custom adapter, how do I use the tag that I set to the button to identify the button that was clicked? I've kind of seen similar adapters on the internet but not exactly , a link to an example would be greatly appreciated also.
I am unsure as to how to use it
tag is a mechanism to make your views remember something, that could be an object an integer a string or anything you like.
My questions are: When I set the tag on a button, how does the
application differentiate my buttons from another if all the tags are
set the same?
i do not understand this question but i think if you notice that your button has a memory and it calls tag you can use it in a better way. if all of your buttons memory(tags) are the same so you can not use tags to distinguish the buttons you must use ids.
say I have an onClick method in my custom adapter, how do I use the
tag that I set to the button to identify the button that was clicked?
you must set different tags for your buttons or grouped them logically and set different tags for each group then in your onClick method use the tags to identify your buttons group:
OnClickListener myButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Object obj = arg0.getTag();
if(obj instanceOf groupOneTagObject){
// do action for group 1
}else if(obj instanceOf groupTwoTagObject){
// do action for group 2
}
}
});
I have list view. I need to set delete button to each row. Is there any standard way to
do this in android?
Thank you very much.
Android way is to not have a delete button but an action based on long press. So when you long press on the list it will show a list of stuff you can do and include delete in it. For example, you can check your Gmail, sms and long press on the thread to get this option.
You need to write your own list view item layout, which includes a delete button, see here for some details on that. In the button XMl you need to have the android:onclick attribute set, then in your code have an appropiate matching method. for example
<button android:onclick="deleteItem" android:text="Delete" ...>
public void deleteItem(View view) {
... code to delete...
}
You have to do xml file with single row (with button) then make class which extends some Adapter for example:
arrayAdapter = new MyArrayAdapter(this, R.layout.news_row, newsList)
where MyArrayAdapter extends ArrayAdapter, news_row is xml with view of single row, newsList is ArrayList with data
I'd like to make a list adapter that formats views like this:
I want to be able to fire a different onClick when the user clicks the image. I have defined the onClick on the image itself in the getView() override, but how do I then get the position of the line that was clicked so I can update the record in the database to record the action?
First, you need the ListView which represents the adapter. If you store this somewhere already, great; if not, you can take the View which is passed to onClick() and call its getParent() method twice (or more, if the image is nested deeper within the clicked item's view) to get the ListView.
From there, call ListView.getPositionForView() on the View passed into onClick(). This will give you an int representing the position of the clicked item within the list adapter. From there, you can do whatever you want with it.
For example:
public void onClick(View v){
ListView lv = (ListView)(v.getParent().getParent()); // you may need more getParent()s and/or extra casting
int position = lv.getPositionForView(v);
/* Do whatever database stuff
* You want to do
*/
}