Reputation: 27
I have AlertDialog in More_Fragment. I have displayed items in listView using CustmAlertAdapter in AlertDialog everything is fine...but clicklistener is not working? This is my More_Fragment:
public class More_Fragment extends Fragment implements AdapterView.OnItemClickListener {
More_Fragment context;
ListView listView;
public static int[] images = {
R.drawable.project14,
R.drawable.event,
R.drawable.social,
R.drawable.gallery,
R.drawable.shop,
R.drawable.share};
public More_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_more_, container, false);
listView = (ListView) rootView.findViewById(R.id.MoreFragment_ListView);
String s[] = {
"Projects",
"Events",
"Social Links",
"Gallery",
"Shop",
"Share our App"};
listView.setAdapter(new CustomAdapter(getActivity(), s, images));
listView.setOnItemClickListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
switch (position) {
case 0:
Intent intent1 = new Intent(getActivity(), Project_Activity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(getActivity(), Events_Activity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(getActivity(), SocialLinks_Activity.class);
intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent3);
Toast.makeText(getActivity(), "You Clicked Social_Links ", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity(), "You Clicked Gallery ", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(getActivity(), "You Clicked Shop ", Toast.LENGTH_LONG).show();
break;
case 5:
final AlertDialog.Builder ad = new AlertDialog.Builder(getActivity());
ad.setTitle("Shar Our App");
ad.setIcon(R.drawable.share);
final String items[] = {
"Share by Email",
"Share on Facebook",
"Share on Twitter",
"Share bye SMS",
"Share by WhatsApp "
};
final int images[] = {
R.drawable.email,
R.drawable.facebook,
R.drawable.twitter,
R.drawable.sms,
R.drawable.whatsapp
};
CustomAlertAdapter adapter = new CustomAlertAdapter(getActivity(),images,items);
ad.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (position) {
case 0:
Toast.makeText(getActivity(), "You Clicked Email ", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getActivity(), "You Clicked Facebook ", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getActivity(), "You Clicked Twitter", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity(), "You Clicked SMS ", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(getActivity(), "You Clicked WhatsApp ", Toast.LENGTH_LONG).show();
break;
default:
}
}
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = ad.create();
alertDialog.show();
}
}
}
This is my Adapter:
public class CustomAlertAdapter extends BaseAdapter {
Context context;
String[] texts;
int[] imagesId;
private static LayoutInflater inflater = null;
public CustomAlertAdapter(FragmentActivity fragmentActivity, int[] images, String[] items){
this.context = fragmentActivity;
this.texts = items;
this.imagesId = images;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return texts.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class CustomAlertHolder{
TextView title;
ImageView imageView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
CustomAlertHolder customAlertHolder = new CustomAlertHolder();
if (convertView == null) {
view = inflater.inflate(R.layout.alert_list_row, null);
customAlertHolder.title = (TextView) view.findViewById(R.id.alert_title);
customAlertHolder.imageView = (ImageView) view.findViewById(R.id.alert_imageView);
//holder.imageView2.setImageResource(imageId_NextAroow[position]);
customAlertHolder.title.setText(texts[position]);
customAlertHolder.imageView.setImageResource(imagesId[position]);
}
return view;
}
}
Upvotes: 1
Views: 991
Reputation: 1667
Please put
android:focusable="false"
android:clickable="false"
for all your view in alert_list_row views like textviews, buttons or imageviews etc. and it will work fine.
Hope it will help you.
Upvotes: 2
Reputation: 1981
In onClick of AlertDialog,replace
@Override
public void onClick(DialogInterface dialog, int which) {
switch (position) {
case 0:
Toast.makeText(getActivity(), "You Clicked Email ", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getActivity(), "You Clicked Facebook ", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getActivity(), "You Clicked Twitter", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity(), "You Clicked SMS ", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(getActivity(), "You Clicked WhatsApp ", Toast.LENGTH_LONG).show();
break;
default:
}
}
});
with
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Toast.makeText(getActivity(), "You Clicked Email ", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getActivity(), "You Clicked Facebook ", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getActivity(), "You Clicked Twitter", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity(), "You Clicked SMS ", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(getActivity(), "You Clicked WhatsApp ", Toast.LENGTH_LONG).show();
break;
default:
}
}
});
Upvotes: 0