我正在尝试改变列表视图中显示的值,具体取决于seekbar的值。 目前列表视图正在被光标填充(我认为我不能在列表中更改一次的值)。 我想在添加到列表之前将光标添加到ArrayList中。
我的想法是,我可以更改数组列表元素,并依次更新列表视图。 我遇到了创建数组列表的问题,然后改变它来更新列表视图。
示例 listview包含2列(名称和值),这些列将返回到游标并添加到arrayList中。 当seekbar值(例如2)时,arrayList中的所有值都将乘以2.它们只能在列表中而不是在数据库中更新。
任何人都可以帮助我在正确的方向上解决我的arrayList和应用seekbar更改
游标和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) 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 }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,如果你想在ArrayList中携带东西,那么最好你创建一个类,它将代表数据库中的一行,并且你想要类似这样的实例变量,
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; } }当你从光标读取数据..你需要实现这样的事情
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; } } }ListView listView = (ListView)findViewById(R.id."listview id from the layout");现在更新你有类型Item's ArrayList ,你可以将它提供给你要做的自定义适配器,这里有一个例子如何做,你可以看一下Github中的代码
制作一个自定义Adapter并提供你的列表,并将适配器设置为listview
listView.setAdapter(adapter)现在你感兴趣的东西,当你改变搜索栏位置时,你可以从数组列表中获取项目并更改你想要的值
一旦你更改了arraylist中的数据,就在Adapter上调用notifyDataSetChanged()
adapter.notifyDataSetChanged();你就完成了
@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; } } }ListView listView = (ListView)findViewById(R.id."listview id from the layout");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
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
更多推荐
发布评论