无法在CustomListAdapter上调用notifyDataSetChanged

时间:2015-03-18 10:28:03

标签: android notifydatasetchanged

我无法刷新扩展ArrayAdapter的CustomListAdapter对象,因此无法使用方法notifyDataSetChanged()。我可以在对象本身内调用它。

知道了这一点,我在自定义适配器类中创建了一个方法,如下所示:

public void refreshAdapter(){
    notifyDataSetChanged();
}

但是我无法在创建适配器对象的活动中调用此方法,所以我回到原点。为什么我无法调用此方法和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;
    }


}

3 个答案:

答案 0 :(得分:1)

不要更改适配器的数据,更改数据源

答案 1 :(得分:1)

MainActivity

更改valueReceipe时
valueReceipe[i] = "Maggiie";

然后调用refresh emthod:

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

答案 2 :(得分:1)

  1. 您不需要:

    public void refreshAdapter() {
    
        notifyDataSetChanged();
    }
    
  2. notifyDataSetChanged是一个公共方法,可以使用。

    1. 您永远不会引用 String []值,(通过使用getItem(position)),这是ArrayAdapter和notifyDataSetChanged()的重点。 notifyDataSetChanged()意味着当您更改适配器中使用的集合项(在您的情况下为值)时从外部调用:

      values[5] = "My changed item";
      
      adapter.notifyDataSetChanged();
      
    2. 史蒂夫乔布斯会说:'你说错了';)