Taka
Taka

Reputation: 141

RecyclerView not filling(Arrays)

I searched a lot but cannot find out why my RecyclerView is not showing all data. When printing data to the console in "MySensorAdapter" the values are complete.

Code is below, thank you very much.

public class MySensorAdapter extends 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;
}

}

EDIT: Maybe this might help too. I just want to display a sensors information in a 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);
    }

}

Upvotes: 2

Views: 75

Answers (2)

Master Fathi
Master Fathi

Reputation: 349

Just remove from your adapter constructor int count = 0; from the loop initialize it outside before the loop like this

public MySensorAdapter(HashMap<String, String> dictionary) {
        mKeys = new String[dictionary.size()];
        mValues = new String[dictionary.size()];
        int count = 0;
        for (String i : dictionary.keySet()) {
            mKeys[count] = i;
            mValues[count] = dictionary.get(i);
            count++;
        }

    }

and you're good to go

Upvotes: 0

jaede
jaede

Reputation: 859

The count value is reset to 0 every time in the

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

Hence the dataset that is used to display the UI has just one element every time. You can try initialising the count outside the loop.

Upvotes: 2

Related Questions