Android - Listview Get Item View by Position

android - listview get item view by position

Use this :

public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}

How to get list item position in list view?

Set onItemClickListener on your listview and then from the position of clicked item get that item from your dataList.

 listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="
+ lines.get(position))));
}
});

Get ListView item position by uri / by the data it contains

ListView does not support updating a single position.

You must update the adapter with the new data (even if you change a single position) and the, invoke mAdapter.notifyDataSetChanged()

If you get the View by its position (via listView.getChildAt()) will work. However, if you scroll up and down the list, that view will display the old data again because the adapter is not aware of the change (and it is the adapter which update the view contect view getView()).

When you invoke notifyDataSetChanged(), you are telling to the adapter that your data set has new info and the ListView/Adapter will re-draw the visible items (it won't re-draw whole list at once.. only the visible items).

You may want to consider to change to RecyclerView in the future. The BaseAdapter used in a RecyclerView support actions such as add/remove/update a single position.

Get item at position at Android ListVIew

The records in your code are stored in the listView adapter, so in onItemClickListener, get your item from the adapter as shown below :

adapter.getItem(position)

Another approach could be :

(typecast_into_respective_object) parent.getItemAtPosition(position)

Here parent is the adapterView object in setOnItemClickListener.

Android, get item view at a position in the list

In ListViews, the idea is to make changes on the underlying data and let the adapter reflect the changes on the views. You shouldn't try to find the view, instead you should make the changes on data model and handle it on the adapter to reflect the result on the view.

For example, you can add a boolean flag shouldFlash to your Model class and in your adapter data you can set it on the item which you want to flash. In the getView() method, you can check it and flash the view(in your case, you should also reset the shouldFlash flag immediately.).

The key here is to inform the view that the underlying data has been refreshed. In ArrayAdapter, this is notifyDataSetChanged() method while the corresponding for the CursorAdapter is changeCursor() or swapCursor().

Get id / position of ListView row

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;
}
}

How to get the view of a ListView item?

There's no guarantee that any specific ListView item will even have a view at any given time. If the item is currently off-screen, then it may not have a view. Since a specific item might not have a view, it might not make any sense to try to get the item's view.

Beyond that, because of the way ListView creates and reuses views, you'll see some odd, undesirable effects if you simply modify the views directly. As the user scrolls through the list, items that become visible will incorrectly end up with the same backgrounds as other items that have fallen outside the visible portion.

I don't know whether what follows is the best way to implement your functionality because I don't know the cost of rebuilding the list after a change. Here's the (probably naive) way I would do this:

  1. Add another boolean member to your data object, something like isInSecondList.
  2. Override getView() in the Adapter. In getView(), set the background to either normal or highlighted depending on the the value of the item's isInSecondList.
  3. When an item is added or removed from the second list, update the data object to reflect the change, then call the Adapter's notifyDataSetChanged().

Android ListView get current position in visible items

getFirstVisiblePosition() can get first visible position in all items,so you can use getSelectedItemPosition()-getFirstVisiblePosition() to make it.

How to get the item position in a ListView and GridView outside the adapter?

I have found a solution:

if(view.getParent() instanceof ViewGroup) {
position = ((ViewGroup)view.getParent()).indexOfChild(view);
}


Related Topics



Leave a reply



Submit