1

I am trying to change the values being displayed in a listview depending on the value from a seekbar. Currently the listview is being populated by a cursor (whos values I dont think i can change once in the list). I am trying to added the cursor into an ArrayList before being added to the list.

My thinking is that I can change the array list elements and in turn update the listview. I am running into issues with creating the array list and then altering it to update the list view.

Example The listview contains 2 columns (name and value), these are what are being returned to the cursor and added into the arrayList. When the seekbar is on value (eg 2), all the value in the arrayList will be multiplied by 2. They are only to be updated in the list and not the database.

Can anyone help push me in the right direction to fix my arrayList and applying the seekbar changes

Cursor and ArrayList

        final Cursor ings = adapter.getIngs(Code);
    ingredients.moveToFirst();
    ArrayList<String> IngsList = new ArrayList<String>();
    while(!ings.isAfterLast())
    {
        Ings.add(ings.getString(ings.getColumnIndex("name")));
        Ings.add(ings.getInt(ings.getColumnIndex("measurement")));
        Ings.add(ings.getString(ings.getColumnIndex("unit")));
    }
    ings.close();


    String[] columns = new String[] {adapter.KEY_NAME, adapter.KEY_MEASUREMENT, adapter.KEY_UNIT};
    int[] to = new int[] {R.id.Name, R.id.Measurement, R.id.Unit};
    final SimpleCursorAdapter Adpater = new SimpleCursorAdapter(this,R.layout.row5, ingredients, ingredient_columns, to, 0);
    ListView Required = (ListView) findViewById(R.id.Required);
    Required.setAdapter(Adpater);

SeekBar

        seekBar = (SeekBar) findViewById(R.id.servingSeek);
    textView = (TextView) findViewById(R.id.actualServing);
    // Initialize the textview with '0'
    textView.setText("Serving Size:" + seekBar.getProgress());
    seekBar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                int progress = 0;

                @Override
                public void onProgressChanged(SeekBar seekBar,
                                              int progresValue, boolean fromUser) {
                    progress = progresValue;
                    textView.setText("Serving Size:" + progress);
                    //alter cursor values

                    //update cursor

                }
JJSmith
  • 1,833
  • 4
  • 17
  • 26

1 Answers1

0

@JJSmith, if you want to carry things in the ArrayList, it would be better you make a class which will represent a row in database with instance variable you want something like this,

class Item{
    private String name;
    private float measurment;
    private float unit;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMeasurment() {
        return measurment;
    }

    public void setMeasurment(float measurment) {
        this.measurment = measurment;
    }

    public float getUnit() {
        return unit;
    }

    public void setUnit(float unit) {
        this.unit = unit;
    }
}

when you read data from the cursor.. you need to implement things something like this

    ArrayList<Item> items = new ArrayList<>();
    Cursor cursor = //make a call to database
    if (cursor.moveToFirst()) {
        Item item;
        for (int i = 0; i < cursor.getCount(); i++) {
            item = new Item();
            item.setName("read data from cursor and add it here"); 
            item.setMeasurment("read data from cursor and add it here"); 
            item.setUnit("read data from cursor and add it here"); 
            myModels.add(item);
            if (!cursor.moveToNext()) {
                break;
            }
        }
    }

Update now that you have the ArrayList of type Item's you can provide this to the custom adapter you would make, here is an example how to do that, you can look at the code in Github

ListView listView = (ListView)findViewById(R.id."listview id from the layout");

make a custom Adapter and provide your list to that, and set the adapter to the listview

listView.setAdapter(adapter)

now the things you are interested in, when you change the seekbar position, you can get the Item's from the arraylist and change the values you want

once you change data in the arraylist, call notifyDataSetChanged() on the Adapter

adapter.notifyDataSetChanged();

and you are done

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
  • interesting, so if i wanted to change the value of measurement I would just do so within the `setMeasurement` function? Also how would i pass all the values of the arrayList into the new class? Sorry for all the questions, just want to make sure I get this right – JJSmith Jan 29 '16 at 17:19
  • @JJSmith, i have updated the answer, I hope this helps – Pankaj Nimgade Jan 29 '16 at 17:54