AlertDialog ClickListener不起作用

时间:2016-07-01 12:54:27

标签: android alertdialog

我在More_Fragment中有AlertDialog。 我在AlertDialog中使用CustmAlertAdapter在listView中显示了项目 一切都很好......但clicklistener不工作? 这是我的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();
        }
    }
}

这是我的适配器:

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;
}
}

2 个答案:

答案 0 :(得分:1)

请放

android:focusable="false"
android:clickable="false"

用于alert_list_row视图中的所有视图,例如textview,button或imageviews等。它可以正常工作。

希望它会对你有所帮助。

答案 1 :(得分:0)

在onClick of AlertDialog中,替换

  @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:
                }

            }
        });

 @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:
                }

            }
        });