Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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..

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top