我有一個稍微複雜的問題。我有一個由SimpleCursorAdapter填充的listview(每行包含3個textview),在listview之上是一個Seekbar。什麼意思發生是當移動seekbar時,中間textview的值應該改變(中間textview * seekbar.getProgress()的值)。用多個文本視圖填充列表
爲了做到這一點,我已經創建了一個從數據庫返回所需信息的光標,這些值存儲在變量和搜索條進度成倍增加,並最終加入到一個數組
的問題,我我正在使用數組中的新值填充listview。當我滑動搜索欄時,listview中的原始值就消失了,並且沒有新的值被輸入到列表中。
我的問題是,如何將所有3個textviews中的新值填充到listview中?
代碼
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
textView.setText("Serving Size:" + progress);
int newServings = seekBar.getProgress();
//read adapter into array multiplying measurement as you go and display new adapter
final Cursor ingredient2 = adapter.getRecipesIngredients(recipeCode);
ingredient2.moveToFirst();
ArrayList<String> newIngredients = new ArrayList<String>();
while(ingredient2.moveToNext()) {
String ingredientName = ingredient2.getString(ingredient2.getColumnIndex("ingredient_name"));
int newMeasurement = ingredient2.getInt(ingredient2.getColumnIndex("measurement"));
newMeasurement = newMeasurement * progress;
String newMes = Integer.toString(newMeasurement);
String ingredientUnit = ingredient2.getString(ingredient2.getColumnIndex("unit"));
newIngredients.add(ingredientName);
newIngredients.add(newMes);
newIngredients.add(ingredientUnit);
}
ingredient2.close();
ArrayAdapter newList = new ArrayAdapter<String>(getApplicationContext(), R.layout.row4,newIngredients);
ListView ingredientsRecquired2 = (ListView) findViewById(R.id.ingredientsRequired);
ingredientsRecquired2.setAdapter(newList);
}
列表視圖行佈局(ROW5)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/ingredientName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#000000"/>
<TextView
android:id="@+id/ingredientMeasurement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ingredientName"
android:padding="5dp"
android:textColor="#000000"/>
<TextView
android:id="@+id/ingredientUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ingredientMeasurement"
android:padding="5dp"
android:textColor="#000000"/>
</RelativeLayout>
真棒........ – GvSharma
你至少應該使用ViewHolder(反)模式 - 我不喜歡它,但你提供的代碼是非常低效的(在調用findViewById的上下文中) – Vasiliy
@varren,非常感謝你!這正是我想要做的!我會盡我所能獎勵賞金,說我必須等待22個小時 – Hayes121