Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android - การทำงานระหว่าง Fragment กับ Activityที่2 มีปัญหาครับผม



 

Android - การทำงานระหว่าง Fragment กับ Activityที่2 มีปัญหาครับผม

 



Topic : 106364



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์




ผมมี class A ซึ่งเป็น Activity มีlayout ที่ไว้แสดง fragment (android.support.v4.widget.DrawerLayout)

ซึ่งผมสามารถนำ Fragment class มาแสดงในlayout นี้ได้ตามปกติ

1


แต่เมื่อผมมีการเรียกใช้อีก Activity นึง หรือ class B แล้วถ้าผมต้องการ นำ Fragment class ไปแสดงบนlayout ของ class A อย่างที่แล้วมา

มันจะทำไม่ได้ครับ

2

อยากทาบว่าเพราะอะไร

และวิธีแก้ปัญหาครับ

นี่คือ นี่คือ 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;
    }
	

}




Tag : Mobile, Android, JAVA, Mobile









ประวัติการแก้ไข
2014-02-28 11:16:42
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-02-28 11:15:46 By : pokneun View : 2588 Reply : 1
 

 

No. 1



โพสกระทู้ ( 1,458 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Blogger

https://stablekernel.com/article/using-fragments-to-simplify-android-navigation-drawers/






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-05-27 18:17:15 By : PhrayaDev
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - การทำงานระหว่าง Fragment กับ Activityที่2 มีปัญหาครับผม
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่