gpt4 book ai didi

android - ListView 不更新行 View

转载 作者:行者123 更新时间:2023-11-30 02:36:55 25 4
gpt4 key购买 nike

我已经使用 ArrayAdapter 创建了一个自定义 ListView。 ListView 的行有两个 View ,一个是主视图,另一个是隐藏的,当单击主视图上的 TextView 时它会出现。

为了在行上显示数据,我创建了一个对象。我的 ListView 中遇到了一些问题。

  1. 这个问题有时会出现。每当我从 ListView 中删除一个项目时,我都可以看到该项目已从我的 ArrayList 中删除,然后我调用 notifyDataSetChanged。问题是 ListView 仍然显示已删除的行,但它不可点击(只有 View 在那里)。只有尚未删除的其他行是可点击的。检查图 1。

Issue 1

  1. 我在我的对象中使用了一个标志来隐藏/显示隐藏的 View 。但有时它看起来并不好。检查图像 2 Issue 2

  2. 有时在隐藏隐藏 View 后它仍然显示在主视图下方。检查图像 3 Issue 3

请帮我解决这个问题。

class ShippingAdapter extends ArrayAdapter<ShippingOption>
{
ShippingAdapter(Context context, ArrayList<ShippingOption> list)
{
super(context, R.layout.row_shipping, R.id.row_shipping_tv_product_name, list);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
ShippingViewHolder holder = (ShippingViewHolder) row.getTag();
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}

final ShippingOption shippingOption = getShippingOption(position);

holder.tvRemove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showRemoveDialog(shippingOption.getProductId(), position);
}
});
holder.tvProductName.setText(shippingOption.getProductName());
imageLoader.DisplayImage(shippingOption.getProductImageUrl(), holder.ivProduct);

ArrayAdapter<String> adapterShippingOption = createAdapter(holder.spinnerShippingOption, shippingOption.getListShipping(), R.layout.spinner_textview_blue);
holder.spinnerShippingOption.setSelection(adapterShippingOption.getPosition(shippingOption.getSelectedOption()));
if (shippingOption.getSelectedOption().equals(Constants.PICK_UP_AT_STORE))
{
HashMap<String, Integer> mapPickupAddress = shippingOption.getMapPickupAddress();
Collection<String> collection = mapPickupAddress.keySet();
ArrayList<String> listAddress = new ArrayList<String>();
listAddress.addAll(collection);

ArrayAdapter<String> adapterAddress = createAdapter(holder.spinnerAddress, listAddress, R.layout.spinner_textview_blue_small);
holder.spinnerAddress.setSelection(adapterAddress.getPosition(shippingOption.getSelectedAddress()));
holder.relativeAddress.setVisibility(View.VISIBLE);
holder.spinnerAddress.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedAddress = parent.getItemAtPosition(position).toString();

String oldAddress = shippingOption.getSelectedAddress();
shippingOption.setSelectedAddress(selectedAddress);
if (oldAddress == null || !oldAddress.equals(selectedAddress))
notifyDataSetChanged();
}

@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
} else
{
holder.relativeAddress.setVisibility(View.GONE);
}

holder.spinnerShippingOption.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
// if (!selectedOption.equals(Constants.FREE_SHIPPING))
// {
String oldOption = shippingOption.getSelectedOption();
shippingOption.setSelectedOption(selectedOption);
if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
// }
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
holder.relativeLayoutGift.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
if (shippingOption.isGiftWrapAvailable())
{
imageLoader.DisplayImage(shippingOption.getGiftImageUrl(), holder.ivGift);
holder.layoutGift.setVisibility(View.VISIBLE);
ArrayList<String> listGift = new ArrayList<String>();
listGift.add("Not a Gift");
listGift.add("Yes it's a Gift $" + shippingOption.getGiftServiceCharge());
ArrayAdapter<String> adapterGift = createAdapter(holder.spinnerGift, listGift, R.layout.spinner_textview_blue);

holder.tvGiftDetails.setVisibility(shippingOption.getSelectedGiftOption().equals("Not a Gift") ? View.GONE : View.VISIBLE);
holder.spinnerGift.setSelection(adapterGift.getPosition(shippingOption.getSelectedGiftOption()));
holder.spinnerGift.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
String oldOption = shippingOption.getSelectedGiftOption();
shippingOption.setSelectedGiftOption(selectedOption);

if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});

holder.tvGiftDetails.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(true);
notifyDataSetChanged();
}
});

holder.relativeGiftDetails.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
holder.relativeLayoutGift.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});

holder.btnCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});

holder.btnClose.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});

final EditText editLocal = holder.editGiftMessage;

holder.btnApply.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String message = editLocal.getText().toString();
shippingOption.setGiftMessageApplied(true);
shippingOption.setGiftMessage(message);
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});

holder.editGiftMessage.setText(shippingOption.getGiftMessage());

holder.editGiftMessage.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

@Override
public void afterTextChanged(Editable s)
{
}
});

} else
{
holder.layoutGift.setVisibility(View.GONE);
holder.relativeGiftDetails.setVisibility(View.GONE);
}

return row;
}
}

最佳答案

这里我将演示一个 View 标记。

        ShippingViewHolder holder;
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}
holder = (ShippingViewHolder) row.getTag();
ShippingOption shippingOption = getShippingOption(position);
holder.tvRemove.setTag(shippingOption);
holder.tvRemove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{

ShippingOption shippingOption1 =(ShippingOption) v.getTag();

int pos = getPosition(shippingOption1); // this is the function that will return you the postion of your object from your passed list of object; write it your self.
showRemoveDialog(shippingOption1.getProductId(), pos);
}
});

关于android - ListView 不更新行 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398591/

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