ผมมี class A ซึ่งเป็น Activity มีlayout ที่ไว้แสดง fragment (android.support.v4.widget.DrawerLayout)
ซึ่งผมสามารถนำ Fragment class มาแสดงในlayout นี้ได้ตามปกติ
แต่เมื่อผมมีการเรียกใช้อีก Activity นึง หรือ class B แล้วถ้าผมต้องการ นำ Fragment class ไปแสดงบนlayout ของ class A อย่างที่แล้วมา
มันจะทำไม่ได้ครับ
อยากทาบว่าเพราะอะไร
และวิธีแก้ปัญหาครับ
นี่คือ นี่คือ layout ของ class A ครับ
Code (XML)
<!--
Copyright 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
นี่คือ code ของ class B ครับ
Code (Java)
package th.ac.kmutnb.reachtotemple;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class ListData extends Activity{
public static final String PK = "PK_macth_data";
public static final String NAMETEMPLE = "nameTemple";
public static final String SCORE = "score";
//public static final String PEOPLE = "people";
public static final String INTERVAL = "interval";
private ArrayList<Integer> pk;
private ArrayList<String> nameTemple;
private double[] score;
private double[] interval;
//private int[] people;
private ListView listView;
private int[] resource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
final Database mHelper = new Database(this);
score = null;
interval = null;
Bundle bundle = getIntent().getExtras();
pk = bundle.getIntegerArrayList(PK);//receive PK
nameTemple = bundle.getStringArrayList(NAMETEMPLE);//receive NameTemple
score = bundle.getDoubleArray(SCORE);//receive score
//people = bundle.getIntArray(PEOPLE);//receive people
interval = bundle.getDoubleArray(INTERVAL);//receive interval
//pull picture value from listview_value.xml
resource = getImageArray(R.array.image_temple,R.drawable.ic_launcher);
//work with ListDataAdapter.java that match pk
listView = (ListView)findViewById(R.id.listData);
listView.setAdapter(new ListDataAdapter(getApplicationContext()
, android.R.id.text1, nameTemple, resource, score, interval));
//===== Event =====
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2,long arg3) {
/*Intent i = new Intent(ListData.this,Detail.class);
i.putExtra("PK_User_Choose", pk.get(arg2));//send PK that user's choose to Class Detail
startActivity(i);*/
Fragment fragment = new Detail();
Bundle args = new Bundle();
args.putInt(ListData.PK, pk.get(arg2));
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
//===================================================================================
//===================================================================================
//===========PROBLEM========PROBLEM=====PROBLEM=========PROBLEM======PROBLEM===
//===================================================================================
//===================================================================================
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
//===================================================================================
//===================================================================================
//===================================================================================
//===================================================================================
//===================================================================================
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
String[] mes = {Database.COL_KEY_TEMPLE,Database.COL_NAME_TEMPLE};
Cursor mCursor = mHelper.select(mes, Database.TABLE_BOOKMARK, Database.COL_KEY_TEMPLE,
""+pk.get(arg2), false, true);
mCursor.moveToFirst();
if(mCursor.isAfterLast()){
AlertDialog.Builder addBookmark = new AlertDialog.Builder(ListData.this);
addBookmark.setTitle("ADD");
addBookmark.setMessage("Do you want to ADD this temple to bookmark ?");
addBookmark.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
new Bookmark(getApplicationContext()).memo(pk.get(arg2), nameTemple.get(arg2));
}});
addBookmark.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do something when the Cancel button is clicked
}});
addBookmark.show();
}else{
AlertDialog.Builder deleteBookmark = new AlertDialog.Builder(ListData.this);
deleteBookmark.setTitle("DELETE");
deleteBookmark.setMessage("Do you want to DELETE this temple from bookmark ?");
deleteBookmark.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
if(new Bookmark(getApplicationContext()).delete(pk.get(arg2))){
refreshBookmark(pk.get(arg2));
}
}});
deleteBookmark.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do something when the Cancel button is clicked
}});
deleteBookmark.show();
}
mHelper.closeConnection();
mCursor.close();
return true;
}
});
}
private void refreshBookmark(int position){
Database mHelper = new Database(this);
String[] mes = {Database.COL_KEY_TEMPLE,Database.COL_NAME_TEMPLE};
Cursor mCursor = mHelper.select(mes, Database.TABLE_BOOKMARK, null,
null, false, false);
nameTemple.clear();
pk.clear();
mCursor.moveToFirst();
if(mCursor.isAfterLast()){
Toast.makeText(getApplicationContext(), "Empty bookmark", Toast.LENGTH_SHORT).show();
}else{
while (!mCursor.isAfterLast()){
nameTemple.add(mCursor.getString(mCursor.getColumnIndex(Database.COL_NAME_TEMPLE)));//keep name temple
pk.add(mCursor.getInt(mCursor.getColumnIndex(Database.COL_KEY_TEMPLE)));//keep PK
mCursor.moveToNext();
}
}
mHelper.closeConnection();
mCursor.close();
listView.setAdapter(new ListDataAdapter(getApplicationContext()
, android.R.id.text1, nameTemple, resource,null,null));
}
//pull picture value from listview_value.xml
private int[] getImageArray(int resId, int defResId) {
TypedArray my_image_array = getResources().obtainTypedArray(resId);
int[] array_res = new int[pk.size()];
for(int i = 0 ; i < array_res.length ; i++) {
array_res[i] = my_image_array.getResourceId(pk.get(i), defResId);
}
my_image_array.recycle();
return array_res;
}
}