3

现在我有一个应用程序可以搜索并返回 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();,我如何告诉意图我们甚至针对什么类?

4

4 回答 4

7

试试这个,

Intent intent = new Intent(getActivity().getApplicationContext(), YourClassName.class);
startActivity(Intent intent);

其中“YourClassName”是您希望调用的类的类型。

于 2013-04-15T20:22:15.687 回答
2

从 listfragment 扩展一个类,然后覆盖 listitemclick 函数。

于 2013-03-20T03:41:01.400 回答
0
Intent i = new Intent((context),class) 

其中 class 是可识别的类型,可以按意图工作。具体来说,这是 Activity、Service 等。此外,上下文应该来自您的应用程序,而不是来自封装的列表视图,您通过使用this. 使用 getApplicationContext() 或从您的应用程序类中获取它,您需要有效的上下文。

如果 PlaceView 不是可识别的类型,那么您将无法做您想做的事情。

于 2013-03-21T01:13:11.357 回答
0

您可以使用此“getActivity().getApplicationContext()”轻松获取上下文

于 2013-03-24T10:24:48.130 回答