如何将ArrayList项添加到我的ListView

时间:2017-03-01 11:50:18

标签: java android listview bluetooth

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

一个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" />

1 个答案:

答案 0 :(得分:1)

ArrayAdapter使用TextView显示其中的每个项目。

请参阅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);

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

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

查看更多详情here