2

I have a listview and each item of the lisview is a linearlayout. Each one of the linearlayouts contains 3 textviews.

How do i set a onclicklistener for those textviews?

i tried this:

TextView tv=(TextView)findById(R.id.textView1);
tv.setOnClickListener(...);

This throws me a nullpointerexception.

I also tried setonitemclickedlistener for the listview,but this only allows me to operate on the linearlayout,not the textview.

thanks in advance.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
  • are you using an Adapter? if so can you edit your question and post your getView() method? inside there is where you'll need to set your listeners. – FoamyGuy Jul 24 '12 at 01:35
  • im using the provided api SimpleAdapter.i didnt override any methods.if i simply add a listener to each of the textviews,would it be very inefficient? – Fermat's Little Student Jul 24 '12 at 01:42
  • it is less efficient than not doing so. But if you need to get click events for all 3 separately in every row in your list that is your only choice (that I know of). Also I don't think it will cause a severe performance degradation or anything if that is what you were getting at. Can you post your activity code? I can try to help you change it to work with a custom adapter that will set click listeners for you on the TextViews. – FoamyGuy Jul 24 '12 at 01:50

3 Answers3

2

If this is needed statically and your view is XML based, this is what I did:

<TextView
    ...
    android:clickable="true"
    android:onClick="myHandler"
/>

This calls myHandler whenever the textview is touched/clicked. As you are using this in a list view, you will still need to add a tag in getView() and use that in myHandler() to figure out which row/field were pressed.

Hope this helps.

Aviral
  • 1,141
  • 1
  • 10
  • 16
0

For getting this requirement you must you use custom adapter class, so that you can get this very easily. using custom adapter is very easy and simple process.

click on this link, its simple application, and In place of Buttons you can use TextView.

Have a nice day..

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

you should go for creating custome adpter like Use BaseAdpter and here pass your list and set that list according to position to Textview and here you can set onClick event also you can create base Adpter like ex (1)

public class GridAdpter extends BaseAdapter 
{

List<String> checkednamelist;

public GridAdpter(Context c, List<String> myitem) {

this.checkednamelist =myitem
}

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

            //Here change with yur row xml file name and  3 textview control id name
    View grid;
    if (convertView == null) {
        grid = layoutInflater.inflate(R.layout.row_grid, null);
    } else {
        grid = convertView;
    }

    TextView textView2 = (TextView) grid.findViewById(R.id.txtlable2);    
    textView.setText(checkednamelist.get(position);
    textView2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // do here as per your require

        }
    });

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return myitem.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return myitem.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public List<String> mycheckeditem() {
    return checkednamelist;
}

}

// finally set this adpter with your listview

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45