简体   繁体   English

android java/kotlin recyclerview 项目不可见

[英]android java/kotlin recyclerview items not visible

i am trying to build an app that scans for bluetooth devices and displays them in a recyclerview.我正在尝试构建一个扫描蓝牙设备并将它们显示在回收站视图中的应用程序。 i am writing some of the app using java and some of the app in kotlin.我正在使用 java 编写一些应用程序,而在 kotlin 中编写一些应用程序。 for some reason my recyclerview items aren't visible.出于某种原因,我的 recyclerview 项目不可见。 somebody please help me to find what mistake that i am making.有人请帮我找出我犯了什么错误。 i am posting the code for my activity and also my recycler view.我正在发布我的活动代码以及我的回收站视图。 To clarify my question.澄清我的问题。 The discovery succeeds, devices are found, and all the inherited methods in my adapter seem to be called.发现成功,找到设备,我的适配器中所有继承的方法好像都被调用了。 I placed log messages in them and I still see nothing in the recyclerview.我在其中放置了日志消息,但在 recyclerview 中仍然看不到任何内容。 Why is nothing show up?为什么什么都不显示? here is my activity written in java :这是我用 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;
            }
        }
    }
}
}

and here is the code for my recyclerview written in kotlin :这是我用 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"
}

}

thank you for helping me.谢谢你帮助我。

you can remove context in contractor adapter and can replace this line using context inside adapter val v = LayoutInflater.from(mContext).inflate(R.layout.device_item, parent, false) into val v = LayoutInflater.from(parent.context).inflate(R.layout.device_item, parent, false) or if you want context inside adapter as argument replace this statement您可以删除承包商适配器中的上下文,并可以使用适配器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());

to mAdapter = new DeviceAdapter(this); to mAdapter = new DeviceAdapter(this);

The problem here was the context that i was using.这里的问题是我使用的上下文。 my context was the one returned from getApplicationContext() which was incorrect.我的上下文是从getApplicationContext()返回的不正确的上下文。 now i have changed it to just using this .现在我已将其更改为仅使用this once making that change it all viewholder items were visible.进行更改后,所有视图项都可见。

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

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