![](/img/trans.png)
[英]Passing data from one fragment to another fragment from FirebaseRecyclerAdapter
[英]Passing data from one fragment to next fragment in android
在我的一個項目中,我正在使用2個片段。一個片段正在顯示項目列表,所以我想在第一個片段中選擇主題時,應該在另一個片段中更改詳細信息。我可以從一個片段傳遞數據到另一個視圖,但視圖不變。在哪種方法中,我應該實現第二個片段的視圖,以便可以相應地更改。 請給我很好的教程或示例。
在這里我必須開發一個android示例,我必須運行該應用程序,這意味着在第一個片段的水平listview上很好地顯示了類別名稱。我必須單擊任何類別意味着我該如何在第二個片段上傳遞類別名稱。請給我解決方案對於這個問題。
我用下面的代碼:
public class Home extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
第一個片段代碼:
public class MainActivity extends Fragment {
static final String URL = "http://dev2.mercuryminds.com/webservices/new_feed_articls.xml";
static String KEY_CATEGORY = "Categories";
static final String KEY_TITLE = "Category";
LazyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,
container, false);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TITLE);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String> ();
Element e = (Element) nl.item(i);
map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));
// map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
songsList.add(map);
}
HorizontalListView list = (HorizontalListView) view.findViewById(R.id.listView1);
adapter = new LazyAdapter(getActivity(), songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent();
in.setClass(getActivity(), SubCate.class);
// in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
startActivity(in);
}
});
return view;
}}
我如何在第二個活動中顯示類別名稱...請給我這些代碼...
編輯:
嗨,我在第一次活動中更改了代碼:
HashMap<String, String> map = songsList.get(position);
SubCate frag = (SubCate) getFragmentManager().findFragmentById(R.id.frag_2);
if (frag != null && frag.isInLayout()) {
frag.setText(getCapt(map));
}
}
private String getCapt(HashMap<String, String> map) {
if (map.containsValue("notchbolly")) {
return "dadad";
}
return "veeman";
在下一個活動中:
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.cate);
view.setText(item);
}
當直接在if(map.containsValue(“ notchbolly”))上提及名稱時,它的功能很好。但是我必須直接顯示名稱而不提及名稱。如果我必須單擊任何類別,則意味着該類別名稱顯示在下一個片段上。我能怎么做 ?????
使用接口方法,我們可以在片段之間傳輸數據
在fragmentA中創建接口並將數據發送到此接口,並在fragmentB中實現此接口方法並獲取所需的數據
以下代碼對您有用
FragmentA中的代碼:
public class FragmentA extends Fragment {
// interface method
public InterfaceDataCommunicatorBetweenfragmentAToFragmentB iInterfaceDataCommunicatorBetweenfragmentAToFragmentB;
// Data to send
String mDataString;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// use this line where u send data
iInterfaceDataCommunicatorBetweenfragmentAToFragmentB.upDatedData(mDataString);
View view = inflater.inflate(R.layout.sent_box, container, false);
return view ;
}
/**
* Interface method to send data to fragmentB
*
*/
public interface InterfaceDataCommunicatorBetweenfragmentAToFragmentB {
public void upDatedData(String data);
}
}
FragmentB中的代碼:
class FragmentB extends Fragment implements InterfaceDataCommunicatorBetweenfragmentAToFragmentB {
// interface method
public InterfaceDataCommunicatorBetweenfragmentAToFragmentB iInterfaceDataCommunicatorBetweenfragmentAToFragmentB;
// fragmentA data
String mgetUpdatedData
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.get_box, container, false);
return view ;
}
/**
* Interface method coming from fragment a to fragmentB after implement interface in fragmentB
*/
public void upDatedData(String data) {
this.mgetUpdatedData = data;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
iInterfaceDataCommunicatorBetweenfragmentAToFragmentB = (InterfaceDataCommunicatorBetweenfragmentAToFragmentB ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement interface");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.