我有这个基本代码,它将listView放在DialogFragment中,还有一个按钮。
我在dismiss()
的按钮上放置了一个监听器并且它有效。然后,我尝试在我的listView上放置onItemClickListener
,当我点击列表项时,这个不会触发任何内容。
这是完整的代码:
public class ExternalInfoSelectorFragment extends DialogFragment{
ListView choiceList;
Button leave;
private ArrayList<ExternalObjectPoulpe> poulpeGet;
public static ExternalInfoSelectorFragment newInstance(int title, ArrayList<ExternalObjectPoulpe> liste){
ExternalInfoSelectorFragment dialog = new ExternalInfoSelectorFragment();
Bundle args = new Bundle();
args.putInt("title", title);
args.putSerializable("liste", liste);
dialog.setArguments(args);
return dialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.list_fragment_poulpe_choice, null);
choiceList = (ListView)v.findViewById(R.id.ListePoulpe);
poulpeGet = (ArrayList<ExternalObjectPoulpe>) getArguments().getSerializable("liste");
final externalObjectPoulpeAdapter adapter = new externalObjectPoulpeAdapter(getActivity(), poulpeGet, null);
choiceList.setAdapter(adapter);
choiceList.setTextFilterEnabled(true);
choiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ExternalObjectPoulpe eop = (ExternalObjectPoulpe) adapter.getItem(position);
Toast.makeText(getActivity(), eop.getName() , Toast.LENGTH_LONG).show();
}
});
leave = (Button) v.findViewById(R.id.FermerChoixPoulpe);
leave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getResources().getString(getArguments().getInt("title"))).setView(v);
return builder.create();
}
为什么我的列表侦听器没有触发任何内容?即使我点击listView的项目,该行也不会变为灰色(当它应该是默认行为时)。
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListePoulpe">
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fermer"
android:id="@+id/FermerChoixPoulpe"/>
</LinearLayout>
我在一个名为CreationSav的活动中通过点击按钮来调用它:
ajoutNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ExternalInfoSelectorFragment frag = ExternalInfoSelectorFragment.newInstance(R.string.dialog, getAllNotesTest());
frag.show(getFragmentManager(), null);
}
});
检查以下适配器类代码:
public class externalObjectPoulpeAdapter extends ArrayAdapter {
public externalObjectPoulpeAdapter(Context context, List<ExternalObjectPoulpe> list, SavDataWorker base) {
super(context, 0, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_list_poulpe_choice, parent, false);
}
final ExternalObjectPoulpe eop = (ExternalObjectPoulpe) getItem(position);
ViewHolderExeternalObjectPoulpe viewHolderExeternalObjectPoulpe = (ViewHolderExeternalObjectPoulpe) convertView.getTag();
if (viewHolderExeternalObjectPoulpe == null) {
viewHolderExeternalObjectPoulpe = new ViewHolderExeternalObjectPoulpe();
viewHolderExeternalObjectPoulpe.titre = (TextView) convertView.findViewById(R.id.NomElementPoulpe);
convertView.setTag(viewHolderExeternalObjectPoulpe);
}
viewHolderExeternalObjectPoulpe.titre.setText(eop.getName());
return convertView;
}
}
我的ListView元素的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/NomElementPoulpe"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>