现在我有一个应用程序可以搜索并返回 ListFragment 中的项目。我希望能够使每个 ListFragment 可点击,这样当它被点击时,一个新的活动开始使用这样的东西:
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 list
我正在使用的一起工作?
2)ListView
没有任何与onClick
等相关的 XML 属性。这没关系吗?
3) 和其他适用的东西应该在onListItemClick()
哪里onItemClickListener()
?
谢谢你的帮助。
编辑:
按照 Pramod 的建议,我创建了一个 class: 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();
,我如何告诉意图我们甚至针对什么类?