Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top