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.
Related
I have some pretty simple xml defining a ListView item. Each item contains 2 TextView widgets and a Button. I only added the Button yesterday, and suddenly taps on the ListView item itself no longer produce onItemClick() events.
I have reproduced this carefully and simply removing the Button entry in the XML for the ListItem allows the onItemClick() events to be fired. Answers to other SO questions showed it is possible for controls placed on a ListView item could capture or otherwise prevent tap events from firing the listener (see here for example). So, hoping to find a workaround I added the following 3 lines to my TextView controls to no effect:
android:focusable="false"
android:textIsSelectable="false"
android:clickable="false"
My xml displays everything properly and is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linLyt"
>
<TextView
android:id="#+id/tvVal1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_gravity="left|center_vertical"
android:text="0."
android:textSize="16sp"
android:maxLines="1"
android:layout_weight=".15"
/>
<TextView
android:id="#+id/tvVal2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:gravity="left"
android:layout_gravity="center_vertical"
android:text=""
android:textSize="16sp"
android:maxLines="1"
android:layout_weight=".55"
android:focusable="false"
android:textIsSelectable="false"
android:clickable="false"
/>
<Button
android:id="#+id/btnClickMe"
android:layout_width="0dp"
android:layout_height="40dp"
android:gravity="right|center_vertical"
android:layout_gravity="right"
android:textColor="#color/CornflowerBlue"
android:text="Click"
android:minHeight="0dp"
android:minWidth="0dp"
android:textSize="16dp"
android:maxLines="1"
android:layout_weight=".3"
android:background="?android:attr/selectableItemBackground"
/>
</LinearLayout>
Again, just simply remove or comment out the Button xml and suddenly the listener begins firing again.
Add this :
android:descendantFocusability="blocksDescendants"
in your root layout of listview to make the listview Item click listener to work. blocksDescendants means that the ViewGroup will block its descendants from receiving focus.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:id="#+id/linLyt"
>
in code:
yourListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item click
}
});
Now for Button click listener in listview row: You can set the onClick() event in your custom adapter's getView() method.
I have a ListView with three TextViews:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtProductName"
android:text="name"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="0dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/txtOPrice"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtOPrice"
android:text="1.60"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
android:clickable="true"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_toLeftOf="#+id/txtNetPrice"
android:textAlignment="center" />
<TextView
android:id="#+id/txtNetPrice"
android:text="1.61"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:textStyle="bold"
android:textColor="#android:color/white"
android:layout_alignParentRight="true"
android:textAlignment="center" />
</RelativeLayout>
I'm handling the ListView item click event as follows:
adapter = new productListAdapter(this,arrayListProducts);
listProducts.setAdapter(adapter);
listProducts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//how to know which textview was clicked?
}
});
I just want to know how can I determine which TextViews were clicked?
I'm a novice, searched a lot but found nothing.
I would appreciate any kind of help. Thanks, in advance!
When you receive the callback, the view parameter is the clicked view item, and i is the position in your listview. However, view is a reference to the whole list item, meaning the RelativeLayout in your case. From there you can find the specific TextView that you need using findViewById() on the view parameter, this will return you the desired TextView within the list item
You can set on click listener on all the 3 textviews of layout as well as set the tag to all the views. So by using tag and id u can get which view is clicked.
Why not use listview with custom adapter and add clicklistners to each of the textview.
I'm trying to add a ListView that has an ImageButton in it. Everything works except my onItemClick() isn't called. I've googled around and searched on StackOverflow to find suggestions of
android:descendantFocusability="blocksDescendants"
and
android:focusable="false"
I've tried both of these but neither work. This my layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/listview_item_height"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_title_text_size"
android:textColor="#color/text_primary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
<TextView
android:id="#+id/song_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/listview_item_subtitle_text_size"
android:textColor="#color/text_secondary"
android:ellipsize="end"
android:singleLine="true"
android:lines="1" />
</LinearLayout>
<TextView
android:id="#+id/song_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<ImageButton
android:id="#+id/overflow_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:background="?android:selectableItemBackground"
android:src="#drawable/ic_action_overflow"
android:scaleX="0.75"
android:scaleY="0.75" />
</LinearLayout>
Then my onItemClick() simply logs the id of the element clicked.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
long id = adapter.getItemId(position);
Log.d(TAG, id + "");
}
I never get the output in the console though. I do get the selectable background when I click on the item though. I did not get that until adding "blocksDescendants" to the code. Any help is appreciated as I've spent a bit of time on this already.
You can use imagsView instead of imageButton and do onClick actions when list item is clicked
Or
You set onClickListener after layout is inflated in view adapter.
Read this: http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html?m=1
Also this should be set like this to let system listen for clicks:
ListView focusable
android:focusable="true"
Button not focusable
android:focusable="false"
Or
You can use this in xml. On click, it will look for your method in same activity. Doesn't work for fragments.
android:onClick="myMethod"
Have you tried using an ImageView instead? That way by default it won't intercept your clicks.
Or you could try playing with onInterceptTouchEvent.
http://developer.android.com/training/gestures/viewgroup.html
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.. :)
I have a small Android APP with a listview in it. Sometimes the listview does not react on my inputs. I can see that I touched on one item (it changes the color as I touched it), but the program does nothing.
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false" >
<TextView
android:id="#+id/textViewRowLable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textSize="50sp" />
<TextView
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/textViewRowLable"
android:gravity="right"
android:textSize="25sp" />
<TextView
android:id="#+id/textViewRemainingTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/textViewTime"
android:layout_toRightOf="#id/textViewRowLable"
android:gravity="right"
android:textSize="25sp" />
</RelativeLayout>
This is the listner:
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
}
});
Could someone give me a hint why it is not always working?
Thank you
Try using android:descendantFocusability="blocksDescendants" on your RelativeLayout
Also go through this tutorials:
Listview tips and tricks
I had a similar problem. Than I looked at the Android source and found the reason.
In the click handling, there is the following line
if (mDataChanged) return;
So the click is discarded, when the internal list data has changed.
I have updated the list very often. Reducing the update interval solved the problem nearly (after the change, only 1 of 100 clicks was not recognized)