0

嗨,我在代码 Q1 和 Q2 中有以下代码和 2 个问题

Q1)我在这个网站上读到你不能从片段内创建一个新的活动,所以如果我在一个片段内并且想做

Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);

我该怎么做?


Q2) 而且似乎我没有进入我的 onListItemClick,我在这里做错了什么,是 OnListItemsClick 放错地方还是什么?

我有文件 ActionBarMain.java 创建一个新片段 Fragment1.java 波纹管

//ActionbarMain.java

        fram1 = new FragMent1();
        fragMentTra.addToBackStack(null);
        fragMentTra = getFragmentManager().beginTransaction().add(rl.getId(), fram1, "fram1");


//Fragment1.java
    import android.app.Fragment;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class FragMent1 extends Fragment {

    private String[] bookNames;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ListView listView = new ListView(getActivity());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1);

        bookNames= bookNames();



        for (String str : bookNames)
            array.add(str);
        listView.setAdapter(array);
        return listView;
    }

    private String[] bookNames(){
        SimpleBookManager.getSimpleBookManager().count();
        bookNames = new String[SimpleBookManager.getSimpleBookManager().count()]; 
        for(int i=0; i < SimpleBookManager.getSimpleBookManager().count(); i++){
            bookNames[i]= SimpleBookManager.getSimpleBookManager().getBook(i).getTitle();
        }
        return bookNames;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        //Q1)Nothing happens here nothing is printed out and I get no errors when I click on
        //an Item in the list, its like it don't come here, why?
        Log.d("test", SimpleBookManager.getSimpleBookManager().getBook(position).getTitle());
        Log.d("test", "jje");

        //Q2)Call the DetailActivity.java how to achive this since I'm inside a fragment?   
    }
}
4

2 回答 2

1

A1)

Intent intent = new Intent(getActvity(), DetailActivity.class);
getActivity().startActivity(intent);

这将打开详细活动。getActivity()返回放置您的片段的活动。

当我获得有关该问题的更多信息时,我将回答您的 Q2

于 2012-11-16T12:57:34.560 回答
0

你为什么不使用 ListFragment ?

像这样的东西:

public class ListFragment extends android.support.v4.app.ListFragment {
    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.app.ListFragment#onListItemClick(android.widget.ListView
     * , android.view.View, int, long)
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        Intent intent = new Intent(getActivity().getApplicationContext(),
                    DetailActivity.class);
            intent.putExtra("value", item);
            startActivity(intent);
        }

    }
} //END OF CLASS
//END OF FILE

上面的代码对我有用。

于 2012-11-16T13:09:31.273 回答