簡體   English   中英

android java/kotlin recyclerview 項目不可見

[英]android java/kotlin recyclerview items not visible

我正在嘗試構建一個掃描藍牙設備並將它們顯示在回收站視圖中的應用程序。 我正在使用 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"
}

}

謝謝你幫助我。

您可以刪除承包商適配器中的上下文,並可以使用適配器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);

這里的問題是我使用的上下文。 我的上下文是從getApplicationContext()返回的不正確的上下文。 現在我已將其更改為僅使用this 進行更改后,所有視圖項都可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM