2

私には がlistviewあり、リスビューの各項目はlinearlayoutです。linearlayouts のそれぞれには、3 つのテキストビューが含まれています。

これらのテキストビューに設定するにはどうすればよいonclicklistenerですか?

私はこれを試しました:

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

これは私をスローしますnullpointerexception

リストビューの setonitemclickedlistener も試しましたが、これにより、テキストビューではなく、線形レイアウトでのみ操作できます。

前もって感謝します。

4

3 に答える 3

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.

于 2012-07-24T07:00:28.853 に答える
0

この要件を取得するには、カスタム アダプター クラスを使用する必要があります。これにより、これを非常に簡単に取得できます。カスタムアダプターの使用は非常にeasy and simpleプロセスです。

このリンクをクリックすると、その簡単なアプリケーションが表示され、代わりにButtonsを使用できますTextView

良い1日を..

于 2012-07-24T03:02:11.617 に答える
0

Use BaseAdpter のようなカスタム adpter を作成する必要があります。ここでリストを渡し、位置に応じてそのリストを Textview に設定します。ここで onClick イベントを設定することも、ex (1) のようなベース Adpter を作成することもできます。

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;
}

}

// 最後に、リストビューでこのアダプタを設定します

于 2012-07-24T07:10:25.653 に答える