嗨,我在代码 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?
}
}