所以我在Fragment中创建了ListView。这是代码:
public class HomeFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
//INFLATE THE FRAGMENT LAYOUT FILE
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);
//CREATE DATASOURCE
String[] datasource={"Potisak s klupe",
"Potisak s klupe bučicama",
"Potisak s kose klupe",
"Potisak s kose klupe bučicama",
"Potisak s kontrakose klupe",
"Potisak s kontrakose klupe bučicama",
"Potisak na spravi",
"Razvlačenja na ravnoj klupi",
"Razvlačenja na kosoj klupi",
"Razvlačenja na kontrakosoj klupi",
"Razvlačenja kablovima na klupi",
"Propadanja",
"Sklekovi",
"Sklekovi s nogama na povišenom",
"Crossover kablovima",
"Razvlačenja na leptir mašini",
};
//CREATE ADAPTER
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout_prsa, R.id.labelrowlayout_prsa_tekst, datasource);
// BIND ADAPTER TO THE LISTFRAGMENT
setListAdapter(adapter);
//RETAIN LISTFRAGMENT INSTANCE ACROSS CONFIGURATION CHANGES
setRetainInstance(true);
return rootView;
}
// HANDLING ITEM CLICK
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup)view;
TextView txt = (TextView)viewGroup.findViewById(R.id.labelrowlayout_prsa_tekst);
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent (getActivity(), PotisakSKlupe.class);
startActivity(intent);
}
});
}
}
但是当用意图点击第一个列表项时,我无法进入另一个活动。还尝试了在互联网上找到的其他几种方法,但没有运气。关于片段列表视图没什么用。
我在这里缺少什么?
答案 0 :(得分:0)
尝试这个我认为你没有在你的code.please调用中调用这个方法,然后尝试。
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup)view;
TextView txt = (TextView)viewGroup.findViewById(R.id.labelrowlayout_prsa_tekst);
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent (getActivity(), PotisakSKlupe.class);
startActivity(intent);
}
});
}
希望它有所帮助。
答案 1 :(得分:0)
首先覆盖此方法。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
在onCreateView之后,调用onActivityCreated方法。所以将onListItemClick()放在onActivityCreated方法中。现在将列表项设置为可单击。
listView.setClickable(true);
然后,
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getActivity(), AddActivity.class));
}
});
这可能会对你有所帮助。 (检查我使用的变量名和你的变量名。)