2017-10-08 91 views
-1

我正在嘗試構建一個應用程序,該應用程序可以掃描藍牙設備並在回收站中顯示它們。我正在編寫一些使用java和kotlin中的一些應用程序的應用程序。出於某種原因,我的recyclerview項目不可見。有人請幫我找到我正在犯的錯誤。我發佈了我的活動代碼和我的回收商視圖。澄清我的問題。發現成功,找到了設備,並且我的適配器中的所有繼承方法似乎都被調用。我將日誌消息放入其中,並且在回收站視圖中仍然看不到任何內容。爲什麼沒有出現?這裏是我的活動java寫的:android java/kotlin recyclerview項目不可見

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; 
      } 
     } 
    } 
} 
} 

,這裏是我的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

這裏的問題是我正在使用的上下文。我的背景是從getApplicationContext()返回的,這是不正確的。現在我已將其更改爲僅使用this。一旦進行更改,所有視圖持有者項目都可見。

相關問題