我有一个稍微复杂的问题。我有一个listview(每行包含3个textviews),由SimpleCursorAdapter填充,listview上方是Seekbar。意味着要发生的是当移动搜索栏时,中间文本视图的值应该改变(中间textview * seekbar.getProgress()的值)。
为了做到这一点,我创建了一个Cursor,它从数据库中返回所需的信息,这些值存储在变量中并乘以seekbar进度,最后添加到数组中
我遇到的问题是使用数组中的新值填充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);
}
列表视图行布局(第5行)
<?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>
答案 0 :(得分:2)
我认为有很多不同的方法可以实现这一目标。如果我的问题正确,你想要这样的东西吗?
https://gist.github.com/bradwilson/8423477
以下是我的版本如何实现: 为列表视图创建自定义数组适配器,并为自定义Ingredient对象创建每个对象的db信息。从您的数据库获取数据,并在自定义适配器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 - 与Listview行布局(第5行)相同
答案 1 :(得分:0)
你需要调用&#34; notifyDataSetChanged&#34;适配器上的方法来进行更改。