片段中的 ListView 单击时没有任何反应
ListView in fragment nothing happen when clicked
我的代码有什么问题?这是我的 listView
片段,用于显示数据库。查看数据库成功,但是点进去没有反应
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
db = new MyDatabase(getActivity());
//View view = inflater.inflate(R.layout.fragment_event, container, false);
View rootView = inflater.inflate(R.layout.fragment_event, container, false);
//ArrayList<ListviewContactItem> listContact = GetlistContact();
final ArrayList<event_constructor> arayList = new ArrayList<event_constructor>();
//ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
eventListView = (ListView) rootView.findViewById(android.R.id.list);
//lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));
Log.d("EventNul", "Start");
HashMap<String, String> map = new HashMap<String, String>();
List<event_constructor> allEvents = db.getAllEvent();
for (event_constructor event : allEvents) {
arayList.add(new event_constructor(event.getId(), event.getEventName()));
}
// Don't forget to close database connection
db.closeDB();
ListAdapter eventAdapter = new ArrayListAdapter(getActivity(), arayList);
eventListView.setAdapter(eventAdapter);
eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String gid = ((TextView) view.findViewById(R.id.noId)).getText()
.toString();
String gname = ((TextView) view.findViewById(R.id.event)).getText()
.toString();
Log.d("EventId", gid +". "+ gname);
//Log.d("Item", ItemListId.toString());
Intent intent = new Intent(getActivity(), NewPostActivity.class);
intent.putExtra("idEvent", gid);
startActivity(intent);
}
});
return rootView;
}
如果列表的任何行项目包含可聚焦或可点击的视图,则 OnItemClickListener 将不起作用。
行项目必须有一个类似 android:descendantFocusability="blocksDescendants" 的参数。
在这里你可以看到例子,你的列表项应该是什么样子。您的列表项 xml 应该是... row_item.xml (your_xml_file.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
感谢您关注 Waheed,但我找不到解决方案。现在我的解决方案是将 "setOnItemClickListener" 移动到 "onStart()"。它的工作
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int pos,
long id) {
String gname = ((TextView) v.findViewById(R.id.event)).getText()
.toString();
String gid = ((TextView) v.findViewById(R.id.noId)).getText()
.toString();
Log.d("EventId", gid +". "+ gname);
Integer i = Integer.parseInt(gid);
Intent intent = new Intent(getActivity(), NewPostActivity.class);
intent.putExtra("idEvent",i);
startActivity(intent);
}
});
}
我的代码有什么问题?这是我的 listView
片段,用于显示数据库。查看数据库成功,但是点进去没有反应
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
db = new MyDatabase(getActivity());
//View view = inflater.inflate(R.layout.fragment_event, container, false);
View rootView = inflater.inflate(R.layout.fragment_event, container, false);
//ArrayList<ListviewContactItem> listContact = GetlistContact();
final ArrayList<event_constructor> arayList = new ArrayList<event_constructor>();
//ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
eventListView = (ListView) rootView.findViewById(android.R.id.list);
//lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));
Log.d("EventNul", "Start");
HashMap<String, String> map = new HashMap<String, String>();
List<event_constructor> allEvents = db.getAllEvent();
for (event_constructor event : allEvents) {
arayList.add(new event_constructor(event.getId(), event.getEventName()));
}
// Don't forget to close database connection
db.closeDB();
ListAdapter eventAdapter = new ArrayListAdapter(getActivity(), arayList);
eventListView.setAdapter(eventAdapter);
eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String gid = ((TextView) view.findViewById(R.id.noId)).getText()
.toString();
String gname = ((TextView) view.findViewById(R.id.event)).getText()
.toString();
Log.d("EventId", gid +". "+ gname);
//Log.d("Item", ItemListId.toString());
Intent intent = new Intent(getActivity(), NewPostActivity.class);
intent.putExtra("idEvent", gid);
startActivity(intent);
}
});
return rootView;
}
如果列表的任何行项目包含可聚焦或可点击的视图,则 OnItemClickListener 将不起作用。
行项目必须有一个类似 android:descendantFocusability="blocksDescendants" 的参数。
在这里你可以看到例子,你的列表项应该是什么样子。您的列表项 xml 应该是... row_item.xml (your_xml_file.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
感谢您关注 Waheed,但我找不到解决方案。现在我的解决方案是将 "setOnItemClickListener" 移动到 "onStart()"。它的工作
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int pos,
long id) {
String gname = ((TextView) v.findViewById(R.id.event)).getText()
.toString();
String gid = ((TextView) v.findViewById(R.id.noId)).getText()
.toString();
Log.d("EventId", gid +". "+ gname);
Integer i = Integer.parseInt(gid);
Intent intent = new Intent(getActivity(), NewPostActivity.class);
intent.putExtra("idEvent",i);
startActivity(intent);
}
});
}