如何将arraylist项目添加到我的listView



我想通过使用两个ListViews单击它们来连接到配对的Bluetooth设备。

一个ListView应显示所有配对设备,这些项目应单击并因单击其自己的单击而连接。然后,该项目应显示在显示连接设备的第二个ListView中。

我有几次尝试解决这个问题,但是我无法实现它的工作。希望您能提供帮助。

这是我的Fragment

编辑:

public class MainActivityFragment extends Fragment implements OnClickListener {

Button connectButton;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static final int ENABLE_BLUETOOTH = 1;
private static final String TAG = MainActivityFragment.class.getSimpleName();
ArrayList<String> mPairedDevicesArrayList;
ArrayAdapter<String> mPairedDevicesAdapter;
ListView listViewPaired, listViewConnected;
public void initBluetooth(){
    if(!mBluetoothAdapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, ENABLE_BLUETOOTH);
    }
}

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    mPairedDevicesArrayList = new ArrayList<>();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_connect, parent, false);
    connectButton = (Button) rootView.findViewById(R.id.connectButton);
    connectButton.setOnClickListener(this);
    listViewPaired = (ListView) rootView.findViewById(R.id.listViewPaired);
    listViewConnected = (ListView) rootView.findViewById(R.id.listViewConnected);

    return rootView;
}
@Override
public void onClick(View rootView){
    connectButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            if(mBluetoothAdapter.isEnabled()) {
                if(mBluetoothAdapter.isDiscovering()){
                    Log.i(TAG, "cancel discovery");
                    mBluetoothAdapter.cancelDiscovery();
                }
                Log.i(TAG, "Bluetooth start search");
                mBluetoothAdapter.startDiscovery();
                getPairedDevices();
            }
            else {
                Log.i(TAG, "Bluetooth not activated");
                initBluetooth();
            }
        }
    });  listViewPaired.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            listViewConnected.setAdapter(mPairedDevicesAdapter);
        }
    });
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(getActivity().getApplicationContext(), "Bluetooth enabled", Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getActivity().getApplicationContext(), "User canceled", Toast.LENGTH_LONG).show();
        }
    }
}
private void getPairedDevices() {
    Set<BluetoothDevice> pairedDevice = mBluetoothAdapter.getBondedDevices();
    if(pairedDevice.size()>0)
    {
        for(BluetoothDevice device : pairedDevice)
        {
            mPairedDevicesArrayList.add(device.getName()+"n"+device.getAddress());  mPairedDevicesAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.fragment_connect);  listViewPaired.setAdapter(mPairedDevicesAdapter);
            Log.i(TAG, "devices in List");
        }
    }
}

这是我的fragment-xml:

<TextView
    android:text="Welcome in Fragment!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp" />
<Button
    android:text="Connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="88dp"
    android:id="@+id/connectButton" />
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:background="@android:color/holo_blue_bright"
    android:layout_toLeftOf="@+id/connectButton"
    android:layout_toStartOf="@+id/connectButton"
    android:layout_below="@+id/connectButton"
    android:id="@+id/listViewPaired" />
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:background="@android:color/darker_gray"
    android:layout_toRightOf="@+id/connectButton"
    android:layout_toEndOf="@+id/connectButton"
    android:layout_below="@+id/connectButton"
    android:id="@+id/listViewConnected" />

ArrayAdapter使用文本视图显示其中的每个项目。

请参阅arrayadapter

它有许多可以使用的构造函数,您可以使用此构造函数在数据中初始化适配器

ArrayAdapter(Context context, int resource, T[] objects). 

,然后将其设置为listView

这样:

mPairedDevicesAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, mPairedDevicesArrayList);
listViewPaired.setAdapter(mPairedDevicesAdapter);

并为ListView行设置onClickListener,请执行此操作:

listViewPaired.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // do you code
    }
}

请参阅此处的更多详细信息

最新更新