listview onitemclick在android片段内的对话框中不起作用
我正在使用片段,因为我使用了一个对话框并填充了一个自定义的listview,现在我想在listview中单击该项。当我在下面这样做的时候,它没有给出任何回应。 请找个人listview onitemclick在android片段内的对话框中不起作用,android,listview,fragment,onitemclicklistener,Android,Listview,Fragment,Onitemclicklistener,我正在使用片段,因为我使用了一个对话框并填充了一个自定义的listview,现在我想在listview中单击该项。当我在下面这样做的时候,它没有给出任何回应。 请找个人 iv_fav_list.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialo
iv_fav_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_fav_country_list);
lv_custom_list_fav_con = (ListView) dialog.findViewById(R.id.list_fav_country);
CFLA = new CustomFavCountryListAdapter(countryList, CountryActivityFragment.this.getActivity());
lv_custom_list_fav_con.setAdapter(CFLA);
dialog.show();
lv_custom_list_fav_con.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(CountryActivityFragment.this.getActivity(), "test", Toast.LENGTH_LONG).show();
}
});
}
});
iv_fav_list.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
dialog=新建对话框(getActivity());
setContentView(R.layout.dialog\u fav\u country\u list);
lv_custom_list_fav_con=(ListView)dialog.findViewById(R.id.list_fav_country);
CFLA=新的CustomFavCountryListAdapter(countryList,CountryActivityFragment.this.getActivity());
lv_定制_列表_fav_con.setAdapter(CFLA);
dialog.show();
lv_自定义_列表_fav_con.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
Toast.makeText(CountryActivityFragment.this.getActivity(),“test”,Toast.LENGTH\u LONG.show();
}
});
}
});
适配器类是
ArrayList<Country> mCountryList;
Context mContext;
int position;
Country cnt;
OnDialogListClickListener mlistener;
public interface OnDialogListClickListener {
void onItemClick(int position);
}
public CustomFavCountryListAdapter(ArrayList<Country> countryList, Context context, OnDialogListClickListener listener) {
this.mCountryList = countryList;
this.mContext = context;
this.mlistener = listener;
}
@Override
public int getCount() {
return mCountryList.size();
}
@Override
public Object getItem(int i) {
return mCountryList.get(i);
}
@Override
public long getItemId(int i) {
return mCountryList.get(i).getCountryID();
}
@Override
public View getView(int i, View view, final ViewGroup viewGroup) {
position = i;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.custom_fav_country_list, viewGroup,
false);
TextView tv_fav_con_name = (TextView) convertView.findViewById(R.id.tv_custom_fav_country_name);
tv_fav_con_name.setText(mCountryList.get(i).getCountryName());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onItemClick(position);
}
});
convertView.setTag(mCountryList.get(i).getCountryName());
return convertView;
}
}
ArrayList mCountryList;
语境;
内部位置;
国家cnt;
OnDialogListClickListenerMLListener;
公共界面OnDialogListClickListener{
无效单击(内部位置);
}
公共CustomFavCountryListAdapter(ArrayList countryList、上下文上下文、OnDialogListClickListener){
this.mCountryList=国家列表;
this.mContext=上下文;
this.mlistener=侦听器;
}
@凌驾
public int getCount(){
返回mCountryList.size();
}
@凌驾
公共对象getItem(int i){
返回mCountryList.get(i);
}
@凌驾
公共长getItemId(int i){
返回mCountryList.get(i.getCountryID();
}
@凌驾
公共视图getView(int i、视图视图、最终视图组视图组){
位置=i;
LayoutFlater充气器=(LayoutFlater)mContext
.getSystemService(上下文布局\充气机\服务);
视图转换视图=充气机。充气(R.layout.custom\u fav\u country\u列表,视图组,
假);
TextView tv\u fav\u con\u name=(TextView)convertView.findViewById(R.id.tv\u custom\u fav\u country\u name);
tv_fav_con_name.setText(mCountryList.get(i.getCountryName());
convertView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
姆利斯滕纳·奥尼麦克利克(职位);
}
});
setTag(mCountryList.get(i).getCountryName());
返回视图;
}
}
在适配器中创建接口
public interface OnDialogListClickListener {
void onItemClick(Country item);
}
通过适配器构造函数将此接口作为参数传递,并将其设置为在适配器中的getView
方法中充气的视图的setOnClickListener
@Override
public View getView(int i, View view, final ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.custom_fav_country_list, viewGroup,
false);
Country country = mCountryList.get(i);
TextView tv_fav_con_name = (TextView) convertView.findViewById(R.id.tv_custom_fav_country_name);
tv_fav_con_name.setText(country.getCountryName());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onItemClick(country);
}
});
convertView.setTag(country.getCountryName());
return convertView;
}
在片段中实现这个接口
private CustomFavCountryListAdapter.OnDialogListClickListener onDialogListClickListener = new CustomFavCountryListAdapter.OnDialogListClickListener () {
@Override
public void onItemClick(Country item) {
Toast.makeText(getActivity(), country.getCountryName(), Toast.LENGTH_SHORT).show();
}
};
像这样创建适配器
CFLA = new CustomFavCountryListAdapter(countryList, CountryActivityFragment.this.getActivity(), onDialogListClickListener);
你是否在click listener上调试了你的代码?不,这甚至没有响应。但这始终提供相同的列表项,我需要它是dynamicCan you post adapter类?但这始终提供相同的项,我需要从列表中获取动态项更改此mlistener.onItemClick(位置);"to"mlistener.onItemClick(i);;否则,当您单击时,它将传递到侦听器相同的位置,因为位置是全局变量,并返回las项的位置。但mlistener.onItemClick(i)在内部类中,对吗?,