现在我有一个应用程序可以搜索并返回 ListFragment 中的项目。我希望能够使每个 ListFragment 都可点击,这样当它被点击时,一个新的 Activity 开始使用这样的东西:
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
//eventually data from the List will be passed to the new Activity...
}
启动我需要可点击的ListFragment的类如下:
public class ResultsView extends FragmentActivity {
private ArrayAdapter<String> mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_view);
//Receive searchTerm from MainActivity
Intent intent = getIntent();
String searchTerm = intent.getStringExtra(MainActivity.SEARCH_TERM);
mAdapter = new ArrayAdapter<String>(this, R.layout.item_label_list);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ListFragment list = new ListFragment();
ft.add(R.id.fragment_content, list);
// Let's set our list adapter to a simple ArrayAdapter.
list.setListAdapter(mAdapter);
FactualResponderFragment responder = (FactualResponderFragment) fm.findFragmentByTag("RESTResponder");
if (responder == null) {
responder = new FactualResponderFragment();
ft.add(responder, "RESTResponder");
}
Bundle bundle = new Bundle();
bundle.putString("search_term", searchTerm);
responder.setArguments(bundle);
ft.commit();
}
public ArrayAdapter<String> getArrayAdapter() {
return mAdapter;
}
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
}
所以我的问题是:
1) 如何设置 onListItemClick()
以使用我正在使用的 ListFragment 列表
?
2) ListView
没有任何与 onClick
等相关的 XML 属性。这不重要吗?
3) onListItemClick()
、onItemClickListener()
和任何其他适用的东西应该放在哪里?
感谢您的帮助。
编辑:
按照 Pramod 的建议,我创建了一个类:ListFragmentClickable extends ListFragment
,并用以下内容填充它:
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
}
Eclipse 告诉我 new Intent(this, PlaceView.class)
是不允许的,我只能说 Intent i = new Intent();
。错误是“构造函数 Intent(ListFragmentClickable, Class) 未定义。”我尝试按照 Eclipse 的建议实例化 Intent,然后添加 i.setClass(this, PlaceView.class)
和 i.setClassName(this, PlaceView.class)
但它没有' 工作,因为我遇到了同样的错误。
1) 如何绕过此错误并按计划覆盖该方法?
2) 如果我不能这样做,并且必须使用 Intent i = new Intent();
,我该如何告诉 Intent 我们的目标是什么类?
最佳答案
试试这个,
Intent intent = new Intent(getActivity().getApplicationContext(), YourClassName.class);
startActivity(Intent intent);
其中“YourClassName”是您希望调用的类的类型。
关于android - 使用 onListItemClick() 从 ListFragment 启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514831/