How to set onClickListener for button for each element in ListView?
When I do something like this:
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.element, parent, false);
}
mButton = (Button) convertView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mButton.setText("Clicked");
}
});
return convertView;
}
And when I press the button I get this errors:
android.content.res.Resources$NotFoundException: String resource ID #0x4c
at android.content.res.Resources.getText(Resources.java:286)
at android.widget.TextView.setText(TextView.java:4148)
at the.vizimir.onezone.adapters.PostListViewAdapter$1.onClick(ListViewAdapter.java:119)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
UPDATE:
I don't get errors but after clicking Button text is chaning not in this element where I press, but in other.
public class PostListViewAdapter extends ArrayAdapter<Post> {
private List<Post> mPostList;
private Context mContext;
Button mPlusImageButton;
TextView mTextTextView;
public PostListViewAdapter(List<Post> postList, Context context){
super(context, R.layout.post, postList);
mPostList = postList;
mContext = context;
}
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post, parent, false);
}
mPlusImageButton = (Button) convertView.findViewById(R.id.plusImageButton);
mTextTextView = (TextView) convertView.findViewById(R.id.textTextView);
String mText = mPostList.get(position).getText();
mTextTextView.setText(mText);
int a = position;
mPlusImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlusImageButton.setText("A");
}
});
return convertView;
}
#Override
public int getCount(){
return mPostList.size();
}
}
Maybe you should try to use:
Button thisButton = (Button) v;
instead of using it straight, if you totally sure that problem is onClick()
So, i've found a time, and made simple example, which works :
MainActivity.class:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> items = new ArrayList<String>();
items.add("1");
items.add("2");
items.add("3");
TestAdapter adapter = new TestAdapter(this, R.layout.list_item, R.id.title, items);
listView.setAdapter(adapter);
}
}
TestAdapter.class(i know i'm not using ViewHolder pattern and so on, i've just made a class base on your example) :
public class TestAdapter extends ArrayAdapter<String>{
private List<String> items;
private Context mContext;
public TestAdapter(Context context, int resource, int textViewResourceId,
List<String> items) {
super(context, resource, textViewResourceId, items);
this.items = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
final Button mButton = (Button) convertView.findViewById(R.id.button);
TextView mTitle = (TextView) convertView.findViewById(R.id.title);
mTitle.setText(items.get(position));
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mButton.setText("clicked");
}
});
return convertView;
}
#Override
public int getCount() {
return items.size();
}
}
So i need all of your code to see what's happening
Edit : Ofcourse you get text on wrong Button, you need to create them in getView(like in my post), and not as field in your adapter class.
you do not need to use buttons.
For listviews use onListItemClick. Its a method built into ListFragment. Otherwise use listview.setOnItemClickListener(); hope that helps. They take parameters ListView l, View v, int position, long id
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mButton.setText("clicked");
notifyDataSetChanged();
}
});
Related
I have a custom listview: http://i.stack.imgur.com/oBCCG.png
Up on clicking an item of 'ListView' the 'ImageView' with that "mark" becomes visible.
But the problem is that I need only ONE item to be marked. How can I deselect all others items up on click?
CustomList adapter:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.listview_item);
viewHolder.postImageView = (ImageView) convertView.findViewById(R.id.image_mark);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position));
viewHolder.postNameView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to deselect all other items here?
if (viewHolder.postImageView.getVisibility() != View.VISIBLE) {
viewHolder.postImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.postImageView.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
class ViewHolder {
TextView postNameView;
ImageView postImageView;
}
}
setting the adapter:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time_zone, container, false);
// Set the adapter
mListView = (ListView) view.findViewById(R.id.listView_timezones);
imageCheck = (ImageView) view.findViewById(R.id.image_mark);
mListView.setAdapter(new TimeZoneItemAdapter(
getActivity(),
R.layout.listview_item,
dataTimezones.getZonesList()
));
return view;
}
Thanks for any help!
You might want to keep track of the currently selected list view item position in a dedicated integer variable. Then you would only need to call notifyDataSetChanged() on the adapter each time an item is selected and set the ImageView as visible in getView() if yourSelectedPosition == position.
Also check out the setOnItemClickListener() method on ListView.
EDIT:
Here's some code.
First in the Activity attach a listener to the ListView:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
timeZoneItemAdapter.selectedPosition = position;
timeZoneItemAdapter.notifyDataSetChanged();
}
});
and the adapter should look something like this:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public int selectedPosition = -1;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = myContext.getLayoutInflater().inflate(R.layout.listview_item, null);
TextView postNameView = (TextView) convertView.findViewById(R.id.listview_item);
ImageView imageMark = (ImageView) convertView.findViewById(R.id.image_mark);
postNameView.setText(datas.get(position));
imageMark.setVisibility(selectedPosition == position ? View.VISIBLE : View.GONE);
return convertView;
}
}
This solution gets rid of the ViewHolder, but would it work for you?
I checked all the possible solutions to handle the focus of EditText in a ListView.
I have a ListView in a fragment. The adapter of this ListView is composed by an ImageButton, an ImageView and an EditText. I want the EditText focusable.
I tried some solutions, one of these was:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity().getApplicationContext(), list);
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
adapter.nome.requestFocus();
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
});
lv.setAdapter(adapter);
return view;
}
}
Then in my XML file i have android:descendantFocusability="beforeDescendants" in my ListView.
Here is my FileAdapter class:
public class FileAdapter extends ArrayAdapter<Files> {
LayoutInflater inflater;
EditText nome;
private final Context context;
private ArrayList<Files> list;
public FileAdapter(Context context, ArrayList<Files> list) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.homeadapter, null);
nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
return convertView;
}
}
Obviously i put here only useful part of my code, as i said before my adapter has also other elements but useless for the question.
The solution i tried doesn't work so i'm searching another.
Yesterday I have the similar problem with my fragment... This is my fragment code:
public static class PlaceholderFragment_MainActivity extends Fragment implements View.OnClickListener {
//Declaration of classes
Floor_Adapter FloorsAdapter;
Floor_List Floors = new Floor_List();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Declaration of controls
View view = inflater.inflate(R.layout.building_view, container, false);
TextView txtvHotelName = (TextView) view.findViewById(R.id.txtvHotelName);
ListView lvFloors = (ListView) view.findViewById(R.id.lvFloors);
txtvHotelName.setText("Hotel");
Floors.setFloorsArray();
FloorsAdapter = new Floor_Adapter(getActivity(),Floors.getFloorsArray(),lvFloors);
lvFloors.setAdapter(FloorsAdapter);
return view;
}
#Override
public void onClick(View view) {
String A;
TextView floor = (TextView)view.findViewById(R.id.txtvFloor);
A = floor.getText().toString();
}
}
And this my adapter code:
public class Floor_Adapter extends ArrayAdapter<Floor_Object>{
//Declaration of atributes
private ArrayList<Floor_Object> Floor_List;
private final Activity context;
private final ListView lvFloors;
public Floor_Adapter(Activity context,ArrayList<Floor_Object> Floors_Array,ListView lvFloors) {
super(context,R.layout.floor_layout,Floors_Array);
this.context = context;
this.Floor_List = Floors_Array;
this.lvFloors = lvFloors;
}
public static class Floor_View{
//Declaration of Atributes
TextView FloorName;
Button btnFloorRow;
ImageView CheckImage;
TextView NoChecks;
ImageView BlockImage;
TextView NoBlocks;
ImageView DirtImage;
TextView NoDirts;
ImageView TroubleImage;
TextView NoTroubles;
ImageButton btnShowTasks;
ImageButton btnShowRooms;
}
#Override
public View getView(int position, View ConvertView, ViewGroup parent) {
//Declaration of Variables
Floor_View rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new Floor_View();
ConvertView = inflator.inflate(R.layout.floor_layout,null,true);
rowView.FloorName = (TextView) ConvertView.findViewById(R.id.txtvFloor);
rowView.btnFloorRow = (Button) ConvertView.findViewById(R.id.btnFlooRow);
rowView.CheckImage = (ImageView) ConvertView.findViewById(R.id.ivCheck);
rowView.NoChecks = (TextView) ConvertView.findViewById(R.id.txtvCheckRooms);
rowView.BlockImage = (ImageView) ConvertView.findViewById(R.id.ivBlockRooms);
rowView.NoBlocks = (TextView) ConvertView.findViewById(R.id.txtvBlockedRooms);
rowView.DirtImage = (ImageView) ConvertView.findViewById(R.id.ivDirtRooms);
rowView.NoDirts = (TextView) ConvertView.findViewById(R.id.txtvDirtRooms);
rowView.TroubleImage = (ImageView) ConvertView.findViewById(R.id.ivTroubleRooms);
rowView.NoTroubles = (TextView) ConvertView.findViewById(R.id.txtvTroubleRooms);
rowView.btnShowTasks = (ImageButton) ConvertView.findViewById(R.id.btnShowTasks);
rowView.btnShowRooms = (ImageButton) ConvertView.findViewById(R.id.btnShowRooms);
ConvertView.setTag(rowView);
}
else
{
rowView = (Floor_View) ConvertView.getTag();
}
Floor_Object Floor = Floor_List.get(position);
rowView.FloorName.setText(Floor.getFloorName());
if(Floor.getNoChecks().equals("0")) {
rowView.CheckImage.setVisibility(View.INVISIBLE);
rowView.NoChecks.setVisibility(View.INVISIBLE);
}
else{
rowView.CheckImage.setVisibility(View.VISIBLE);
rowView.NoChecks.setVisibility(View.VISIBLE);
rowView.NoChecks.setText(Floor.getNoChecks());
}
if(Floor.getNoBlocks().equals("0")) {
rowView.BlockImage.setVisibility(View.INVISIBLE);
rowView.NoBlocks.setVisibility(View.INVISIBLE);
}
else{
rowView.BlockImage.setVisibility(View.VISIBLE);
rowView.NoBlocks.setVisibility(View.VISIBLE);
rowView.NoBlocks.setText(Floor.getNoBlocks());
}
if(Floor.getNoDirts().equals("0")) {
rowView.DirtImage.setVisibility(View.INVISIBLE);
rowView.NoDirts.setVisibility(View.INVISIBLE);
}
else{
rowView.DirtImage.setVisibility(View.VISIBLE);
rowView.NoDirts.setVisibility(View.VISIBLE);
rowView.NoDirts.setText(Floor.getNoDirts());
}
if(Floor.getNoTroubles().equals("0")) {
rowView.TroubleImage.setVisibility(View.INVISIBLE);
rowView.NoTroubles.setVisibility(View.INVISIBLE);
}
else{
rowView.TroubleImage.setVisibility(View.VISIBLE);
rowView.NoTroubles.setVisibility(View.VISIBLE);
rowView.NoTroubles.setText(Floor.getNoTroubles());
}
if(Floor.getNoBlocks().equals("0") && Floor.getNoDirts().equals("0") && Floor.getNoTroubles().equals("0")) {
rowView.btnFloorRow.setClickable(false);
rowView.btnShowTasks.setClickable(false);
rowView.btnShowTasks.setImageResource(R.drawable.list_999999_50);
}
else{
rowView.btnFloorRow.setClickable(true);
rowView.btnFloorRow.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setClickable(true);
rowView.btnShowTasks.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setImageResource(R.drawable.list_3a4b66_50);
}
rowView.btnShowRooms.setOnClickListener(OnShowRoomsClickListener);
return ConvertView;
}
private View.OnClickListener OnShowTasksClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
private View.OnClickListener OnShowRoomsClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
}
You can see when I work with fragment it's mandatory to create control events in adapter... Just change event of view and that it's your need, I think... I am waiting that sample helps you. If you need more info or help advice me, Good luck!
PD: I think this is your code that you need:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity(), list);
lv.setAdapter(adapter);
return view;
}
}
public class FileAdapter extends ArrayAdapter<Files> {
private final Context context;
private ArrayList<Files> list;
private final ListView Lv;
public FileAdapter(Context context, ArrayList<Files> list,ListView lv) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
this.Lv = lv;
}
public static class RowView{
//Declaration of Atributes
EditText Nome;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
RowView rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new RowView();
convertView = inflator.inflate(R.layout.homeadapter, null);
rowView.nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
}
rowView.nome.setOnItemSelectedListener(ItemSelecteListener);
return convertView;
}
private View.setOnItemSelectedListener ItemSelecteListener = new View.OnClickListener() {
#Override
public void onItemSelected(View view) {
final int position = lvFloors.getPositionForView((View) v.getParent());
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
};
}
Im using a array adapter with a so show a list of linear layouts. The layouts have 3 text fields and a hidden button that is shown when the list item is clicked. What I am trying to do is attach a onclicklistener to the button that will delete that row from the arraylist/adapter and update the data set. I have the following code:
public class CheckAdapter extends ArrayAdapter<Check> {
Context context;
int layoutResourceId;
ArrayList<Check> data = null;
public CheckAdapter(Context context, int resource, ArrayList<Check> objects) {
super(context, resource, objects);
this.context = context;
this.layoutResourceId = resource;
this.data = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder(convertView.findViewById(R.id.checklistRoot));
holder.checkName = (TextView) convertView.findViewById(R.id.checklistname);
holder.checkNumber = (TextView) convertView.findViewById(R.id.checklistnumber);
holder.checkTotal = (TextView) convertView.findViewById(R.id.checklisttotal);
holder.deleteButton = (Button) convertView.findViewById(R.id.deletecheck);
holder.swipeButtons();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
holder.swipeButtons();
}
String name = data.get(position).getName();
Double total = data.get(position).getAmmount();
Long number = data.get(position).getCheckNumber();
holder.checkName.setText(name);
holder.checkNumber.setText(number.toString());
holder.checkTotal.setText(total.toString());
holder.deleteButton.setTag(position);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer position = (Integer) view.getTag();
Log.d("Check Adapter", "Button Clicked: " + position);
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
static class ViewHolder{
TextView checkName;
TextView checkNumber;
TextView checkTotal;
Button deleteButton;
View rootView;
ViewHolder(View rootView) {
this.rootView = rootView;
}
public void swipeButtons(){
rootView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
deleteButton.setVisibility(View.VISIBLE);
return false;
}
});
}
}
}
My logs are showing I have the correct position being sent into data.remove(position); However the ListView doesn't seem to be updating and the data seems to be remaining. Any suggestions?
Thanks so much!
You can also try using position value provided by getView method itself.
Also remove line holder.deleteButton.setTag(position); from getView method.
so your code will be like below :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// --- Your other code ---//
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
Hope this will help you.
I want to access a button from an activity in a lazyadapter's getview( that is to make clickable from lazyadapter) is there any method to do that? The button click brings the user to the next screen. Please if someone has any idea or code.
You just follow this code on get your Click function and call your next activity in the onclick listener....
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
return convertView;
}
}
I've got ListFragment and a custom adapter.
Each row in my list consists of two TextViews and one ImageButton.
I want to call a method when clicking the Image Button.
public class MyCustomAdapter extends SimpleAdapter {
private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;
public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
this.context=context;
this.data=data;
this.from=from;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
firstLine.setText((CharSequence) data.get(position).get(from[0]));
secondLine.setText((CharSequence) data.get(position).get(from[1]));
ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
final long tourId = (Long) data.get(position).get("key");
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something dependend on tourId
}
});
return rowView;
}
I want to have this onClickListener in my ListFragment
The onListItemClick method in my ListFragment is only called when I click on one of the both TextViews but not when I click on the ImageButton.
Is it possible to move this onClickListener from my adapter to the ListFragment? The tourId should also be passed from my adapter to a method in the ListFragment.
Why is the onListItemClick method in my ListFragment not called when I click the ImageButton?
A friend of mine helped me and showed me how to solve my problem.
I created an interface in my adapter and implemented it in my fragment.
public class MyCustomAdapter extends SimpleAdapter {
private OnImgClicked mImageClicked;
private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;
public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
this.context=context;
this.data=data;
this.from=from;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
firstLine.setText((CharSequence) data.get(position).get(from[0]));
secondLine.setText((CharSequence) data.get(position).get(from[1]));
ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
final long tourId = (Long) data.get(position).get("key");
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mImageClicked.imgClicked(tourId);
}
});
return rowView;
}
public interface OnImgClicked {
public void imgClicked(long tourId);
}
public void setOnImgClickListener(OnImgClicked imgClicked) {
this.mImageClicked = imgClicked;
}
}
Implement the interface in the fragment
public class MyFragment extends ListFragment implements MyCustomAdapter.OnImgClicked {
//...other methods like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[...]//init/create variables for MyCustomAdapter Creation
MyCutsomAdapter adapter = new MyCustomAdapter(getActivity(),....);
adapter.setOnImgClickListener(this);
}
#Override
public void imgClicked(long tourId) {
//do something with tourId
}
}
in getView do
imgBtn.setFocusable(false);
imgBtn.setClickable(false);
that should fix ur click problem