android - 使用 onListItemClick() 从 ListFragment 启动新 Activity

标签 android onclick

现在我有一个应用程序可以搜索并返回 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/

相关文章:

android - strings.xml 中的 "is translated here but not found in default locale"错误,可翻译 = "false"

android - 在 Android 中绘制锦标赛支架的最佳方式

javascript - 如何调用jquery

jQuery anchor 滚动...两个 onclick 事件

javascript - 将 onclick 事件添加到 html 选择选项

android - 从 strings.xml 文件中的字符串数组加载字符串

安卓VideoView : How do I validate an illegal character in my url?

android - 如何在android中连续删除键盘退格长按上的字符

java - 如何不同时处理 View.onClick() 和处理 View.onLongClick()?

javascript - 使用 javascript 显示表单元素