Android How to set onClick event in list item of ListView
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(context, SendMessage.class);
String message = "abcpqr";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
try this way.
onClick event on button generated on ListView android
You can set the onClick event in the getView method of the custom adapter class
Android : How to set onClick event for Button in List item of ListView
You can set the onClick
event in your custom adapter's getView
method..
check the link http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html
Assign onClick event for Button in list item of ListView
You have
View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
But you return
return convertView;
Should be
View convertView = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
And
Button buttonEdit = (Button) convertView.findViewById(R.id.item_button);
And
return convertView;
Edit:
As suggested in the comment by blackbelt. You are not using Custom ArrayAdapter
You probably meant
Myadapter adapter = new MyAdapter(ActivityName.this,R.layout.addtooutlet_list_item,mylist);
setListAdapter(adapter);
Then
ArrayList<HashMap<String,String> map;
LayoutInflater mInflater;
public Myadapter(Context context, int resource, int textViewResourceId,
ArrayLsit<HashMap<String,String> map, OnClickListener callback) {
super(context, resource, textViewResourceId, map);
this.map = map;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView ==null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder.b = (Button) convertView.findViewById(R.id.item_button);
convertView.setTaf(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
HashMap<String,String> hm = map.get(position);
holder.tv1.setText(hm.get(postion).get("bus_name").toString());
holder.tv2.setText(hm.get(postion).get("bus_id").toString());
holder.b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("xx", "Button pressed!");
}
});
return convertView;
}
The ViewHodler
static class ViewHolder
{
Button b;
TextView tv1,tv2;
}
Now you should have a textview's in layout.addtooutlet_list_item.xml
and update views in getView.
Also consider using a ViewHolder Pattern
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
set button onclick event for every row of listview
Use a CustomAdapter.
You need to understand how listview works
How ListView's recycling mechanism works
Pass the activity context and the list NewItems to the constructor of the custom adapter.
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
list=(ListView)findViewById(R.id.listupdate);
CustomAdapter cus = new CustomAdapter(MainActivtiy.this,newItemlist);
list.setAdapter(cus);
}
Use a Custom Layout with textviews and buttons. Name it list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="40dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/textView2"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="20dp"
android:text="Button" />
</RelativeLayout>
Inflate the layout, initialize and update the views. Set Listener on the button do what is required.
public class CustomAdapter extends BaseAdapter
{
LayoutInflater mInlfater;
ArrayList<HashMap<String,String>> list;
public CustomAdapter(Context context,ArrayList<HashMap<String,String>> list)
{
mInlfater = LayoutInflater.from(context);
this.list =list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView ==null)
{
convertView = mInlfater.inflate(R.layout.list_item,false);
holder = new ViewHolder();
holder.b1 = (Button)convertView.findViewById(R.id.button1);
holder.tv1 = (TextView)convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView)convertView.findViewById(R.id.textView2);
holder.tv3 = (TextView)convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else
{
holder =(ViewHolder) convertView.getTag();
}
HashMap<String,String> map = list.get(position);
holder.tv1.setText(map.get("name"));
holder.tv2.setText(map.get("description"));
holder.tv3.setText(map.get("price"));
holder.b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return convertView;
}
static class ViewHolder
{
Button b1;
TextView tv1,tv2,tv3;
}
}
Set OnClick Listener on button inside list view in android
In your Adapter try this:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
YourWrapper wrapper = null;
if (row == null)
{
row = inflater.inflate(R.layout.layout, parent, false);
wrapper = new YourWrapper (row);
row.setTag(wrapper);
}
else
wrapper = (YourWrapper) row.getTag();
wrapper.getButton().setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// What you want
}
});
return row;
}
EDIT
Your wrapper class:
public class YourWrapper
{
private View base;
private Button button;
public YourWrapper(View base)
{
this.base = base;
}
public Button getButton()
{
if (button == null)
{
button = (Button) base.findViewById(R.id.your_button);
}
return (button`);
}
}
Change sub=(Button)findViewById(R.id.subscribe_now_btn);
into sub=(Button) mRow.findViewById(R.id.subscribe_now_btn);
How to set click listener for button in listview in Fragment onCreateView()
You could register your onClickListener inside your adapter:
@Override
public void bindView(View view, final Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.name);
ImageButton image = (ImageButton) view.findViewById(R.id.account_menu);
int nameColumnIndex =
cursor.getColumnIndex(accountContract.accountEntry.COLUMN_NAME);
String name = cursor.getString(nameColumnIndex);
nameTextView.setText(name);
int position;
position=cursor.getPosition();
image.setTag(position);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//do something
}
});
}
OnClick Button action in Custom ListView
Add your button's OnClick event in the Pro_Adapter's getView() methond as you do normally in your activities' onCreate() method.
Related Topics
Actionbar Not Shown with Appcompat
How to Change Android Minsdkversion in Flutter Project
Published App on Play Store Can't Communicate with Google Maps API and Facebook API
Android. How Does Notifydatasetchanged() Method and Listviews Work
How to Detect User Pressing Home Key in My Activity
Recyclerview Horizontal Scroll Snap in Center
How to Monitor the Network Connection Status in Android
Android Recyclerview VS Listview with Viewholder
Decimal Separator Comma (',') with Numberdecimal Inputtype in Edittext
Android Fragment Onattach() Deprecated
How to Deploy and Execute an Application on a Device Connected to a Remote System
Unsupportedoperationexception: Can't Convert to Dimension: Type=0X1
How to Change the Color of Alertdialog Title and the Color of the Line Under It
Google Play Services Library Update and Missing Symbol @Integer/Google_Play_Services_Version