首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlertDialog ClickListener不工作

AlertDialog ClickListener不工作
EN

Stack Overflow用户
提问于 2016-07-01 20:54:27
回答 0查看 883关注 0票数 1

我在More_Fragment有AlertDialog。我在listView中使用CustmAlertAdapter在AlertDialog中显示了项目一切都是fine...but点击侦听器不工作?这是我的More_Fragment:

代码语言:javascript
复制
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();
        }
    }
}

这是我的适配器:

代码语言:javascript
复制
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;
}
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38145989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档