gpt4 book ai didi

android - RecyclerView 未填充(数组)

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:39 28 4
gpt4 key购买 nike

我搜索了很多但找不到为什么我的 RecyclerView 没有显示所有数据。在“MySensorAdapter”中将数据打印到控制台时,值已完成。

代码如下,非常感谢。

公共(public)类 MySensorAdapter 扩展 RecyclerView.Adapter {

private String[] mKeys;
private String[] mValues;

public MySensorAdapter(HashMap<String, String> dictionary){
mKeys = new String[dictionary.size()];
mValues = new String[dictionary.size()];

for(String i : dictionary.keySet()){
int count = 0;
mKeys[count]=i;
mValues[count] = dictionary.get(i);
count++;
}

}

public static class MyViewHolder extends RecyclerView.ViewHolder{

public TextView mTextViewTitle;
public TextView mTextViewData;

public MyViewHolder(View v){
super(v);
mTextViewTitle = v.findViewById(R.id.textViewTitle);
mTextViewData = v.findViewById(R.id.textViewData);
}
}

@NonNull
@Override
public MySensorAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_sensor_item, parent, false);

MySensorAdapter.MyViewHolder viewHolder = new MySensorAdapter.MyViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull MySensorAdapter.MyViewHolder holder, int position) {
String title=mKeys[position];
String data=mValues[position];

Log.i("DEBUG POSITION", String.valueOf(position));

holder.mTextViewTitle.setText(title);
holder.mTextViewData.setText(data);
}

@Override
public int getItemCount() {
if(mKeys != null){
Log.i("DEBUG LENGTH", String.valueOf(mKeys.length));

return mKeys.length;
}
return 0;
}

编辑:也许这也有帮助。我只想在 recyclerview 中显示传感器信息

public class SensorDataActivity extends AppCompatActivity implements SensorEventListener {

private SensorManager mSensormanager;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

private HashMap<String, String> list = new HashMap<>();

private Sensor mSensor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_data);

Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);

Log.i("WICHTIG", String.valueOf(position));


mSensormanager = (SensorManager) getSystemService(SENSOR_SERVICE);
assert mSensormanager != null;
List<Sensor> sensors = mSensormanager.getSensorList(Sensor.TYPE_ALL);



//Debugging to be done!
int type = sensors.get(position).getType();

Log.i("WICHTIG", String.valueOf(type));

mSensor = mSensormanager.getDefaultSensor(type);

getSensorInformation(mSensor);

mRecyclerView = findViewById(R.id.my_sensor_recycler_view);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new MySensorAdapter(list);
mRecyclerView.setAdapter(mAdapter);

}

private void getSensorInformation(Sensor sensor){

list.put("Name", sensor.getName());
list.put("Type", String.valueOf(sensor.getType()));
list.put("Version", String.valueOf(sensor.getVersion()));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
list.put("Id", String.valueOf(sensor.getId()));
list.put("Type(String)", sensor.getStringType());
}

list.put("Power", String.valueOf(sensor.getPower()));
list.put("Vendor",sensor.getVendor());
list.put("MaximumRange",String.valueOf(sensor.getMaximumRange()));
list.put("Resolution",String.valueOf(sensor.getResolution()));
list.put("MinDelay",String.valueOf(sensor.getMinDelay()));


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
list.put("MaximumDelay",String.valueOf(sensor.getMaxDelay()));
list.put("FifoMaxEvent",String.valueOf(sensor.getFifoMaxEventCount()));
list.put("FifoReservedEvent",String.valueOf(sensor.getFifoReservedEventCount()));
list.put("ReportingMode",String.valueOf(sensor.getReportingMode()));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
list.put("HighestDirectReportRate",String.valueOf(sensor.getHighestDirectReportRateLevel()));
}



}

@Override
public void onSensorChanged(SensorEvent event) {

for(int i = 0; i<event.values.length; i++) {

String key = "Value"+String.valueOf(i);

list.put(key, String.valueOf(event.values[i]));
}

mAdapter.notifyDataSetChanged();

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
list.put("Accuracy", String.valueOf(accuracy));
mAdapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
super.onResume();
mSensormanager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
super.onPause();
mSensormanager.unregisterListener(this);
}

}

最佳答案

计数值在每次重置为0

for(String i : dictionary.keySet()){
int count = 0;
mKeys[count]=i;
mValues[count] = dictionary.get(i);
count++;
}

因此,用于显示 UI 的数据集每次都只有一个元素。您可以尝试在循环外初始化计数。

关于android - RecyclerView 未填充(数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com