2016-10-06 54 views
2

我有一個主要活動,當第一次啓動顯示一個片段,顯示配對藍牙設備的列表視圖。我不想使用ListFragment,因爲我想在頂部顯示TextView。 我創建了一個onItemClickListener,用於設置選定設備的MAC地址和名稱的字符串。這在LOG 中顯示正常,我想將這些字符串發送回Main活動,然後打開另一個使用這些字符串連接到Bluetooth設備的Fragment。我如何獲得一個片段發送onClickListener結果回主要活動

我已經使用所有活動得到了這項工作,但想要更改爲碎片。

我的主要活動是。

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


public class MainActivity extends AppCompatActivity { 

    // MAC-address of Bluetooth module (you must edit this line) 
    private static String address = "No Device Selected"; 
    private static String itemName = "No Device Selected"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

} 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     class="com.ming.pondcontroller.AddressFragment" 
     android:id="@+id/main_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

我的地址片段

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.Intent; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.Set; 

public class AddressFragment extends Fragment { 

    private static final String TAG = "get address Activity"; 
    private BluetoothAdapter BA = null; 
    private Set<BluetoothDevice> pairedDevices; 
    LayoutInflater inflater2; 
    ListView lv; 
    String address; 
    String itemName; 
    String itemString; 

    public AddressFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     inflater2 = inflater; 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_address, container, false); 
    } 

    @Override 
    public void onStart(){ 
     super.onStart(); 
     lv = (ListView)getView().findViewById(R.id.listView); 
     BA = BluetoothAdapter.getDefaultAdapter(); 
     checkBTState(); 

     // get paired devices 
     pairedDevices = BA.getBondedDevices(); 
     ArrayList list = new ArrayList(); 
     // get a list of paired devices 
     for(BluetoothDevice bt:pairedDevices){ 
      list.add(bt.getName()+"\n"+bt.getAddress()); 
     } 
     // display the list in the list view 
     final ArrayAdapter adapter = new ArrayAdapter(inflater2.getContext(),android.R.layout.simple_list_item_1,list); 
     lv.setAdapter(adapter); 

     // onItemClick Listener 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> adapterView,View view, int position, long l){ 
       // get the text of the item clicked from position 
       itemString = (String)(lv.getItemAtPosition(position)); 
       // get last 17 characters for MAC address 
       address = itemString.substring(itemString.length()-17); 
       itemName = itemString.substring(0,itemString.length()-17); 
       Log.d(TAG,address); 

      } 
     }); //end of onClickListener 
    } 

    private void checkBTState() { 
     // Check for Bluetooth support and then check to make sure it is turned on 
     // Emulator doesn't support Bluetooth and will return null 
     if(BA==null) { 
      Log.d("Fatal Error", "Bluetooth not support"); 
     } else { 
      if (BA.isEnabled()) { 
       Log.d(TAG, "...Bluetooth ON..."); 
      } else { 
       //Prompt user to turn on Bluetooth 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, 1); 
      } 
     } 
    } 
} 

而且其佈局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorMainBack" 
    tools:context="com.ming.pondcontroller.AddressFragment"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:id="@+id/textView" 
     android:background="@color/colorPrimary" 
     android:textColor="@color/colorWhite" 
     android:textSize="20dp" 
     android:text="Select Device"/> 
    <ListView 
     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/textView" 
     android:background="@color/colorTextBack"> 
    </ListView> /> 
</RelativeLayout> 

我已經看過回調,偵聽器和接口的例子的負載,但似乎無法找到正確的東西,而不使用listFragment。

回答

0

片段:

public static class AddressFragment extends Fragment { 

    OnAddressListener mListener; 

    // Container Activity must implement this interface 
    public interface OnAddressListener { 
     public void onAddressChanged(String address); 
    } 

    // Use : mListener.onAddressChanged(address) 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnAddressListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement OnAddressListener"); 
     } 
    } 
} 

活動:

public class MyActivity extends Activity implements OnAddressListener{ 


    @Override 
    public void onAddressChanged(String address){ 
      // Set your address 
    } 

} 
0

我會建議你在你的Activity中實現一個監聽器。然後在你的片段,你調用監聽器在onclick

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    try { 
     yourActivityListener = (IActivityListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + " must implement IActivityListener"); 
    } 
} 

在onItemClicked方法,你可以這樣調用它

yourActivityListener.onItemClicked(); 

這將發送到您的活動要知道得到了什麼單擊項。

1

請您的活動會實現一個接口,然後設置作爲onAttach()在片段的成員變量:

接口類:

interface BluetoothItemSelected { 
    void onBluetoothItemSelected(String itemName, String itemAddress); 
} 

MainActivity實現:

public class MainActivity extends AppCompatActivity implements BluetoothItemSelected { 

    // MAC-address of Bluetooth module (you must edit this line) 
    private static String address = "No Device Selected"; 
    private static String itemName = "No Device Selected"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public void onBluetoothItemSelected(String itemName, String itemAddress) { 
     // TODO do your work here 
    } 

    // your other Activity code... 

} 

片段實施:

public class AddressFragment extends Fragment { 

    private static final String TAG = "get address Activity"; 
    private BluetoothAdapter BA = null; 
    private Set<BluetoothDevice> pairedDevices; 
    LayoutInflater inflater2; 
    ListView lv; 
    String address; 
    String itemName; 
    String itemString; 

    BluetoothItemListener btItemListener; 

    @Override 
    public void onAttach(Context activity) { 
     if (activity instanceof BluetoothItemListener) { 
      btItemListener = (BluetoothItemListener) activity; 
     } 
    } 

    @Override 
    public void onDetach() { 
     // remove the reference to your Activity 
     btItemListener = null; 
    } 



    @Override 
    public void onStart(){ 
     super.onStart(); 
     lv = (ListView)getView().findViewById(R.id.listView); 
     BA = BluetoothAdapter.getDefaultAdapter(); 
     checkBTState(); 

     // get paired devices 
     pairedDevices = BA.getBondedDevices(); 
     ArrayList list = new ArrayList(); 
     // get a list of paired devices 
     for(BluetoothDevice bt:pairedDevices){ 
      list.add(bt.getName()+"\n"+bt.getAddress()); 
     } 
     // display the list in the list view 
     final ArrayAdapter adapter = new ArrayAdapter(inflater2.getContext(),android.R.layout.simple_list_item_1,list); 
     lv.setAdapter(adapter); 

     // onItemClick Listener 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> adapterView,View view, int position, long l){ 
       // get the text of the item clicked from position 
       itemString = (String)(lv.getItemAtPosition(position)); 
       // get last 17 characters for MAC address 
       address = itemString.substring(itemString.length()-17); 
       itemName = itemString.substring(0,itemString.length()-17); 
       Log.d(TAG,address); 
       // call your interface here 
       if (btItemListener != null) { 
        btItemListener.onBluetoothItemSelected(itemName, address); 
       } 
      } 
     }); //end of onClickListener 
    } 

    // your other Fragment code ... 
} 
相關問題