gpt4 book ai didi

android - AlertDialog ONClickListener 不工作

转载 作者:行者123 更新时间:2023-11-30 01:13:58 25 4
gpt4 key购买 nike

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

最佳答案

请放

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

对于您在 alert_list_row View 中的所有 View ,如 TextView 、按钮或 ImageView 等,它会正常工作。

希望对你有所帮助。

关于android - AlertDialog ONClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38145989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com