简体   繁体   English

如何将ArrayList项目添加到我的ListView

[英]How to add ArrayList items to my ListView

I want to connect to paired Bluetooth devices by clicking on them using two ListViews . 我想通过使用两个ListViews单击配对的Bluetooth设备来连接它们。

One ListView should display all paired devices, those items should be clickable and get connected due to a click on the item itsself. 一个ListView应该显示所有配对的设备,这些项目应该是可单击的,并且由于单击项目本身而可以连接。 Then this item should be displayed in the second ListView that shows connected devices. 然后,该项目应显示在第二个ListView ,该列表显示已连接的设备。

I had several attempts to this problem but i couldnt manage to get it working. 我曾多次尝试解决此问题,但我无法使其正常运行。 Hope you can help. 希望能对您有所帮助。

Here is my Fragment : 这是我的Fragment

EDIT: 编辑:

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");

        }
    }

}

Here is my Fragment-XML: 这是我的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 uses a TextView to display each item within it. ArrayAdapter使用TextView来显示其中的每个项目。

See ArrayAdapter 请参阅ArrayAdapter

It has a number of constructors that can be used,You can use this constructor to initialize the adapter with you data 它有许多可以使用的构造函数,您可以使用此构造函数用数据初始化适配器

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

And then set it to your listView 然后将其设置为您的listView

like this: 像这样:

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

and to set an onClickListener for ListView rows, do that: 并为ListView行设置onClickListener ,请执行以下操作:

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

See more details here 在这里查看更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM