Reputation: 71
I have an activity which contains only a listview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@+id/list_data"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>
</LinearLayout>
Each row of this list is styled by this xml file. Each row has 2 textviews and a button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/playPauseB"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Play" />
<TextView
android:id="@+id/rec_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="20sp"
android:layout_toRightOf="@id/playPauseB" />
<TextView
android:id="@+id/rec_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rec_name"
android:text="Sample Data 2"
android:textSize="15sp" />
</RelativeLayout>
This listview has a cutom cursor adapter. My question is how to setup onClick listener for each button in a row. And how to access buttons of each row? My activity has a DBHelper object and customCursor object.My adapter contains a bindView and newView methods. Please comment if more details are needed.
Upvotes: 0
Views: 1194
Reputation: 992
You can try this
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View hView = convertView;
final ViewHolder holder;
if (convertView == null) {
hView = mInflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.text1 = (TextView) hView.findViewById(R.id.text1);
holder.text2 = (TextView) hView.findViewById(R.id.text2);
holder.button = (Button) hView.findViewById(R.id.button);
hView.setTag(holder);
}else{
holder = (ViewHolder) hView.getTag();
}
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// do your work on click...
}
});
return hView;
}
static class ViewHolder {
TextView text1;
TextView text2;
Button button;
}
Upvotes: 0
Reputation: 1967
Change your getView method in CustomAdapter class as below:
@Override
public View getView(final int arg0, View convertview, ViewGroup arg2) {
// TODO Auto-generated method stub
final ViewHolder holder;
if(convertview==null)
{
holder = new ViewHolder();
convertview = layoutinflater.inflate(R.layout.yourXmlName, null);
holder.txt1=(TextView)convertview.findViewById(R.id.rec_desc);
holder.txt2=(TextView)convertview.findViewById(R.id.rec_name);
holder.btn=(Button)convertview.findViewById(R.id.playPauseB);
convertview.setTag(holder);
}
else{
holder=(ViewHolder)convertview.getTag();
}
holder.txt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
holder.txt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
holder.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
return convertview;
}
static class ViewHolder
{
TextView txt1;
TextView txt2;
Button btn;
}
Upvotes: 0
Reputation: 340
Assuming your cursor adapter extends BaseAdapter
;
implement getView method as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
Button playPauseBtn = (Button) view.findViewById(R.id.playPauseB);
playPauseBtn.setOnClickListener(playPauseListener);
}
OnClickListener playPauseListener = new OnClickListener() {
@Override
public void onClick(View view) {
//Do some thing ...
}
};
Upvotes: 0
Reputation: 8745
You can set onClickListener for each row in getView method of your adapter
like this
private View getView(int position, View convertView, ViewGroup parent) {
View row = // initialize your row
Button b = (Button)row.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do something
}
})
}
Upvotes: 0
Reputation: 10969
Simple add click listener within getview(), as you doing in activities.
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pos = (Integer) v.getTag();
Log.d("#####", "tag pos=" + pos);
}
});
You need to set Tag & get Tag to avoid position issue, on click of button.
Upvotes: 3