Android listview, current item counter - android

In my android application I have a listview which displays dynamic data according to the user query.
The problem is I need to display in the footer, which Item the user currently have selected(while Scrolling through the listview).
I Implemented OnItemSelectedListener and can catch the scrolling position.
I initially defined a global variable and asigned it to -1.
int selectedRowIndex = -1;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
selectedRowIndex = arg2;
footer.setText(String.valueOf(selectedRowIndex+1)+ " of " + String.valueOf(companyList.size()));
}
The problem is, when the page initially load and even no item have yet selected(Scroll), the selectedRowIndex value had become 0(The first Item in the listview). I cant understand why it happen and can anyone suggest a way to implement this .
Thanks inadvance
Edits
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:text="TextView"
android:id="#+id/noCompanysFound_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="italic"
android:layout_marginTop="10dp">
</TextView>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/companylistView"
android:listSelector="#drawable/item_focus_bkg"
android:layout_above="#+id/footer_linearLayout"
android:focusable="false">
</ListView>
<LinearLayout android:id="#+id/footer_linearLayout" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="#drawable/bottom_bkg" android:gravity="center_horizontal">
<TextView android:text="Footer Text"
android:id="#+id/companylistFooterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</RelativeLayout>

I think whenever your activity created your listview get Focused and the first row will be selected that's why listview's onItemSelected() will be surely called once and the index of first row of list is 0 so in code
selectedRowIndex = arg2; became selectedRowIndex = 0;
If I am wrong then give me suggestion or let me know.

Related

Get TextView string resource from a selected Listview item

I didn't understand ListViews, so I read and copied the code from this website to get me rolling: https://www.sitepoint.com/starting-android-development-creating-todo-app/
The website didn't provide a comprehensive breakdown of the code, but it all worked flawlessly so I've kept it as it is, bar adding 3 additional views (That are not yet used by the ListView database) and removing the delete button from item_todo.xml; I also removed the delete button's corresponding code in MainActivity.
So my item_todo.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutItemToDo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="6dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text="Mark Skidder"
android:typeface="serif"
android:textSize="40sp" />
<TextView
android:id="#+id/characterWeightDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/characterNameDisplay"
android:layout_below="#+id/characterNameDisplay"
android:text="120/213"
android:typeface="serif"
android:textSize="20sp" />
<TextView
android:id="#+id/characterLevelDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/imageView2"
android:layout_below="#+id/characterNameDisplay"
android:text="Level 14"
android:typeface="serif"
android:textSize="20sp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/characterWeightDisplay"
android:layout_centerHorizontal="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="25dp"
android:background="#color/colorGrey" />
</RelativeLayout>
And my MainActivity looks like the code I linked on the website, minus the "Deleting Tasks" chapter.
My question is:
When I click on a given list element, how do I get that ListView's position within the list, and then, how do I extract the corresponding text from the task_title TextView as a string?
If I undersand your issue correctly so you can extract a string from selected row in list view by this code.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
TextView textView = (TextView)view.findViewById(R.id.task_title);
String text = textView.getText().toString();
}
});
Just set OnItemClickListener on your listview. this method will be invoked each time you click on any item in your list.
Inside OnItemClick you have the view of selected row so you can extract any child view from this layout and get your text.
Hope it helps.

Click item in listview does not work

When I click on any list item nothing happens, can someone please help or tell me where the problem is...?
Here is my Listview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="#+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="5dp"
android:choiceMode="singleChoice" />
</RelativeLayout>
Here is my Listview Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/customButtonLayout"
android:layout_height="50dp"
style="#android:style/Widget.Button"
android:focusable="false"
android:layout_width="200dp">
<! hoofstuk_id is hidden and is only for reference >
<TextView
android:text="id"
android:id="#+id/hoofstuk_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</TextView>
<TextView
android:textColor="#000"
android:text="nommer"
android:layout_height="wrap_content"
android:id="#+id/custom_row_hoofstuktext"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp">
</TextView>
</LinearLayout>
</LinearLayout>
Here is my OnItemclicklistener in the activity:
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers =((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
Ive looked at a few other solutions but nothing works in my case, I added this code but it makes no difference when I use it or not:
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
Yout list item is taking focus. Set below line in LinearLayout Tag in my Listview Item xml file
android:clickable="false"
Your intent will fire without taking focus and work properly I ahve checked it... Let me know once its works for you...
Use AdapterView to get adpater item views here you should use parent to find TextView
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers =((TextView)parent.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
Here is your solution:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="#+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="5dp"/>
</RelativeLayout>
In your listview item you have to make this changes:
<LinearLayout
android:id="#+id/customButtonLayout"
android:layout_height="50dp"
style="#android:style/Widget.Button"
android:layout_width="200dp">
<! hoofstuk_id is hidden and is only for reference >
<TextView
android:text="id"
android:id="#+id/hoofstuk_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</TextView>
<TextView
android:textColor="#000"
android:text="nommer"
android:layout_height="wrap_content"
android:id="#+id/custom_row_hoofstuktext"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp">
</TextView>
</LinearLayout>
</LinearLayout>
This should solve your question if not just hit comment on this answer. After that you will start getting OnItemClick() event of listview.
I removed
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
and then applied
android:clickable="false"
it did not work but after fiddling around I found that
android:clickable="false"
android:focusable="false"
in combination did the trick!
This was applied in the listview item just after LinearLayout.
I tested and can confirm it works with Gridview as well.

OnItemLongClick event of listView Android

I have a list view with custom cell layout. Actually it shows data from a table, there are two button one for editing and the other is for deleting the record. these two buttons are hidden, when long click on the row then these two buttons shows up .
Here is the cell_layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Code and Name "
android:textSize="16sp"
android:textColor="#ff000000" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginRight="25dp">
<TextView
android:id="#+id/txtCusCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cus code"
android:textSize="16sp"
android:textColor="#ff000000" />
<TextView
android:id="#+id/txtCusName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cus Name"
android:textSize="16sp"
android:textColor="#ff000000"
android:layout_marginLeft="10dp" />
</LinearLayout>
<ImageView
android:id="#+id/imgbtnOrderActions"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/down"
android:layout_alignParentEnd="false"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="#drawable/test"/>
</RelativeLayout>
<TableLayout
android:id="#+id/tblLayoutOrderAction"
android:layout_width="fill_parent"
android:layout_height="0dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/lmgbtnOrderEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="#drawable/edit"
android:layout_weight="1"
android:layout_column="1"
android:clickable="true"
android:background="#ff00b4df" />
<ImageView
android:id="#+id/ImgbtnOrderDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="#drawable/delete"
android:layout_weight="1"
android:layout_column="2"
android:background="#ffff625a"
android:clickable="true" />
</TableRow>
</TableLayout>
</LinearLayout>
those two buttons are in Table Layout i give them 0dp height fro hiding them.
And this is OnLongItemClick event of listView :
lstviewOrders.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l)
{
final TableLayout tblLay = (TableLayout) view.findViewById(R.id.tblLayoutOrderAction);
TableLayout.LayoutParams lay = new TableLayout.LayoutParams(30, ViewGroup.LayoutParams.MATCH_PARENT);
tblLay.setLayoutParams(lay);
return false ;
}
});
Here comes the problem . When an item in listview is long clicked then it shows the edit and delete button of that items but it also shows those button in the item which is at next 7th position . For example if i click item on position 3 then button of 3,10,17,.... are also showed ...
how to get ride of this problem ???
This sounds to me like you're dealing with the ListView's view recycling feature. This answer provides a great explanation.
Basic example: if a ListView has a total of 20 items but only enough room to show 4 on screen at the same time, then the ListView will only use 4 view objects but recycle them for each item in the list. So if you change something on view 2, then scroll down you'll find that this change also applied to view 6. This is what makes dynamic views difficult with ListViews.
In the above example, if your adapter is loading view 6, in your adapter's getView method, the convertView object is the view from 2, which is then repurposed for data element 6. I would play around with storing whether the buttons are shown in your data and resetting the convertView in this method and then showing/hiding the buttons based on the underlying data.
ListViews handle focus on displaying underlying data, but not necessarily editing that data in a view.
You could try skipping the part of your getView that tries to use the convertView so that you're always making a new view, but I'm found this can cause some other unexpected UI experiences. Good luck!
The problem is occuring because listview cells are reusing.
And when you are showing the button for a particular cell and scroll the list view then the item which is reusing that cell will also show the buttons.
To avoid the problem what you can do is take a position in adapter by any int variable and update the posion on long press in adapter.
In getView method of adapter put the position check if items are having same postion will show the button else it will set visibilty GONE.
Like in adapter : -
int selectedposition = -1;
public View getView(.........){
// your code
if (position == selectedposition){
button.setVisibility(VISIBLE);
}
else{
button.setVisibility(GONE);
}
return convertedView;
}

How to pick a value from a TextView into a ListView

this app is a simple phonebook ,this is the xml file for contact
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:scaleType="fitCenter"
android:layout_toLeftOf="#+id/tvName"
android:layout_toStartOf="#+id/tvName" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
</LinearLayout>
<TextView
android:id="#+id/tvName"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="right"
android:layout_below="#+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<TextView
android:id="#+id/tvPhone"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_below="#+id/tvName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
</RelativeLayout>
and contacts are contained in this listView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MyActivity">
<ListView
android:id="#+id/listPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
how can i pick up the value of the textfield called tvPhone when i click on his contact on the ListView ?
If you just want to get the information for the TextView, you do as this :
String tvValue = tv.getText().toString();
If you loading the values from an adapter, and if you are storing the values in an array list, you will have something like this in the adapter side in the getView() method:
yourTextView.setText(mDetailsArrayList.get(position));
And to get the value of that, you will again get the value using:
getText().toString()
Depending on what's your values looks like, you may have values for int, float etc.
In this example, its a String value you are getting back.
Now, if you have a ListView and a textView on the same layout, you will simple get the value from the textView using the above method and add it to you arrayList for example and to Update the listView you will just notify the changes by using notifyDataSetChanged() method.
EDIT:
For an onClick event on a ListView, just use onItemClickListener as this :
yourListView.setOnItemClickListener(listItemClickListener);
public OnItemClickListener listItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String getText = yourArrayList.get(position).getTextFromTextViewMethod();
}
};
Good luck.. :)

Android ListView not scrolling due to use of Fragment instead of ListFragment

I had set up an Activity and a Fragment, and in the layout I had a ListView, which I gave an arbitrary name and accessed via findViewById(). All was working fine, until I noticed that scrolling didn't work for the ListView.
There are many threads here on SO about ListView and lack of scrolling. I used much more time than I care to admit trying out one possible solution after another. Nothing worked, until I stumbled upon the solution, which I'll describe in an answer to this question.
EDIT:
I've deleted the answer I posted to my own question, where I claimed that you had to use ListFragment (or SherlockListFragment) to get scrolling of a ListView to work.
Thanks to Zackehh9lives I've determined that I was wrong in that claim.
The key phrase in his answer is that the stump of code he shows is in the onCreateView() method. After some experimenting, I think the situation is as follows:
If you use ListFragment (or SherlockListFragment) you have to set up the ListView widget in the onActivityCreated() method, and ListView scrolling works.
If you use Fragment (or SherlockFragment) you can set up ListView in onActivityCreated() or in onCreateView(), but if you do it in onActivityCreated() then scrolling doesn't work.
That was my problem - I was using SherlockFragment and setting up ListView in onActivityCreated().
I have a ListView in a SherlockFragment and it scrolls just fine. Not sure what the issue is? Perhaps you just have a something incorrect somewhere (might want to post your code?). Below is my ListView and XML:
MyFragment.java:
// Find the ListView we're using
list = (ListView)view.findViewById(R.id.listView);
// Set the vertical edges to fade when scrolling
list.setVerticalFadingEdgeEnabled(true);
// Create a new adapter
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.title, questions);
// Set the adapter to the list
list.setAdapter(adapter);
// Set on item click listener to the ListView
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Do stuff
}
});
All that code is all I have related to my ListView in the Fragment, and it's in my onCreateView(). Below is the XML for it.
ListItem.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/arrow"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/title"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title"
android:layout_alignParentBottom="true"
android:layout_below="#+id/title"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="#string/date"
android:textColor="#7F7F7F"
android:textSize="12sp" />
</RelativeLayout>
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="#string/app_name"
android:src="#drawable/arrow_next" />
ListView.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:paddingTop="-2dp" >
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
If you post your code, we have a better chance at helping :)

Categories

Resources