Reputation: 73
I have a grid view with imageview and textview. Gridview itemSelectedListener is not working.I have tried every solution provided here i.e. making focusable and clickable true or false. If I am applying click listener in adapter then its working fine. I am not understanding what the problem is.
My code is-
MainActivity.java
public class HomeScreen extends Activity {
TextView txtUserName;
public CustomSharedPreferences preferences;
// Array of strings storing options
String[] options = new String[] {
"Create Group",
"Add People",
"Personal",
"Sharing",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] images = new int[]{
R.drawable.user_group,
R.drawable.add_user,
R.drawable.personal,
R.drawable.sharing,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option);
txtUserName=(TextView)findViewById(R.id.textViewUserName);
preferences = CustomSharedPreferences.getInstance(this);
String username= preferences.getStringValue(CustomSharedPreferences.USERNAME,"");
txtUserName.setText(username);
GridView gridView = (GridView) findViewById(R.id.gridView1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this,options,images));
gridView.setTextFilterEnabled(true);
gridView.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
Log.e("inisde","click grid");
Log.e("pos",""+position);
switch(position)
{
case 0:
Intent createGrp=new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("on","nothingselected");
// TODO Auto-generated method stub
}
});
}
}
Adapter.java
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
String [] option;
int [] imageId;
//private static LayoutInflater inflater=null;
// Keep all Images in array
/* public Integer[] mThumbIds = {
R.drawable.user_group
R.drawable.pic_2,
R.drawable.pic_3
};
*/
// Constructor
public ImageAdapter(Context c,String[] options,int[] img)
{
mContext = c;
option=options;
imageId=img;
// inflater = ( LayoutInflater )mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return option.length;
}
/*@Override
public int getCount() {
return mThumbIds.length;
}*/
@Override
public Object getItem(int position) {
return option[position];
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
TextView tv;
ImageView img;
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_layout, parent,false);
}
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.img.setClickable(false);
holder.tv.setText(option[position]);
holder.img.setImageResource(imageId[position]);
/* convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(position==0)
{
Intent createGrp=new Intent(mContext,GroupList.class);
mContext.startActivity(createGrp);
}
else if(position==1)
{
Intent addPeople=new Intent(mContext,AddMembers.class);
mContext.startActivity(addPeople);
}
else if(position==3)
{
Intent sharing=new Intent(mContext,AddMembers.class);
mContext.startActivity(sharing);
}
// TODO Auto-generated method stub
Toast.makeText(mContext, "You Clicked "+option[position], Toast.LENGTH_LONG).show();
}
});*/
return convertView;
}
}
XML files-
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/welcome_logout"/>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_marginTop="@dimen/editTextMarginTop"
android:gravity="center"
android:stretchMode="columnWidth"
android:clickable="true">
</GridView>
</LinearLayout>
gridlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:gravity="center_horizontal"/>
</LinearLayout>
Upvotes: 1
Views: 352
Reputation: 11597
Looks your getView
has problem, try the modified codes:
@Override public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// First let's verify the convertView is not null
if (convertView == null)
{
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_layout, parent,false);
holder = new ViewHolder();
holder.tv=(TextView) convertView.findViewById(R.id.txt);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.img.setClickable(false);
convertView.setTag(holder);
}
else
holder = (ViewHolder)convertView.getTag();
holder.tv.setText(option[position]);
holder.img.setImageResource(imageId[position]);
return convertView;
}
onItemSelectedListener
is for view item selection but not click, you could not get callback onItemSelected(...)
when click. And only:
This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
Hope this help!
Upvotes: -1
Reputation: 326
If you want to do something when user click on each item in GridView, you should use setOnItemClickListener of GridView.
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent createGrp = new Intent(HomeScreen.this,CreateGroup.class);
startActivity(createGrp);
}
});
Upvotes: 1