我有一個主要活動,當第一次啓動顯示一個片段,顯示配對藍牙設備的列表視圖。我不想使用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。