Creating a split button for a custom ListView adapter - android

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
*/
}

Related

If I use simple adapter to feed the listview and the listview contains textview and button so how can I perform operation on specific position?

i want to find the position of the view which is the part of the list-view's custom XML file. How can find the position of the views. Can i automatically perform onItemClick?
I face this difficulty. In my case when i click on textview which is part of list-view's custom XML file then the position is right, but when i click on the button which is the part of list-view's custom XML file then it always gives first position.
After i have to decided to perform automatic onItemClick and store all the values of clicked position into another declared variable which is below.
See the image i used kotlin language instead of java. I stored all the map values into another variable which is declared above with public access specifier.
[Now when i perform operation on Button click i use
lvShayari.performItemClick(v, lvShayari.getPositionForView(v), lvShayari.selectedItemId)
this statement in which lvShayari is listview and performItemClick() method perform the onItemClick automatically when we clicked on button. Here v is the view, lvShayari.getPositionForView(v) method gives the position of v in my case it is button.]2
check out below code , hope it will help you:
listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
Toast.makeText(this, "Click on: " + position, Toast.LENGTH_SHORT).show()
}
for more Details follow this link
#Suvidha Malaviya
Make your own custom adapter for your listview with one interface in which there is one method with three parameters namely View, Position, Model class of your list which you are passing in your adapter.
After making this interface initialize that interface in your adapter and set that interface method in your view click event.
Now set this interface in your activity or fragment where you are using your custom adapter and get the value which you want...
For example, see below link in which there is explain how to use custom listener in list view adapter. Here you have to just make your own click listener in which you have to pass three parameters which I have mention above.
Try it. I hope you will resolve your issue...

OnItemClickListener use for TableRow

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

Display item on click on ListView

I need help on a specific task :
I have a ListView in my main activity with a custom Adapter associated, and an OnItemLongClick listener that displays a Dialog with many operations configured (change quantity, cancel item...) and it works like a charm.
My goal is to have a physical copy of the clicked item in the Dialog, to display immediately the changes made.
Since my ListView item is complex and designed by the Adapter, i can't just get the reference to the original object, i need a visual copy of the selected ListView item...
I thought about creating the same structure (ListView and Adapter) with only the selected item associated but it's kind of heavy...
Is there a simpler solution that can help me in this situation ? I mean real copy of the original ListView Item with changes made in my Dialog done to the original object...
Posting your relavent code may help us to guide you more clearly.
However you can get your object in onItemLongClick((AdapterView, View, int position, long) using position params i.e
YourObject objReference = yourActivityClassObjectList.get(position);
now when showing dialog, pass this objReference to your dialog and when changes made, change the contents of this objReference and call
adapter.notifyDataSetChanged();
instead of passing the objReference to dialog, you can pass int position and while changes made, get the reference to that object, change contents and notify data set changed to Adapter.
When you click on one listview item the onListItemClick callback should be fired. As you see in the doc the third parameter is position. You can call YourAdapter.getItem(int) to retrive the element hold by the listview at position

how to access ListView with CheckedTextView?

I'm now developing an application that uses a ListView with a
CheckedTextView on every item that managed by an ArrayAdapter to
support multiple chooses. The contents in my ListView are dynamic, that
means, can be changed during runtime. Now I try to use
ListView.getCheckedItemPositions() to get all checked items, Because I want
to save all the checked positions and auto-check them when user go back to
this page again. So I need to save checked results for every page.
For the first page everything works fine as expected. But when user goes to
another page and make some chooses, the result array that ListView returned
contains some positions that are never checked. I don't why ListView has
this strange behavior. Even for a page that in fact no checked happens but
ListView gives me a result that indicates there's one item has been checked.
could anyone who can teach me how to get the position of CheckedTextView
in its OnClickListener callback?
example code is appreciate.
Thanks in advance...
The listview recycles its views so when you go to a different page and then return to the previous page, the listview recalls the getView() function for its views. To make sure that the order of the checked views are not mixed up, create an arraylist that contains the check state of all the views before initializing the adapter. Then pass the arraylist as an argument for the adapter's constructor. There, in the getView() function, set the checked state of each checkable textview based on the arraylist. Then, return to the activity class and override the onItemClick() event. Using the view that is given to you when the function is called, do the following to get the checkable textview and set its checked state:
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
{
CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
if (tv.isChecked())
{
tv.setChecked(false);
checkStatesOfViews.get(position) = false;
}
else
{
tv.setChecked(true);
checkStatesOfViews.get(position) = true;
}
}
});

How to Identify a ListViewItem to Update a Single Row

I have a ListView displays information about an object. When I click a ListView Item, I open an Activity that let's me manipulate parameters of the object held in the adapter. These parameters are updated and stored remotely.
When I return to the ListView (via the back button), I want to update the ListView Item that I clicked originally by requesting the parameter values from the remote server.
I am currently doing this up updating the entire ListView by clearing it and rebuilding it.
How do I reference the ListView Item so that I can update the data for that item only?
Thanks,
Jason
Just set the data for your adapter and call notifyDatasetChanged. Android only draws the rows that are visible anyway, so it's pretty efficient.
Knowing the position of the view, this can be done.
View singleItemView = myListView.getChildAt(position);
ImageView icn = (ImageView) singleItemView.findViewById(R.id.icn_icon);
ProgressBar prg = (ProgressBar) singleItemView.findViewById(R.id.prg_icon);
icn.setVisibility(View.GONE);
prg.setVisibility(View.VISIBLE);
(EDIT)
This doesn't work because the "position" of the View could be changed upon returning from another Activity. After the onResume() fires, getView() is called and the position of the visible ListView Items are numbered starting with 0. So, the ListView items (id and position) become renumbered based on visiblity. As a result, I end up accessing the wrong object.

Categories

Resources