我遇到问题,其中Child Activity的列表视图更新了Parent Activity的列表视图的内容。
基本上,在我的父活动(DetailActivity)中,我有一个名为lvContacts的列表视图,我使用以下代码填充:
ListView lvDetail = (ListView)findViewById(R.id.lvContacts);
SingleLineAdapter adapter = new SingleLineAdapter(this, Items);
lvDetail.setAdapter(adapter);
lvDetail.setSelector(R.drawable.generic_selector);
其中Items是GenericDataObject的ArrayList:
public class GenericDataObject implements Serializable {
private static final long serialVersionUID = 7661717999129942666L;
private String Id;
private String Description;
private String RawData;
// getters and setters snipped for smaller code
}
SingleLineAdapter看起来像这样:
public class SingleLineAdapter extends CustomListAdapter implements Filterable {
private static ArrayList<GenericDataObject> ObjectArray;
private static ArrayList<GenericDataObject> OriginalArray;
private LayoutInflater layoutInflater;
private String SearchedText = "";
private Context OriginatingContext;
public SingleLineAdapter(Context context, ArrayList<GenericDataObject> Objects) {
OriginatingContext = context;
ObjectArray = Objects;
OriginalArray = Objects;
layoutInflater = LayoutInflater.from(context);
if (Objects != null)
Collections.sort(Objects, ObjectComparator);
}
private Comparator<GenericDataObject> ObjectComparator = new Comparator<GenericDataObject>() {
@Override
public int compare(GenericDataObject object1,
GenericDataObject object2) {
return object1.getDescription().compareTo(object2.getDescription());
}
};
@Override
public int getCount() {
return ObjectArray.size();
}
@Override
public Object getItem(int position) {
return ObjectArray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listitem_generic, null);
holder = new ViewHolder();
holder.lblTitle = (TextView)convertView.findViewById(R.id.lblTitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.lblTitle.setText(ObjectArray.get(position).getDescription(), TextView.BufferType.SPANNABLE);
if (!SearchedText.equals("")) {
Spannable wordToSpan = (Spannable)holder.lblTitle.getText();
String[] comps;
if (SearchedText.startsWith("\""))
comps = new String[] { SearchedText.substring(1, SearchedText.length() - 1) };
else
comps = SearchedText.split(" ");
String name = holder.lblTitle.getText().toString().toLowerCase();
for(int i = 0; i < comps.length; i++) {
int Start = name.indexOf(comps[i]);
while (Start > -1) {
int End = Start + comps[i].length();
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFF9900), Start, End, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Start = name.indexOf(comps[i], End);
}
}
}
setFont(holder.lblTitle);
return convertView;
}
static class ViewHolder {
TextView lblTitle;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
SearchedText = constraint.toString();
final FilterResults result = new FilterResults();
final ArrayList<GenericDataObject> custs = new ArrayList<GenericDataObject>();
if(constraint != null) {
String[] searches;
if (constraint.toString().startsWith("\"") && constraint.toString().endsWith("\""))
searches = new String[] { SearchedText.substring(1, SearchedText.length() - 1) };
else
searches = constraint.toString().split(" ");
for(int i = 0; i < OriginalArray.size(); i++) {
String customerName = OriginalArray.get(i).getDescription().toLowerCase();
boolean bNotFound = false;
for(int j = 0; j < searches.length && bNotFound == false; j++) {
String searchFor = searches[j];
if (!customerName.contains(searchFor))
bNotFound = true;
}
if (!bNotFound)
{
custs.add(OriginalArray.get(i));
}
}
result.values = custs;
}
return result;
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint,
FilterResults results) {
ObjectArray = (ArrayList<GenericDataObject>)results.values;
notifyDataSetChanged();
}
};
}
}
OnItemClick for lvContacts对Web服务上的数据的查询然后显示一个新的Activity:
Intent detail = new Intent(DetailActivity.this, ContactDetailActivity.class);
detail.putExtra("ListOfDetails", RowsFromService);
startActivity(detail);
RowsFromService也是GenericDataObject的ArrayList。
在ContactDetailActivity中,我有一个名为lvContactDetail的ListView。我填写这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<GenericDataObject> DetailRows = (ArrayList<GenericDataObject>)getIntent().getExtras().getSerializable("ListOfDetails");
ListView lvContactDetail = (ListView)findViewById(R.id.lvContactDetail);
SingleLineAdapter detailAdapter = new SingleLineAdapter(this, DetailRows);
lvContactDetail.setAdapter(detailAdapter);
lvContactDetail.setSelector(R.drawable.generic_selector);
}
到目前为止,所有内容都能完美运行,但如果我使用后退按钮甚至按钮上的activity.finish()返回到DetailActivity(父活动),则原始列表(lvContacts)现在会填充结果of lvContactDetail。
这只发生在我的应用程序中的两个列表中,其余列表都正常工作。
从其他列表中加载列表时是否有任何特殊的预防措施需要注意?值得注意的是,我的应用程序中的主要活动为CLEAR_TOP和REORDER_TO_FRONT设置了标志......
奇怪的是:即使我在子活动中注释掉setAdapter行,父活动也会继续使用其他活动的数据进行更新。这可能与创建SingleLineAdapter的新实例有关吗?
答案 0 :(得分:0)
似乎我是唯一真正遇到此问题的人,所以我所做的就是在onCreate中“重新加载”父活动的UI部分。 (保持布尔值以指示数据是否已加载)