我正在尝试构建一个扫描蓝牙设备的应用程序,并在recyclerview中显示它们。我正在使用java和kotlin中的一些应用程序编写一些应用程序。由于某种原因,我的recyclerview项目不可见。有人请帮助我找出我正在犯的错误。我发布了我的活动代码以及我的回收者视图。澄清我的问题。发现成功,找到设备,并且似乎调用了我的适配器中的所有继承方法。我在其中放置了日志消息,我仍然在recyclerview中看不到任何内容。为什么没有出现?这是我用java编写的活动:
public class ToofActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = "ToofActivity";
private static final int REQUEST_COARSE_LOCATION = 222;
private static BluetoothAdapter btAdapter;
private TextView mStatusLabel;
private Switch mStatusSwitch;
private ProgressBar mProgressBar;
private RecyclerView mDeviceRecylcer;
private static ArrayList<BluetoothDevice> mDevices = new ArrayList<>();
private DeviceAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toof);
checkLocationPermission();
btAdapter = BluetoothAdapter.getDefaultAdapter();
mStatusLabel = (TextView) findViewById(R.id.status_view);
mStatusLabel.setText(btAdapter.isEnabled() ? "Active" : "Inactive");
mStatusSwitch = (Switch) findViewById(R.id.status_switch);
mStatusSwitch.setChecked(btAdapter.isEnabled());
mStatusSwitch.setOnCheckedChangeListener(this);
mProgressBar = (ProgressBar) findViewById(R.id.search_progress_bar);
mDeviceRecylcer = (RecyclerView) findViewById(R.id.device_recycler);
mDeviceRecylcer.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new DeviceAdapter(getApplicationContext());
mDeviceRecylcer.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchOption:
DiscoveryTask discovery = new DiscoveryTask(btAdapter, this, mProgressBar);
discovery.setOnCompleteListener(new DiscoveryTask.OnCompletedListener() {
@Override
public void onComplete() {
mAdapter.updateItems(mDevices);
}
});
discovery.execute((Void) null);
break;
}
return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btAdapter.enable();
mStatusLabel.setText("Active");
} else {
btAdapter.disable();
mStatusLabel.setText("Inactive");
}
}
public static void setDeviceList(ArrayList<BluetoothDevice> deviceList) {
mDevices.clear();
mDevices = deviceList;
}
protected void checkLocationPermission() {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_COARSE_LOCATION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_COARSE_LOCATION: {
if (grantResults.length <= 0
|| grantResults[0] != PackageManager.PERMISSION_GRANTED) {
checkLocationPermission();
break;
}
}
}
}
}
这是我用kotlin编写的recyclerview的代码:
class DeviceAdapter(val mContext : Context) : RecyclerView.Adapter<DeviceAdapter.DeviceHolder>(){
val mDevices = ArrayList<BluetoothDevice>()
fun updateItems(list: ArrayList<BluetoothDevice>){
mDevices.clear()
mDevices.addAll(list)
Log.d(TAG, "updating items : $mDevices")
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: DeviceHolder, position: Int) {
Log.d(TAG, "onBindViewHolder called!")
holder.bindItems(mDevices.get(position))
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DeviceHolder{
Log.d(TAG, "onCreateViewHolder called!")
val v = LayoutInflater.from(mContext).inflate(R.layout.device_item, parent, false)
return DeviceHolder(v)
}
override fun getItemCount(): Int {
return mDevices.size
}
inner class DeviceHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val nameView = itemView.findViewById(R.id.nameView) as TextView
val addrView = itemView.findViewById(R.id.addressView) as TextView
fun bindItems(btDevice: BluetoothDevice){
Log.d(TAG, "holder created!")
nameView.text = btDevice.name
addrView.text = btDevice.address
}
}
companion object {
val TAG = "DeviceAdapter"
}
}
谢谢你的帮助。
答案 0 :(得分:1)
您可以删除承包商适配器中的上下文,并且可以使用适配器 val v = LayoutInflater.from(mContext).inflate(R.layout.device_item, parent, false)
中的上下文替换为 val v = LayoutInflater.from(parent.context).inflate(R.layout.device_item, parent, false)
的这一行,或者如果您希望适配器中的上下文作为参数替换此语句,
mAdapter = new DeviceAdapter(getApplicationContext());
到 mAdapter = new DeviceAdapter(this);
答案 1 :(得分:0)
这里的问题是我正在使用的上下文。我的上下文是getApplicationContext()
返回的不正确的上下文。现在我已将其更改为仅使用this
。一旦进行了更改,所有查看者项目都可见。