无法在 CustomListAdapter 上调用 notifyDataSetChanged
Unable to call notifyDataSetChanged on CustomListAdapter
我无法刷新扩展 ArrayAdapter 的 CustomListAdapter 对象,方法 notifyDataSetChanged() 在其上不可用。不过,我可以在对象本身中调用它。
知道这一点后,我在自定义适配器 class 中创建了一个方法,如下所示:
public void refreshAdapter(){
notifyDataSetChanged();
}
但我无法在创建适配器对象的 activity 中调用此方法,因此我回到原点。为什么我无法调用此方法和 notifyDataSetChanged()?
这是我要求的适配器:
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.henleyb.aeropressbrewer.R;
import com.henleyb.aeropressbrewer.model.CoffeeClass;
public class RecipeDetailsListAdapter extends ArrayAdapter {
public SharedPreferences sharedPrefs;
private TextView greenBox;
private TextView peachBox;
private String grindSize = "non set";
private String tempType;
public RecipeDetailsListAdapter(Context context, String[] values) {
super(context, R.layout.d_brew_coffee_list_item, values);
sharedPrefs = context.getSharedPreferences("com.henleyb.aeropressbrewer", 0);
}
public void refreshAdapter(){
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.d_brew_coffee_list_item, parent, false);
// text for the list items (changes to populate everything)
greenBox = (TextView) theView.findViewById(R.id.tvGreenText);
peachBox = (TextView) theView.findViewById(R.id.tvPeachText);
// Depending on the position of the list, we change the row data to suit
switch (position) {
case 0:
getGrams();
break;
case 1:
getTemp();
Log.i("HEN","We're in getTemp switch");
break;
case 2:
getGrindSize();
break;
case 3:
getInverted();
break;
default:
Log.i("HEN", "INVALID SWITCH POS");
}
notifyDataSetChanged();
return theView;
}
private void getGrams() {
Log.v("HEN", "position = 0");
greenBox.setText("Grams");
peachBox.setText(Integer.toString(CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsGrams()));
}
private void getTemp() {
Log.i("HEN","We're in getTemp method!");
greenBox.setText("Temp");
tempType = sharedPrefs.getString("TEMPTYPE", "fahrenheit");
switch (tempType) {
case "celsius":
Log.i("HEN","tempType now celsius");
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "\u00b0C");
break;
case "fahrenheit":
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "\u00b0f");
break;
default:
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "!");
}
}
private void getGrindSize() {
Log.v("HEN", "position = 1");
greenBox.setText("Grind Size");
peachBox.setText(convertGrindSize());
}
private void getInverted() {
Log.v("HEN", "position = 3");
greenBox.setText("Inverted?");
if (CoffeeClass.getCurrentSelectedCoffeeObject().getBrewID() == 1) {
if (CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeInverted()) {
peachBox.setText("YES");
} else {
peachBox.setText("NO");
}
} else {
peachBox.setText("NA");
}
}
private String convertGrindSize() {
Integer gs = CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsGrindSize();
if (gs >= 0 && gs <= 20) {
grindSize = "Turkish";
} else if (gs > 20 && gs <= 30) {
grindSize = "Very \nFine";
} else if (gs > 30 && gs <= 40) {
grindSize = "Fine";
} else if (gs > 40 && gs <= 50) {
grindSize = "Medium \nFine";
} else if (gs > 50 && gs <= 60) {
grindSize = "Medium";
} else if (gs > 60 && gs <= 70) {
grindSize = "Medium \nCoarse";
} else if (gs > 70 && gs <= 80) {
grindSize = "Coarse";
} else if (gs > 80 && gs <= 90) {
grindSize = "Very \nCoarse";
} else if (gs > 90 && gs <= 100) {
grindSize = "Like Huge \nBoulders";
}
return grindSize;
}
}
不要更改适配器的数据,更改数据的来源
当您从 MainActivity 更改 valueReceipe 时:
valueReceipe[i] = "Maggiie";
然后调用刷新方法:
RecipeDetailsListAdapter adapter = new RecipeDetailsListAdapter(getApplicationContext(), valueRecipe);
adapter.refreshAdapter(valueReceipe);
像这样更改 refreshAdapter() 主体:
public void refreshAdapter(String[] changedValues){
values = changedValues;
this.notifyDataSetChanged();
}
声明值 String[] 也 :
private String[] values; // define above constructor
你不需要:
public void refreshAdapter() {
notifyDataSetChanged();
}
notifyDataSetChanged 是一个 public 方法并且可用。
您永远不会引用 String[] 值 ,(通过使用 getItem(position)),这是 ArrayAdapter 和 notifyDataSetChanged() 的全部要点。 notifyDataSetChanged() 意味着当您更改适配器中使用的集合项(您的情况下的值)时从外部调用:
values[5] = "My changed item";
adapter.notifyDataSetChanged();
正如史蒂夫·乔布斯所说:“你的想法是错误的”;)
我无法刷新扩展 ArrayAdapter 的 CustomListAdapter 对象,方法 notifyDataSetChanged() 在其上不可用。不过,我可以在对象本身中调用它。
知道这一点后,我在自定义适配器 class 中创建了一个方法,如下所示:
public void refreshAdapter(){
notifyDataSetChanged();
}
但我无法在创建适配器对象的 activity 中调用此方法,因此我回到原点。为什么我无法调用此方法和 notifyDataSetChanged()?
这是我要求的适配器:
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.henleyb.aeropressbrewer.R;
import com.henleyb.aeropressbrewer.model.CoffeeClass;
public class RecipeDetailsListAdapter extends ArrayAdapter {
public SharedPreferences sharedPrefs;
private TextView greenBox;
private TextView peachBox;
private String grindSize = "non set";
private String tempType;
public RecipeDetailsListAdapter(Context context, String[] values) {
super(context, R.layout.d_brew_coffee_list_item, values);
sharedPrefs = context.getSharedPreferences("com.henleyb.aeropressbrewer", 0);
}
public void refreshAdapter(){
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.d_brew_coffee_list_item, parent, false);
// text for the list items (changes to populate everything)
greenBox = (TextView) theView.findViewById(R.id.tvGreenText);
peachBox = (TextView) theView.findViewById(R.id.tvPeachText);
// Depending on the position of the list, we change the row data to suit
switch (position) {
case 0:
getGrams();
break;
case 1:
getTemp();
Log.i("HEN","We're in getTemp switch");
break;
case 2:
getGrindSize();
break;
case 3:
getInverted();
break;
default:
Log.i("HEN", "INVALID SWITCH POS");
}
notifyDataSetChanged();
return theView;
}
private void getGrams() {
Log.v("HEN", "position = 0");
greenBox.setText("Grams");
peachBox.setText(Integer.toString(CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsGrams()));
}
private void getTemp() {
Log.i("HEN","We're in getTemp method!");
greenBox.setText("Temp");
tempType = sharedPrefs.getString("TEMPTYPE", "fahrenheit");
switch (tempType) {
case "celsius":
Log.i("HEN","tempType now celsius");
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "\u00b0C");
break;
case "fahrenheit":
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "\u00b0f");
break;
default:
peachBox.setText(String.valueOf((int) CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsTemp()) + "!");
}
}
private void getGrindSize() {
Log.v("HEN", "position = 1");
greenBox.setText("Grind Size");
peachBox.setText(convertGrindSize());
}
private void getInverted() {
Log.v("HEN", "position = 3");
greenBox.setText("Inverted?");
if (CoffeeClass.getCurrentSelectedCoffeeObject().getBrewID() == 1) {
if (CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeInverted()) {
peachBox.setText("YES");
} else {
peachBox.setText("NO");
}
} else {
peachBox.setText("NA");
}
}
private String convertGrindSize() {
Integer gs = CoffeeClass.getCurrentSelectedCoffeeObject().getRecipeDetailsGrindSize();
if (gs >= 0 && gs <= 20) {
grindSize = "Turkish";
} else if (gs > 20 && gs <= 30) {
grindSize = "Very \nFine";
} else if (gs > 30 && gs <= 40) {
grindSize = "Fine";
} else if (gs > 40 && gs <= 50) {
grindSize = "Medium \nFine";
} else if (gs > 50 && gs <= 60) {
grindSize = "Medium";
} else if (gs > 60 && gs <= 70) {
grindSize = "Medium \nCoarse";
} else if (gs > 70 && gs <= 80) {
grindSize = "Coarse";
} else if (gs > 80 && gs <= 90) {
grindSize = "Very \nCoarse";
} else if (gs > 90 && gs <= 100) {
grindSize = "Like Huge \nBoulders";
}
return grindSize;
}
}
不要更改适配器的数据,更改数据的来源
当您从 MainActivity 更改 valueReceipe 时:
valueReceipe[i] = "Maggiie";
然后调用刷新方法:
RecipeDetailsListAdapter adapter = new RecipeDetailsListAdapter(getApplicationContext(), valueRecipe);
adapter.refreshAdapter(valueReceipe);
像这样更改 refreshAdapter() 主体:
public void refreshAdapter(String[] changedValues){
values = changedValues;
this.notifyDataSetChanged();
}
声明值 String[] 也 :
private String[] values; // define above constructor
你不需要:
public void refreshAdapter() { notifyDataSetChanged(); }
notifyDataSetChanged 是一个 public 方法并且可用。
您永远不会引用 String[] 值 ,(通过使用 getItem(position)),这是 ArrayAdapter 和 notifyDataSetChanged() 的全部要点。 notifyDataSetChanged() 意味着当您更改适配器中使用的集合项(您的情况下的值)时从外部调用:
values[5] = "My changed item"; adapter.notifyDataSetChanged();
正如史蒂夫·乔布斯所说:“你的想法是错误的”;)