2016-02-04 71 views
-1

我有一個稍微複雜的問題。我有一個由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> 

回答

2

我覺得有很多不同的方式來實現這一目標。如果我有你的問題,你想要這樣的東西?

demo

這裏是我的版本,你可以如何實現這一點: 你的列表視圖和自定義成分對象來存儲信息從數據庫中每個對象的創建自定義陣列適配器。從您的數據庫獲取數據一次,並更新您的自定義適配器getView方法代碼中更改的每個SeekBar進度的視圖。

演示代碼:

MainActivity.java

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.SeekBar; 
import android.widget.TextView; 

import java.util.ArrayList; 


public class MainActivity extends FragmentActivity implements SeekBar.OnSeekBarChangeListener { 
    private SeekBar seekBar; 
    private ListView listView; 
    private MyCustomAdapter listViewAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     seekBar = (SeekBar) findViewById(R.id.seekBar); 

     listView = (ListView) findViewById(R.id.listView); 
     seekBar.setOnSeekBarChangeListener(this); 
     listViewAdapter = new MyCustomAdapter(getApplicationContext()); 
     listView.setAdapter(listViewAdapter); 

     updateListDataFromDB(); 
    } 

    private void updateListDataFromDB(){ 
     ArrayList<Ingredient> data = listViewAdapter.getDataArray(); 
     data.clear(); 

     //test data instead of data from db 
     data.add(new Ingredient("One", 123, "kg")); 
     data.add(new Ingredient("Two", 15, "kg")); 
     data.add(new Ingredient("Three", 10, "kg")); 

     /* 
     final Cursor ingredient2 = adapter.getRecipesIngredients(recipeCode); 

     ingredient2.moveToFirst(); 

     while(ingredient2.moveToNext()) { 
      String ingredientName = ingredient2.getString(ingredient2.getColumnIndex("ingredient_name")); 
      int newMeasurement = ingredient2.getInt(ingredient2.getColumnIndex("measurement")); 
      String ingredientUnit = ingredient2.getString(ingredient2.getColumnIndex("unit")); 
      Ingredient newIngredient = new Ingredient(ingredientName, newMeasurement, ingredientUnit); 
      data.add(newIngredient); 
     } 

     ingredient2.close(); 
     */ 

     listViewAdapter.notifyDataSetChanged(); 
    } 

    public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) { 
     listViewAdapter.notifyDataSetChanged(); 
    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 

    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 

    } 

    public class MyCustomAdapter extends BaseAdapter{ 
     private ArrayList<Ingredient> data = new ArrayList<>(); 
     private Context context; 

     public MyCustomAdapter(Context context){ 
      this.context = context; 
     } 

     public ArrayList<Ingredient> getDataArray(){ 
      return data; 
     } 

     @Override 
     public int getCount() { 
      return data.size(); 
     } 

     @Override 
     public Ingredient getItem(int position) { 
      return data.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if(convertView == null){ 
       convertView = LayoutInflater.from(context).inflate(R.layout.row, null); 
      } 

      TextView nameView = (TextView) convertView.findViewById(R.id.ingredientName); 
      TextView measurementView = (TextView) convertView.findViewById(R.id.ingredientMeasurement); 
      TextView unitView = (TextView) convertView.findViewById(R.id.ingredientUnit); 
      Ingredient ingredient = getItem(position); 

      nameView.setText(ingredient.name); 
      int currMeasurement = ingredient.measurement * (seekBar.getProgress() + 1); // +1 because seekBar default value is 0; 
      measurementView.setText(String.valueOf(currMeasurement)); 
      unitView.setText(ingredient.unit); 

      return convertView; 
     } 
    } 

    private class Ingredient { 
     public String name; 
     public int measurement; 
     public String unit; 

     public Ingredient(String name, int measurement, String unit) { 
      this.name = name; 
      this.measurement = measurement; 
      this.unit = unit; 
     } 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <SeekBar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/seekBar" 
     android:padding="20dp"/> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:layout_weight="1" /> 
</LinearLayout> 

row.xml - 與您的列表視圖行佈局(ROW5)

+0

真棒........ – GvSharma

+0

你至少應該使用ViewHolder(反)模式 - 我不喜歡它,但你提供的代碼是非常低效的(在調用findViewById的上下文中) – Vasiliy

+0

@varren,非常感謝你!這正是我想要做的!我會盡我所能獎勵賞金,說我必須等待22個小時 – Hayes121

0

您需要調用適配器上的「notifyDataSetChanged」方法才能進行更改。