So I am trying to add a Button some functionality with an onClickListener but I can't seem to get it to work.
My button is declared in the XML of the list item and my list is implemented within a Fragment.
I have done some research to find some answers and every solution I tried it either failed or it was too complicated. I am kind of starting so sorry for my ignorance.
-
1Please post your code.Vamsi– Vamsi2015-03-01 19:27:48 +00:00Commented Mar 1, 2015 at 19:27
-
Post your activity and the list item code.Psypher– Psypher2015-03-01 19:29:27 +00:00Commented Mar 1, 2015 at 19:29
Add a comment
|
2 Answers
in your adpater class find the button by id then in same class write button.setOnClickListner. it would work
If you want the click even in your frament class the you have to write the interface in adapter class and extend your fragment by that interface then use the interface method for click
if you need click in fragment class with index/position then use the belo sample code
//Inside adpater class
public interface OnEditLocationListener {
public void onDeleteClick(int position);
}
private OnEditLocationListener listener;
public void setOnEditLocationListener(OnEditLocationListener listener) {
this.listener = listener;
}
//Inside getView method
holder.imv_delete.setTag(position);
holder.imv_delete.setOnClickListener(null);
holder.imv_delete.setOnClickListener(new OnClickListener() {
@Override
public void onDeleteClick(View view) {
if (listener != null) {
listener.onDeleteClick(position);
}
}
});
//In Activity
yourclass extends yourchoice implements adapter.OnEditLocationListener
//inside onCreate/onCreateview
adapter.setOnClickListener(getActivity/this);
//Button click
@Override
public void onDeleteClick(int pos) {
Toast.makeText(getActivity(), "clicked @ " + pos, Toast.LENGTH_SHORT).show();
}
1 Comment
Shadik Khan
@abforce You were right so I updated my code. please check it. Now you can use current row index or anything else b passing it in onDeleteClick method in adapter
Hello ) First you must create your custom adapter. For example :
public class EatListAdapter extends BaseAdapter {
private ArrayList<EatTime> eatTimes;
private Activity activity;
public EatListAdapter(Activity activity, ArrayList<EatTime> items) {
this.eatTimes = items;
this.activity = activity;
}
@Override
public int getCount() {
return eatTimes.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
Button delete = (Button) v.findViewById(R.id.button_delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your ON CLICK CODE
});
return v;
}
}
Than set adapter
ListView eatingList = (ListView) findViewById(R.id.eatingList);
adapter = new EatListAdapter(this, eatArrayList);
eatingList.setAdapter(adapter);