I’ve a custom ListView containing 4 TextViews. Now, the TextViews have to be Linkifiable. Since Linkify wasn’t working in ListView, I made a callIntent function to see if the link is clickable or not. But if there’s no clickable link, I want to start a new Activity. I use the following code:
lvMembersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final TextView txtLine1 = (TextView) arg1.findViewById(R.id.tv_member_details_line1);
final TextView txtLine2 = (TextView) arg1.findViewById(R.id.tv_member_details_line2);
final TextView txtLine3 = (TextView) arg1.findViewById(R.id.tv_member_details_line3);
final TextView txtLine4 = (TextView) arg1.findViewById(R.id.tv_member_details_line4);
txtLine1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(1);
}
});
txtLine2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(2);
}
});
txtLine3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(3);
}
});
txtLine4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(4);
}
});
}}
Now, when I click on an item for the first time, the TextView listeners are getting set. The individual clicks only works after the second time. What should I do in such a case?
Put this code in the getView() method in the adapter. And in your code onItemClick is called when you click on a row of the listview.
Add the following attribute for each of the textviews(The four textviews in the layout which going to use as the custom list item)
android:onClick="onTextView1_click"
- You have to do this for all the 4 textviews to track the click event seperately-
Then in the code add the following method to trigger when you click the textview1
public void onTextView1_click(View view)
{
final int position = (Integer) view.getTag();
//implementation (Which needs to be done when someone click textview1)
}
Also most importantly You have to add the position as a tag from the getViewMethod in adapter class. this will help to track the clicked item in the listview
holder.textView1.setTag(position);
and you can access this value as I have indicated in the "onTextView1_click(View view)" method "Position variable"
In the List view --->
android:focusable="false"
In the code add the "setItemsCanFocus(true)" soon after initialize list view. This indicates focusable items contains within each item in list view.
lvMembersList = (ListView) findViewById(R.id.NameOfListTView);
lvMembersList.setItemsCanFocus(true); // <---------[X]
Related
I'm new in Android development and I don't understand something, even if is a simple thing: I have a ListView that each item contains a textview and a button. When clicked on a button it fires setOnClickListener inside getView method. Also, I have a setOnItemClickListener adapter on this ListView but that method does not fire when the label is clicked. Why is this happening?
UPDATE
This is how the button click fires:
ImageButton btnToMap = view.findViewById(R.id.btnToMap);
btnToMap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Coordinates coordinate = MainActivity.coordinates.get(position);
Toast.makeText(getApplicationContext(), "coordonata apasata: " + coordinate.latitude + ";"
+ coordinate.longitude, Toast.LENGTH_SHORT).show();
}
});
This is kind of View focus problem everyone newbie suffers from ;)
Simple solution:
Make all the children of your row_layout to
focusable=false, focusableInTouchMode = false and add android:descendantFocusability="blocksDescendants" to your parent.
Use focusable or requestfocus() as per your requirement.
In your xml file where you defined your button add:
android:focusable="false"
You can override onClick method in your Adapter class and check which view was clicked.
#Override
public void onClick(View v) {
if (v.getId() == R.id.button)
mButtonListener.buttonOnClick();
else
mItemListener.itemOnClick();
}
mButtonListener and mItemListener are listeners passed to Adapter in constructor.
private final OnButtonListener mButtonListener;
private final OnItemListener mItemListener;
public MyAdapter(OnButtonListener buttonListener, OnItemListener itemListener) {
mButtonListener = buttonListener;
mItemListener = itemListener;
}
public interface OnButtonListener {
void buttonOnClick();
}
public interface OnItemListener {
void itemOnClick();
}
I used this in my Inventory App you can find source code here
This is the situation: I have a Listview with some items. Inside each item there are two clickable views that are only shown after user clickes in their parent item.
It works like this:
productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
TextView txtAdd = (TextView) view.findViewById(R.id.txt_add_units);
txtAdd.setVisibility(View.VISIBLE);
txtAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
TextView txtRemove = (TextView) view.findViewById(R.id.txt_remove_units);
txtRemove.setVisibility(View.VISIBLE);
txtRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
basketProductsListAdapter.notifyDataSetChanged();
}
});
The first time a listview item is clicked, its txtAdd and txtRemove child views are shown and then if user clicks them, code inside their onclickListener is executed.
The problem appears ONLY IN THE FIRST listview item. First time it is clicked txtAdd and txtRemove are shown, BUT THE FIRST TIME user clicks over the child views, their parent's ItemClickListener is fired again, and then the child views (txtAdd and txtRemove) are not receiving the clickListener. Following times they are work perfectly.The weird thing is that it only happens first time on the first item in the listView.
Any ideas? thanks in advance!
Add this line to your listview xml android:descendantFocusability="blocksDescendants"
The Thing is our child view may be focusable so the first click will be for the focus and after that the itemClick will work. to avoide focusable add that line in the xml
Still if it does not work then try adding
android:focusable="false"
android:focusableInTouchMode="false"
to the child View of the listview
I have a ListView with Custom Adapter. I have seen this thread where people asked if the items in the custom view had a clickable item. And Yes, I have a clickable ImageView in the listrow. So clicking anywhere else(other than that ImageView) should perform some other action. I gave an onItemClickListener to the ListView. However, it doesn't work on first click and works on two-three clicks.
Update:
In my adapter's getView method, I set onClick of ImageView like this:
holder.chatImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff here
}
});
It works fine, and in my activity, I gave onItemClickListener to listView likw this:
onlineListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//Stuff here
}
});
PS: I didn't explicitly give any focus to anything.
Some time ago I've faced the same problem but with a CheckBox, I've resolve it by creating a onClickListener as member of the Custom Adapter. For example
public MyAdapter extends BaseAdapter{
// ... ..
// ... ...
private OnClickListener imageClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
/// your data .getTag()
// process onClickListener for image
}
};
}
And then, in your getView:
if (convertView == null){
//Create your views and register onClickListener
holder.chatImageView.setOnClickListener(imageClickListener);
}
Add android:focusable="false" to your image in xml.
Hope it helps, Here you have the entire example:
I have populated a list view using the custom cursor adapter, That listview consists of a checkbox and button in the below manner.
checkbox1 button1
checkbox2 button2
.
.
.
Now I want to navigate to another activity when click on the button in list view now my issue here is when I click on the button1 then I want to see the data in checkbox1 and similarly for other rows as well.
Now I am confused on how to maintain a sync between the checkbox and button and how to achieve the click functionality. How can I achieve this?
You can do the following on the Button's onClickListener if the Button and CheckBox share the same parent
boldButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
View parentView = (View) view.getParent();
CheckBox checkBox = (CheckBox) parentView.findViewById(R.id.check);
}
});
This will give you the CheckBox corresponding to the clicked Button
You will need to find your corresponding view in the list view implementing the onclickListener
For eg
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Button b = (Button)v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(this,SecondActivity.Class)
startActivity(i);
}
});
//Similarly for the checkbox
}
});
Use a ViewHolder class in adapter class and use that in getView(), Then within getView
viewHolder.yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(viewHolder.checkbox.isChecked())
// toggle the check and do your work here
}
});
You can learn ViewHolder pattern here its very simple
Another example link
This Selectable ListView Tutorial link should help in your solution.
Check this post might help.
You have to preserve the state of row in listview so row can be populated with its state.
Android how to identify item in listview with checkbox
I have a ListView and each row contains button. I don't want to assign to each button its own listener object, cause it will create a lot of objects. But the question is how to determine row number or contextual information when button is clicked?
Every View (including Button) can be "identified" via its setId(int) method.
When clicked you will be passed a view to a View.OnClickListener.onCLick(view). Just extract the ID and you'll know which button this is.
When creating a button:
button.setId(1);
and when this button is clicked:
button.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
int id = view.getId();
// do something with the Id
}
});
Update: answer was updated based on #Octavian Damiean's comment: for identifying Views setId() should be used instead of setTag().
using the
listview..setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int rowNumber,
long arg3) {
// TODO Auto-generated method stub
}
});
we can get the clicked row