gpt4 book ai didi

java - Android ArrayList 更新值

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:04 25 4
gpt4 key购买 nike

我正在尝试根据搜索栏的值更改 ListView 中显示的值。目前 ListView 正在由光标填充(我认为我无法在列表中更改其值)。我正在尝试将光标添加到 ArrayList 中,然后再添加到列表中。

我的想法是我可以更改数组列表元素,然后更新 ListView 。我在创建数组列表然后更改它以更新 ListView 时遇到问题。

示例 ListView 包含 2 列(名称和值),这些列将返回到光标并添加到 arrayList 中。当seekbar为值时(例如2),arrayList中的所有值将乘以2。它们仅在列表中更新,而不在数据库中更新。

任何人都可以帮助我朝正确的方向前进,以修复我的 arrayList 并应用搜索栏更改

游标和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;
}
}
}

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");

创建一个自定义适配器并为其提供您的列表,并将适配器设置为listview

listView.setAdapter(adapter)

现在你感兴趣的东西,当你改变seekbar位置时,你可以从数组列表中获取Item并改变你想要的值

更改数组列表中的数据后,请在适配器上调用 notifyDataSetChanged()

adapter.notifyDataSetChanged();

你就完成了

关于java - Android ArrayList 更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35087559/

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