OnItemClickEvent not fired in GridView in android - java

i m developing an application in which gridview contain list of button...
when i place images instead of button in gridview then onItemClickEvent get fired..but if i place button in gridView then click event not getting callled...i dont know what is the problem...even i m not getting exception..
here is my code...
public class MainMenu extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.mainMenu);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(MainMenu.this, "hello" + position, Toast.LENGTH_SHORT).show();
}
});
}
//inner class for adapter
class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;
if (convertView == null) { // if it's not recycled, initialize some attributes
btn=new Button(mContext);
// imageView = new ImageView(mContext);
btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)
// imageView.setPadding(8,8, 8, 8);
} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}
// references to our images
private Integer[] mThumbIds =
{
R.drawable.pantrylocator_icon,
R.drawable.volunteeropportunity_icon,
R.drawable.volunteerlocator_icon,
R.drawable.volunteermanagement_icon,
R.drawable.donationform_icon,
R.drawable.donationviamsg_icon,
R.drawable.donationvideo_icon,
R.drawable.virtualfooddrive_icon,
R.drawable.newevent_icon,
R.drawable.pressrelease_icon,
R.drawable.volunteerphotos_icon,
R.drawable.aboutus_icon,
};
}
}

The button has its own OnClickListener:
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;
if (convertView == null) { // if it's not recycled, initialize some attributes
btn=new Button(mContext);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
// imageView = new ImageView(mContext);
btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)
// imageView.setPadding(8,8, 8, 8);
} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}

In your ImageAdapter-> getView method add the following line before returning newly created "convertView"
convertView.setClickable(false);
convertView.setFocusable(false);
If any of the views in gridview are clickable then they will block the grid's ItemClick listener from responding.

There is no onclick event written for the Buttons you are adding. Write code for the buttons to handle the click event! let us know then.

i have fece this problem also but finally got the solution i have follow the above suggession
and define button click event in base adapter class like as
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = LayoutInflater.from(mContext);
v = li.inflate(R.layout.icon, null);
tv = (Button)v.findViewById(R.id.icon_text);
iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(mThumbIds[position]);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(mContext, "vim", Toast.LENGTH_LONG).show();
}
});
}
else
{
v = (View)convertView;
}
return v;
}

gridview = (GridView) findViewById(R.id.gameGrid);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Ur Code here
}
Add click events for the button added in gridView

Here's the cleanest way to do it: call performItemClick() on the GridView from within each button's click listener. That way you can still use the GridView's onItemClickListener like normal.
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
...
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView) parent).performItemClick(v, position, 0);
}
});
}
http://www.migapro.com/click-events-listview-gridview/

I have solved my problem as i define button click event in base adpter class and my problem is solved......

Related

Get the clicked position of carousel with Recycler View and Card View

I'm using this CarouselView library.
I have used Recycler View and Card View to create the following UI.
In each card view, I have added this carousel view and contains images inside carousel.
This library has the setImageClickListener that gives the index of each clicked image in each card view.
customCarouselView.setImageClickListener(new ImageClickListener() {
#Override
public void onClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
}
});
I want to know which image is clicked and from which card that image belongs since there are multiple card-views.
I tried to register the click event in the ImageView and get the adapterPosition of clicked cardview using getAdapterPosition().
fruitImageView.setOnClickListener(new View.OnClickListener() { /*Hits only when another click event (setImageClickListner) is not registered*/
#Override
public void onClick(View view) {
int a = getAdapterPosition(); //gives the index of clicked cardview position.
}
});
When both of the events are registered then only click on ImageView (fruitImageView.setOnClickListener) gets hit, which gives the position of the adapter. I also want to know which image is clicked.
When I remove this ImageView Click listener, then carousel view click customCarouselView.setImageClickListener gets hit which gives the postion of image clicked but not the adapter position.
Are there any work around for this? Please help.
My Adapter Class complete code:
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
private ArrayList<HomeSalesModel> salesLists;
int[] sampleImages = {R.drawable.album1, R.drawable.album2, R.drawable.album3, R.drawable.album4, R.drawable.album5};
String[] sampleTitles = {"A", "B", "C", "D", "E"};
Context myContext = null;
public GridAdapter(ArrayList<HomeSalesModel> salesLists) {
this.salesLists = salesLists;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
myContext = parent.getContext();
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(GridAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_country.setText(salesLists.get(i).getTitle());
}
#Override
public int getItemCount() {
return salesLists.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CarouselView customCarouselView;
private TextView tv_country;
private CardView cardView;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.grid_card_row, parent, false));
tv_country = (TextView) itemView.findViewById(R.id.tv_country);
customCarouselView = (CarouselView) itemView.findViewById(R.id.customCarouselView);
cardView = (CardView) itemView.findViewById(R.id.card);
customCarouselView.setPageCount(sampleImages.length);
//customCarouselView.setSlideInterval(4000);
customCarouselView.setViewListener(viewListener);
customCarouselView.setImageClickListener(imageClickListner);
}
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
LayoutInflater li = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = li.inflate(R.layout.view_custom, null);
TextView labelTextView = (TextView) customView.findViewById(R.id.labelTextView);
ImageView fruitImageView = (ImageView) customView.findViewById(R.id.fruitImageView);
fruitImageView.setImageResource(sampleImages[position]);
labelTextView.setText(sampleTitles[position]);
fruitImageView.setOnClickListener(new View.OnClickListener() { /*Works when another click event (imageClickListner) is not registered*/
#Override
public void onClick(View view) {
int a = getAdapterPosition(); //gives the index cardview position.
}
});
//labelTextView.setText(salesLists.get(position).getProducts().get(position).getSlug());
return customView;
}
};
ImageClickListener imageClickListner = new ImageClickListener() {
#Override
public void onClick(int position) {
Toast.makeText(myContext, "Clicked item: " + position, Toast.LENGTH_SHORT).show(); //gives index the clicked image.
}
};
}
}
Simply you can get the adapter position inside ImageClickListener.
int postion = getAdapterPosition();

getView() Method and Checkbox

I'm new to android studio and I'm working on a project. I have a ListView and it consists of CheckBox and TextView. I've added two buttons add and delete. If I check the CheckBox and press the delete button, that item is deleted. Thus, I designed a custom adapter and it has a getView() method.
This getView() method includes a listener for CheckBox. When I check the CheckBox, is the getView() method called or only listener is executed?
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
final int pos = position;
rowView = myInflater.inflate(R.layout.row_layout, null);
TextView textView = (TextView)rowView.findViewById(R.id.content);
final CheckBox checkBox = (CheckBox)rowView.findViewById(R.id.check);
textView.setText( list.get(pos).getContent() );
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( checkBox.isChecked() ){
list.get(pos).setChecking(1);
}
else {
list.get(pos).setChecking(0);
}
}
});
return rowView;
}
When I check the checkbox, is the getView() method called or only
listener is executed ?
Only the code within your listener will be executed. In this case:
if( checkBox.isChecked() )
{
list.get(pos).setChecking(1);
}
else
{
list.get(pos).setChecking(0);
}
Adapter's getView() is called everytime the view for the list item is either created or reused. This method is executed before the user gets to see the item (for this reason it is recommended to keep this method short and free from heavy computations).
When the checkbox is checked, getView() will not be called rather the listener of the checkbox will be called. In this particular case what you are trying to achieve you will have to set a OnCheckedChangeListener and OnClickListener to checkbox and button respectively.
Your CustomAdapter.java will look something like this:
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> list;
public static List<Integer> selectedPositions;
public CustomAdapter(Context context,List<String> list){
this.context=context;
this.list=list;
this.selectedPositions=new ArrayList<>(0);
}
#Override
public int getCount() {
return list==null?0:list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate rowitem.xml file for each row ( Defined below ) *******/
vi = LayoutInflater.from(context).inflate(R.layout.row_item, null);
/****** View Holder Object to contain rowitem.xml file elements ******/
holder = new ViewHolder(vi);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
//Set listeners for checkbox and button
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked()){
selectedPositions.add(position);
}else{
int count=0;
for(int item:selectedPositions){
if(item==position){
selectedPositions.remove(count);
}
count++;
}
}
}
});
return vi;
}
private class ViewHolder{
CheckBox checkBox;
TextView textView;
ViewHolder(View itemView){
checkBox=(CheckBox)itemView.findViewById(R.id.checkbox);
textView=(TextView)itemView.findViewById(R.id.textView);
}
}
}
The Delete Button in the activity will be handeled like this :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(CustomAdapter.selectedPositions != null &&
!CustomAdapter.selectedPositions.isEmpty()){
for(int count = 0;
count < CustomAdapter.selectedPositions.size(); count++){
list.remove(CustomAdapter.selectedPositions.get(count));
adapter.notifyDataSetChanged();
}
}
}
});

Access certain element of listview adapter

I have an Activity with a ListView. This ListView is set with the Class GetAllEntrysListViewAdapter:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ListCell cell;
if (convertView == null) {
convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);
cell = new ListCell();
cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
cell.likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
convertView.setTag(cell);
}
else {
cell = (ListCell)convertView.getTag();
}
public static class ListCell {
private TextView note;
private ImageView img;
public ImageButton likeButton;
}
In the Activity I now want to change the Image of likeButton on the certain list entry that is clicked, but how can I reach this certain element? Her is what I have until now:
GetAllEntrysListViewAdapter.ListCell listCell;
listCell = new GetAllEntrysListViewAdapter.ListCell();
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
Try this code. Replace your
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
}
with
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//first get the elements by doing like
TextView note = (TextView)view.findViewById(R.id.listViewNote);
ImageView img = (ImageView) view.findViewById(R.id.listViewImg);
ImageButton likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
likeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
likeButton.setBackgroundResource(R.id.icon);
}
});
}
This will be helpful ...thanks
That part should be done in getView() method itself. It should work like this.
Set data in your Adapter
Display data in getView for each item
Once you Like any item, update data with new Like status
Now call notifyDatasetChanged() on your adapter, getView() will update item view accordingly.
You should read ListAdapter first, and see some examples how it works.

How to get value of textviews on button click from a listview?

i have a listview with a custom adapter class . Each of the list items contain 2 text view and one Button . i want to get the data of those text views while i click on the Button inside the list .
below is the demo model of listview
please help me out guys
#Override
public View getView(int position, View convertView, ViewGroup parent) {
IconView icon;
TextView title;
TextView description;
Button favour;
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(textViewResourceId, parent, false);
}
icon = (IconView) convertView.findViewById(R.id.station_icon);
title = (TextView) convertView.findViewById(R.id.station_title);
description = (TextView) convertView.findViewById(R.id.station_description);
favour=(Button)convertView.findViewById(R.id.favbutton);
Station station = getItem(position);
convertView.setTag(station);
title.setText(station.title);
icon.setIcon(station.thumb);
description.setText(station.description);
return convertView;
}
}
You can use your list widgets in getView method in your custom adapter as follows-
public View getView(int pos, View convertView, ViewGroup parent) {
View gridView = convertView;
if (gridView == null) {
gridView = mInflater.inflate(layoutId, null);
}
TextView tv1 = (TextView) gridView.findViewById(R.id.tv1);
TextView tv2 = (TextView) gridView.findViewById(R.id.tv2);
Button button = (Button)gridView.findViewById(R.id.btn);
}
Now on button click, you can use text views.
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Value That u Pass In Adapter
Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
}
});
inside the getview methed also see Toast
title.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
icon.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
favour.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
just set an onclick listener in your getView method;
icon = (IconView) convertView.findViewById(R.id.station_icon);
title = (TextView) convertView.findViewById(R.id.station_title);
description = (TextView) convertView.findViewById(R.id.station_description);
favour=(Button)convertView.findViewById(R.id.favbutton);
then inside your onclick listener get the TextView's text.
favour.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//title.getText()
// description.getText();
}
//Here I have one textview and edit text and submit button in list view
#Override
public View getView (int position, View convertView, final ViewGroup parent)
{
final View v = super.getView(position, convertView, parent);
final TextView txt= (TextView) v.findViewById(R.id.lblMenuname);
final EditText QtyView = (EditText) v.findViewById(R.id.editText);
Button b=(Button)v.findViewById(R.id.btnInterview);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String txt2 = txt.getText().toString();
Toast.makeText(ItemList.this,txt2.toString() + "=" + QtyView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
return v;
}

OnLongClick stopping OnClick

I am using a Listview. before implementing OnLongClick, my onListItemClick was working perfectly, however now, after implementing OnLongClick the long clicks work and normal list clicks don't do anything. It seems to hide exposure to the onListItemClick() function you already have working
can anyone see why/ suggest a solution?
public class CombChange extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListEdit(this, symbols));
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedValue = (String) getListAdapter().getItem(position);
if (lastPressed.equals(selectedValue) ){
count++;}
}
public class ListEdit extends ArrayAdapter<String> implements OnLongClickListener{
private final Context context;
private final String[] values;
public ListEdit(Context context, String[] values) {
super(context, R.layout.activity_comb_change, values);
this.context = context;
this.values = values;
}
#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.activity_comb_change, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
rowView.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View arg0) {
context.startActivity(new Intent(context,RestoreOriginal.class));
return false;
}
});
// Change icon based on name
String s = values[position];
if (s.equals("a")) {
imageView.setImageResource(R.drawable.a);
return rowView;
}
}
I think you shouldn't do rowView.setOnLongClickListener.
Try something likes this:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
// whatever you wanna do
return true;
}
});
I took the code from how to capture long press event for Listeview item of a ListActivity?
Hope this helps.

Categories