Set Button Onclick In Listview Android
i have a listView with list item like this. i want to set an action at button 'Edit' and 'Delete'. this is my code. listitem.xml <
Solution 1:
Try to use BaseAdapter Instead of ListAdapter it will be easier
Here i'm giving sample code you can implement like this in your code,
publicclassBaseAdapContactextendsBaseAdapter {
List<String> liName;
Context con;
publicBaseAdapContact(List<String> liName, Context con) {
this.liName = liName;
this.con = con;
}
@OverridepublicintgetCount() { // TODO Auto-generated method stubreturn liName.size();
}
@Overridepublic Object getItem(int position) {
returnnull;
}
@OverridepubliclonggetItemId(int position) {
return0;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
LayoutInflaterlif= (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lif.inflate(R.layout.list_view, null);
TextViewtxt_Name= (TextView) convertView.findViewById(R.id.txtName);
Buttonbtn_Call= (Button) convertView.findViewById(R.id.btnCall);
txt_Name.setText(liName.get(position));
btn_Call.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
//Here perform the action you want
}
});
return convertView;
}
}
Solution 2:
As per my aspect you should initialized
your list in setUpView();
list = (ListView) findViewById(R.id.list);
Your app is crash at
list.invalidateViews();
list.setAdapter(adapter);
in onPostExecute(Void result)
at this line list==null
Solution 3:
Where is your list adapter???
You should set onClickListner
on a button inside your list adapter
Solution 4:
you should use interface to listen Click Event.
Here is example,
First, you need to create a interface in adapter.
publicinterfaceonListItemClickListener{
voidonEditClick();
voidonDeleteClick();
}
And then implement onListItemClickListener in your registerItem activity. It will need to implement two methods in your activity.
in click event from your adapter, for ex
btn_edit.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
//you will contain Context from your parent activity//cast your context to listener coz it implement that listeneronListItemClickListenerlistener= (onListItemClickListener) mContext;
// you can add parameter in method
listener.onEditClick();
//Done this will call your method in your activity.
}
});
Hope this will help you.
Post a Comment for "Set Button Onclick In Listview Android"