您好我在代码Q1和Q2中有以下代码和2个问题
Q1)我在这个网站上看过你不能从一个片段里面创建一个新的Activity,所以如果我在片段里面并想做
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
我该怎么做?
Q2) 此外,我似乎没有进入我的onListItemClick,我在这里做错了,OnListItemsClick错位或是什么?
我有ActionBarMain.java文件,它创建了一个新片段Fragment1.java bellow
// 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?
}
}
答案 0 :(得分:1)
A1)
Intent intent = new Intent(getActvity(), DetailActivity.class);
getActivity().startActivity(intent);
这将打开详细活动。 getActivity()
会返回放置片段的活动。
当我将获得有关该问题的更多信息时,我将回答您的第二季度
答案 1 :(得分: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
以上代码适合我。