从 Activity 返回后列表视图不更新

发布于 2024-11-14 03:56:10 字数 15039 浏览 1 评论 0原文

我有以下课程,由于某种原因,当选择菜单项 1 时,我启动一个新的活动。当活动返回时,它调用 onActivityResult() 并在该函数内我尝试刷新我的 ListView。不幸的是,由于某些奇怪的原因,它没有更新列表。有谁知道如何解决这个问题?当我通过调试器运行它并在 onActivityResult() 中设置断点时,在活动关闭后,它确实正确地命中了该函数。看起来“m_Adapter.notifyDataSetChange()”行似乎什么也没做。

public class RestaurantZoom extends Activity {
    private ArrayList<FoodItem> m_FoodItems;
    private Context m_Context;
    private FoodItemAdapter m_Adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        m_Context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foodchooser);

        TextView tvName = (TextView) findViewById(R.id.txtTitleShow);

        m_FoodItems = ((ArrayList<FoodItem>) this.getIntent()
                .getSerializableExtra("FoodItems"));
        tvName.setText(this.getIntent().getExtras().getString("RestaurantName"));

        ListView lv = (ListView) findViewById(R.id.listView1);
        m_Adapter = new FoodItemAdapter(this, R.layout.row, m_FoodItems);
        lv.setAdapter(m_Adapter);
        // lv.setBackgroundResource(R.drawable.arbys_logo_small);
        // lv.setCacheColorHint(0);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView av, View v, int index, long arg) {
                FoodItem fi = (FoodItem) av.getAdapter().getItem(index);

                Intent i = new Intent(m_Context, ItemZoom.class);
                i.putExtra("FoodItem", fi);
                try {
                    startActivity(i);
                } catch (Exception e) {
                    Exception ex = e;
                }
            }
        });

        // SetCheckedItems(this);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        SetCheckedItems(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    private void SetCheckedItems(Menu c) {
        MenuItem itemBad = c.findItem(R.id.item2);
        MenuItem itemGood = c.findItem(R.id.item3);

        String PrefFile = getString(R.string.prefname);
        SharedPreferences settings = getSharedPreferences(PrefFile, 0);
        boolean BadChecked = settings.getBoolean("BadChecked", false);
        boolean GoodChecked = settings.getBoolean("GoodChecked", false);
        itemBad.setChecked(BadChecked);
        itemGood.setChecked(GoodChecked);

        // ListView lv = (ListView) findViewById(R.id.listView1);
        // m_Adapter = new FoodItemAdapter(this, R.layout.row,
        // m_FoodItems);
        // lv.setAdapter(m_Adapter);
        m_Adapter.notifyDataSetChanged();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;        
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        m_Adapter.notifyDataSetChanged();

        /*ListView lv = (ListView) findViewById(R.id.listView1);

        m_Adapter = new FoodItemAdapter(this, R.layout.row, m_FoodItems);
        lv.setAdapter(m_Adapter);
        ((BaseAdapter) lv.getAdapter()).notifyDataSetChanged();
        lv.invalidate();*/
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.item1:
            Intent i = new Intent(m_Context, Options.class);

            try {
                // startActivity(i);
                startActivityForResult(i, 1);
            } catch (Exception e) {
                Exception ex = e;
            }
            return true;
        case R.id.item2:
            if (item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            String PrefFile = getString(R.string.prefname);
            SharedPreferences settings = getSharedPreferences(PrefFile, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("BadChecked", item.isChecked());
            editor.commit();
            m_Adapter.notifyDataSetChanged();
            return true;
        case R.id.item3:
            if (item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            String PrefFile2 = getString(R.string.prefname);
            SharedPreferences settings2 = getSharedPreferences(PrefFile2, 0);
            SharedPreferences.Editor editor2 = settings2.edit();
            editor2.putBoolean("GoodChecked", item.isChecked());
            editor2.commit();
            m_Adapter.notifyDataSetChanged();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

编辑

这是我的自定义适配器的代码:

public class FoodItemAdapter extends ArrayAdapter<FoodItem> {
    private ArrayList<FoodItem> m_items;
    private Context m_Context;

    public FoodItemAdapter(Context context, int textViewResourceId,
            ArrayList<FoodItem> items) {
        super(context, textViewResourceId, items);
        this.m_items = items;
        m_Context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) m_Context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        FoodItem fi = m_items.get(position);
        //if (fi != null) {
            TextView tvTop = (TextView) v.findViewById(R.id.txtTextTop);
            TextView tvBottom = (TextView) v.findViewById(R.id.txtTextBottom);
            ImageView iv = (ImageView) v.findViewById(R.id.imgIcon);
            LinearLayout lv = (LinearLayout) v.findViewById(R.id.loMainLayout);
            String PrefFile = m_Context.getString(R.string.prefname);
            SharedPreferences settings = m_Context.getSharedPreferences(PrefFile, 0);

            boolean BadChecked = settings.getBoolean("BadChecked", false);
            boolean GoodChecked = settings.getBoolean("GoodChecked", false);
            int SpinnerValue1 = settings.getInt("Spinner1Value", 0);
            int SpinnerValue2 = settings.getInt("Spinner2Value", 0);
            float NutValue1 = settings.getFloat("NutValue1", 200f);
            float NutValue2 = settings.getFloat("NutValue2", 100f);
            float BaseValue = 0.0f;
            boolean BadHighlighted = false;
            boolean GoodHighlighted = false;

            if (BadChecked)
            {
                if (SpinnerValue1 == 0)
                    BaseValue = fi.GetCalories();
                if (SpinnerValue1 == 1)
                    BaseValue = fi.GetFat();
                if (SpinnerValue1 == 2)
                    BaseValue = fi.GetSatFat();
                if (SpinnerValue1 == 3)
                    BaseValue = fi.GetTransFat();
                if (SpinnerValue1 == 4)
                    BaseValue = fi.GetFat();
                if (SpinnerValue1 == 5)
                    BaseValue = fi.GetCholesterol();
                if (SpinnerValue1 == 6)
                    BaseValue = fi.GetSodium();
                if (SpinnerValue1 == 7)
                    BaseValue = fi.GetCarbs();              
                if (SpinnerValue1 == 8)
                    BaseValue = fi.GetSugar();
                if (SpinnerValue1 == 9)
                    BaseValue = fi.GetProtein();
                if (BaseValue >= NutValue1) {
                    lv.setBackgroundColor(0xff440000);
                    BadHighlighted=true;
                }
            }
            if (GoodChecked)
            {
                if (SpinnerValue2 == 0)
                    BaseValue = fi.GetCalories();
                if (SpinnerValue2 == 1)
                    BaseValue = fi.GetFat();
                if (SpinnerValue2 == 2)
                    BaseValue = fi.GetSatFat();
                if (SpinnerValue2 == 3)
                    BaseValue = fi.GetTransFat();
                if (SpinnerValue2 == 4)
                    BaseValue = fi.GetFat();
                if (SpinnerValue2 == 5)
                    BaseValue = fi.GetCholesterol();
                if (SpinnerValue2 == 6)
                    BaseValue = fi.GetSodium();
                if (SpinnerValue2 == 7)
                    BaseValue = fi.GetCarbs();              
                if (SpinnerValue2 == 8)
                    BaseValue = fi.GetSugar();
                if (SpinnerValue2 == 9)
                    BaseValue = fi.GetProtein();
                if (BaseValue <= NutValue2) {
                    lv.setBackgroundColor(0xff004400);
                    GoodHighlighted=true;
                }
                if ((BaseValue <= NutValue2) && (BadHighlighted))
                    lv.setBackgroundColor(0xff552200);
            }
            if (!BadChecked && !GoodChecked)
            {
                lv.setBackgroundColor(0xff000000);
            }
            if (GoodHighlighted == false && BadHighlighted==false)
            {
                lv.setBackgroundColor(0xff000000);
            }
            //lv.setBackgroundColor(0xffff0000);
            tvTop.setText(fi.GetFoodItem());

            String QuickView = "<b>Calories:</b> " + (new DecimalFormat("#.#")).format(fi.GetCalories());
            QuickView = QuickView + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Fat:</b> " + (new DecimalFormat("#.#")).format(fi.GetFat()) + "g";
            QuickView = QuickView + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Carbs:</b> " + (new DecimalFormat("#.#")).format(fi.GetCarbs()) + "g";
            tvBottom.setText(Html.fromHtml(QuickView));

            if (!fi.GetFoodIcon().equals(""))
            {
                if (fi.GetFoodIcon().equals("hamburger_icon"))
                {
                    iv.setImageResource(R.drawable.hamburger_icon);
                }
                if (fi.GetFoodIcon().equals("chicken_icon"))
                {
                    iv.setImageResource(R.drawable.chicken_icon);
                }
                if (fi.GetFoodIcon().equals("fish_icon"))
                {
                    iv.setImageResource(R.drawable.fish_icon);
                }
                if (fi.GetFoodIcon().equals("fries_icon"))
                {
                    iv.setImageResource(R.drawable.fries_icon);
                }
                if (fi.GetFoodIcon().equals("salad_icon"))
                {
                    iv.setImageResource(R.drawable.salad_icon);
                }
                if (fi.GetFoodIcon().equals("onion_rings_icon"))
                {
                    iv.setImageResource(R.drawable.onion_rings_icon);
                }
                if (fi.GetFoodIcon().equals("soft_drink_icon"))
                {
                    iv.setImageResource(R.drawable.soft_drink_icon);
                }
                if (fi.GetFoodIcon().equals("drink_icon"))
                {
                    iv.setImageResource(R.drawable.soft_drink_icon);
                }
                if (fi.GetFoodIcon().equals("milkshake_icon"))
                {
                    iv.setImageResource(R.drawable.milkshake_icon);
                }
                if (fi.GetFoodIcon().equals("cake_icon"))
                {
                    iv.setImageResource(R.drawable.cake_icon);
                }
                if (fi.GetFoodIcon().equals("breakfast_icon"))
                {
                    iv.setImageResource(R.drawable.bacon_eggs_icon);
                }
                if (fi.GetFoodIcon().equals("hotdog_icon"))
                {
                    iv.setImageResource(R.drawable.hotdog_icon);
                }
                if (fi.GetFoodIcon().equals("root_beer_float_icon"))
                {
                    iv.setImageResource(R.drawable.root_beer_float_icon);
                }
                if (fi.GetFoodIcon().equals("slush_icon"))
                {
                    iv.setImageResource(R.drawable.slush_icon);
                }
                if (fi.GetFoodIcon().equals("wing_icon"))
                {
                    iv.setImageResource(R.drawable.wing_icon);
                }
                if (fi.GetFoodIcon().equals("ice_cream_cone_icon"))
                {
                    iv.setImageResource(R.drawable.ice_cream_cone_icon);
                }
                if (fi.GetFoodIcon().equals("ice_cream_icon"))
                {
                    iv.setImageResource(R.drawable.ice_cream_cone_icon);
                }
                if (fi.GetFoodIcon().equals("smoothie_icon"))
                {
                    iv.setImageResource(R.drawable.smoothie_icon);
                }
                if (fi.GetFoodIcon().equals("sub_icon"))
                {
                    iv.setImageResource(R.drawable.sub_icon);
                }
                if (fi.GetFoodIcon().equals("sandwich_icon"))
                {
                    iv.setImageResource(R.drawable.sandwich_icon);
                }
                if (fi.GetFoodIcon().equals("soup_icon"))
                {
                    iv.setImageResource(R.drawable.soup_icon);
                }
                if (fi.GetFoodIcon().equals("potato_chips_icon"))
                {
                    iv.setImageResource(R.drawable.potato_chips_icon);
                }
                if (fi.GetFoodIcon().equals("pizza_icon"))
                {
                    iv.setImageResource(R.drawable.pizza_icon);
                }
                if (fi.GetFoodIcon().equals("taco_icon"))
                {
                    iv.setImageResource(R.drawable.taco_icon);
                }
                if (fi.GetFoodIcon().equals("burrito_icon"))
                {
                    iv.setImageResource(R.drawable.burrito_icon);
                }
                if (fi.GetFoodIcon().equals("nachos_icon"))
                {
                    iv.setImageResource(R.drawable.nachos_icon);
                }
                if (fi.GetFoodIcon().equals("noodles_icon"))
                {
                    iv.setImageResource(R.drawable.noodles_icon);
                }
                if (fi.GetFoodIcon().equals("tomato_icon"))
                {
                    iv.setImageResource(R.drawable.tomato_icon);
                }
                if (fi.GetFoodIcon().equals("beef_icon"))
                {
                    iv.setImageResource(R.drawable.beef_icon);
                }
            } else {
                iv.setVisibility(View.INVISIBLE);
            }
        //}
        return v;
    }
}

I have the following class and for some reason, when menu item1 is selected, I launch a new Activity. When the activity returns, it calls onActivityResult() and inside that function I am trying to refresh my ListView. Unfortunately, for some odd reason, it is not updating the list. Does anyone have any idea on how to fix this? When I run it through the debugger and set a breakpoint in onActivityResult(), it does indeed hit the function correctly, after the activity closes. It just seems the line "m_Adapter.notifyDataSetChange()" seems to do nothing.

public class RestaurantZoom extends Activity {
    private ArrayList<FoodItem> m_FoodItems;
    private Context m_Context;
    private FoodItemAdapter m_Adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        m_Context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foodchooser);

        TextView tvName = (TextView) findViewById(R.id.txtTitleShow);

        m_FoodItems = ((ArrayList<FoodItem>) this.getIntent()
                .getSerializableExtra("FoodItems"));
        tvName.setText(this.getIntent().getExtras().getString("RestaurantName"));

        ListView lv = (ListView) findViewById(R.id.listView1);
        m_Adapter = new FoodItemAdapter(this, R.layout.row, m_FoodItems);
        lv.setAdapter(m_Adapter);
        // lv.setBackgroundResource(R.drawable.arbys_logo_small);
        // lv.setCacheColorHint(0);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView av, View v, int index, long arg) {
                FoodItem fi = (FoodItem) av.getAdapter().getItem(index);

                Intent i = new Intent(m_Context, ItemZoom.class);
                i.putExtra("FoodItem", fi);
                try {
                    startActivity(i);
                } catch (Exception e) {
                    Exception ex = e;
                }
            }
        });

        // SetCheckedItems(this);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        SetCheckedItems(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    private void SetCheckedItems(Menu c) {
        MenuItem itemBad = c.findItem(R.id.item2);
        MenuItem itemGood = c.findItem(R.id.item3);

        String PrefFile = getString(R.string.prefname);
        SharedPreferences settings = getSharedPreferences(PrefFile, 0);
        boolean BadChecked = settings.getBoolean("BadChecked", false);
        boolean GoodChecked = settings.getBoolean("GoodChecked", false);
        itemBad.setChecked(BadChecked);
        itemGood.setChecked(GoodChecked);

        // ListView lv = (ListView) findViewById(R.id.listView1);
        // m_Adapter = new FoodItemAdapter(this, R.layout.row,
        // m_FoodItems);
        // lv.setAdapter(m_Adapter);
        m_Adapter.notifyDataSetChanged();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;        
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        m_Adapter.notifyDataSetChanged();

        /*ListView lv = (ListView) findViewById(R.id.listView1);

        m_Adapter = new FoodItemAdapter(this, R.layout.row, m_FoodItems);
        lv.setAdapter(m_Adapter);
        ((BaseAdapter) lv.getAdapter()).notifyDataSetChanged();
        lv.invalidate();*/
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.item1:
            Intent i = new Intent(m_Context, Options.class);

            try {
                // startActivity(i);
                startActivityForResult(i, 1);
            } catch (Exception e) {
                Exception ex = e;
            }
            return true;
        case R.id.item2:
            if (item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            String PrefFile = getString(R.string.prefname);
            SharedPreferences settings = getSharedPreferences(PrefFile, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("BadChecked", item.isChecked());
            editor.commit();
            m_Adapter.notifyDataSetChanged();
            return true;
        case R.id.item3:
            if (item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            String PrefFile2 = getString(R.string.prefname);
            SharedPreferences settings2 = getSharedPreferences(PrefFile2, 0);
            SharedPreferences.Editor editor2 = settings2.edit();
            editor2.putBoolean("GoodChecked", item.isChecked());
            editor2.commit();
            m_Adapter.notifyDataSetChanged();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

EDIT

Here is the code for my custom adapter:

public class FoodItemAdapter extends ArrayAdapter<FoodItem> {
    private ArrayList<FoodItem> m_items;
    private Context m_Context;

    public FoodItemAdapter(Context context, int textViewResourceId,
            ArrayList<FoodItem> items) {
        super(context, textViewResourceId, items);
        this.m_items = items;
        m_Context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) m_Context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        FoodItem fi = m_items.get(position);
        //if (fi != null) {
            TextView tvTop = (TextView) v.findViewById(R.id.txtTextTop);
            TextView tvBottom = (TextView) v.findViewById(R.id.txtTextBottom);
            ImageView iv = (ImageView) v.findViewById(R.id.imgIcon);
            LinearLayout lv = (LinearLayout) v.findViewById(R.id.loMainLayout);
            String PrefFile = m_Context.getString(R.string.prefname);
            SharedPreferences settings = m_Context.getSharedPreferences(PrefFile, 0);

            boolean BadChecked = settings.getBoolean("BadChecked", false);
            boolean GoodChecked = settings.getBoolean("GoodChecked", false);
            int SpinnerValue1 = settings.getInt("Spinner1Value", 0);
            int SpinnerValue2 = settings.getInt("Spinner2Value", 0);
            float NutValue1 = settings.getFloat("NutValue1", 200f);
            float NutValue2 = settings.getFloat("NutValue2", 100f);
            float BaseValue = 0.0f;
            boolean BadHighlighted = false;
            boolean GoodHighlighted = false;

            if (BadChecked)
            {
                if (SpinnerValue1 == 0)
                    BaseValue = fi.GetCalories();
                if (SpinnerValue1 == 1)
                    BaseValue = fi.GetFat();
                if (SpinnerValue1 == 2)
                    BaseValue = fi.GetSatFat();
                if (SpinnerValue1 == 3)
                    BaseValue = fi.GetTransFat();
                if (SpinnerValue1 == 4)
                    BaseValue = fi.GetFat();
                if (SpinnerValue1 == 5)
                    BaseValue = fi.GetCholesterol();
                if (SpinnerValue1 == 6)
                    BaseValue = fi.GetSodium();
                if (SpinnerValue1 == 7)
                    BaseValue = fi.GetCarbs();              
                if (SpinnerValue1 == 8)
                    BaseValue = fi.GetSugar();
                if (SpinnerValue1 == 9)
                    BaseValue = fi.GetProtein();
                if (BaseValue >= NutValue1) {
                    lv.setBackgroundColor(0xff440000);
                    BadHighlighted=true;
                }
            }
            if (GoodChecked)
            {
                if (SpinnerValue2 == 0)
                    BaseValue = fi.GetCalories();
                if (SpinnerValue2 == 1)
                    BaseValue = fi.GetFat();
                if (SpinnerValue2 == 2)
                    BaseValue = fi.GetSatFat();
                if (SpinnerValue2 == 3)
                    BaseValue = fi.GetTransFat();
                if (SpinnerValue2 == 4)
                    BaseValue = fi.GetFat();
                if (SpinnerValue2 == 5)
                    BaseValue = fi.GetCholesterol();
                if (SpinnerValue2 == 6)
                    BaseValue = fi.GetSodium();
                if (SpinnerValue2 == 7)
                    BaseValue = fi.GetCarbs();              
                if (SpinnerValue2 == 8)
                    BaseValue = fi.GetSugar();
                if (SpinnerValue2 == 9)
                    BaseValue = fi.GetProtein();
                if (BaseValue <= NutValue2) {
                    lv.setBackgroundColor(0xff004400);
                    GoodHighlighted=true;
                }
                if ((BaseValue <= NutValue2) && (BadHighlighted))
                    lv.setBackgroundColor(0xff552200);
            }
            if (!BadChecked && !GoodChecked)
            {
                lv.setBackgroundColor(0xff000000);
            }
            if (GoodHighlighted == false && BadHighlighted==false)
            {
                lv.setBackgroundColor(0xff000000);
            }
            //lv.setBackgroundColor(0xffff0000);
            tvTop.setText(fi.GetFoodItem());

            String QuickView = "<b>Calories:</b> " + (new DecimalFormat("#.#")).format(fi.GetCalories());
            QuickView = QuickView + "        <b>Fat:</b> " + (new DecimalFormat("#.#")).format(fi.GetFat()) + "g";
            QuickView = QuickView + "        <b>Carbs:</b> " + (new DecimalFormat("#.#")).format(fi.GetCarbs()) + "g";
            tvBottom.setText(Html.fromHtml(QuickView));

            if (!fi.GetFoodIcon().equals(""))
            {
                if (fi.GetFoodIcon().equals("hamburger_icon"))
                {
                    iv.setImageResource(R.drawable.hamburger_icon);
                }
                if (fi.GetFoodIcon().equals("chicken_icon"))
                {
                    iv.setImageResource(R.drawable.chicken_icon);
                }
                if (fi.GetFoodIcon().equals("fish_icon"))
                {
                    iv.setImageResource(R.drawable.fish_icon);
                }
                if (fi.GetFoodIcon().equals("fries_icon"))
                {
                    iv.setImageResource(R.drawable.fries_icon);
                }
                if (fi.GetFoodIcon().equals("salad_icon"))
                {
                    iv.setImageResource(R.drawable.salad_icon);
                }
                if (fi.GetFoodIcon().equals("onion_rings_icon"))
                {
                    iv.setImageResource(R.drawable.onion_rings_icon);
                }
                if (fi.GetFoodIcon().equals("soft_drink_icon"))
                {
                    iv.setImageResource(R.drawable.soft_drink_icon);
                }
                if (fi.GetFoodIcon().equals("drink_icon"))
                {
                    iv.setImageResource(R.drawable.soft_drink_icon);
                }
                if (fi.GetFoodIcon().equals("milkshake_icon"))
                {
                    iv.setImageResource(R.drawable.milkshake_icon);
                }
                if (fi.GetFoodIcon().equals("cake_icon"))
                {
                    iv.setImageResource(R.drawable.cake_icon);
                }
                if (fi.GetFoodIcon().equals("breakfast_icon"))
                {
                    iv.setImageResource(R.drawable.bacon_eggs_icon);
                }
                if (fi.GetFoodIcon().equals("hotdog_icon"))
                {
                    iv.setImageResource(R.drawable.hotdog_icon);
                }
                if (fi.GetFoodIcon().equals("root_beer_float_icon"))
                {
                    iv.setImageResource(R.drawable.root_beer_float_icon);
                }
                if (fi.GetFoodIcon().equals("slush_icon"))
                {
                    iv.setImageResource(R.drawable.slush_icon);
                }
                if (fi.GetFoodIcon().equals("wing_icon"))
                {
                    iv.setImageResource(R.drawable.wing_icon);
                }
                if (fi.GetFoodIcon().equals("ice_cream_cone_icon"))
                {
                    iv.setImageResource(R.drawable.ice_cream_cone_icon);
                }
                if (fi.GetFoodIcon().equals("ice_cream_icon"))
                {
                    iv.setImageResource(R.drawable.ice_cream_cone_icon);
                }
                if (fi.GetFoodIcon().equals("smoothie_icon"))
                {
                    iv.setImageResource(R.drawable.smoothie_icon);
                }
                if (fi.GetFoodIcon().equals("sub_icon"))
                {
                    iv.setImageResource(R.drawable.sub_icon);
                }
                if (fi.GetFoodIcon().equals("sandwich_icon"))
                {
                    iv.setImageResource(R.drawable.sandwich_icon);
                }
                if (fi.GetFoodIcon().equals("soup_icon"))
                {
                    iv.setImageResource(R.drawable.soup_icon);
                }
                if (fi.GetFoodIcon().equals("potato_chips_icon"))
                {
                    iv.setImageResource(R.drawable.potato_chips_icon);
                }
                if (fi.GetFoodIcon().equals("pizza_icon"))
                {
                    iv.setImageResource(R.drawable.pizza_icon);
                }
                if (fi.GetFoodIcon().equals("taco_icon"))
                {
                    iv.setImageResource(R.drawable.taco_icon);
                }
                if (fi.GetFoodIcon().equals("burrito_icon"))
                {
                    iv.setImageResource(R.drawable.burrito_icon);
                }
                if (fi.GetFoodIcon().equals("nachos_icon"))
                {
                    iv.setImageResource(R.drawable.nachos_icon);
                }
                if (fi.GetFoodIcon().equals("noodles_icon"))
                {
                    iv.setImageResource(R.drawable.noodles_icon);
                }
                if (fi.GetFoodIcon().equals("tomato_icon"))
                {
                    iv.setImageResource(R.drawable.tomato_icon);
                }
                if (fi.GetFoodIcon().equals("beef_icon"))
                {
                    iv.setImageResource(R.drawable.beef_icon);
                }
            } else {
                iv.setVisibility(View.INVISIBLE);
            }
        //}
        return v;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-11-21 03:56:10

notifyDataSetChanged() 只是告诉适配器通知视图重新加载其内容...

通知附加视图底层数据已更改,并且应该自行刷新。

您需要在调用此适配器之前更新适配器,否则视图将简单地从适配器重新加载相同的数据。

notifyDataSetChanged() simply tells the adapter to notify the view to reload its contents...

Notifies the attached View that the underlying data has been changed and it should refresh itself.

You need to update the adapter before calling this otherwise the view will simply reload the same data from the adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文