gpt4 book ai didi

android - 从 Activity 返回后 Listview 不更新

转载 作者:行者123 更新时间:2023-11-29 00:46:16 24 4
gpt4 key购买 nike

I have the following class and for some reason, when menu item1 is selected, I launch a new Activity.当 Activity 返回时,它调用 onActivityResult() 并在该函数内我试图刷新我的 ListView。不幸的是,出于某种奇怪的原因,它没有更新列表。有谁知道如何解决这个问题?当我通过调试器运行它并在 onActivityResult() 中设置断点时,它确实在 Activity 关闭后正确地命中了函数。 “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;
}
}

最佳答案

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

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

您需要在调用此之前更新适配器,否则 View 只会从适配器重新加载相同的数据。

关于android - 从 Activity 返回后 Listview 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com