我正在使用片段,并且我已经使用了一个对话框并填充了自定义列表视图,现在我想在列表视图中单击该项目。当我在下面这样做时没有给出任何回应。 请检查一下。
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();
}
});
}
});
适配器类是
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;
}
}
答案 0 :(得分:1)
在适配器中创建接口
public interface OnDialogListClickListener {
void onItemClick(Country item);
}
通过适配器构造函数将此接口作为参数传递,并设置为在适配器中以setOnClickListener
方法膨胀的视图的getView
。
@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);