我有一个问题,子活动的列表视图会更新父活动的列表视图的内容。
基本上,在我的父活动 (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();
}
};
}
}
lvContacts 的 OnItemClick 查询 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) 现在填充有 lvContactDetail 的结果。
这只发生在我的应用程序中的两个列表中,其余所有列表都 100% 正确工作。
从其他列表加载列表时,是否有任何特殊注意事项需要注意?值得注意的是,我的应用程序中的主要活动为 CLEAR_TOP 和 REORDER_TO_FRONT 设置了标志...
很奇怪:即使我注释掉子活动中的 setAdapter 行,父活动仍然会使用其他活动的数据进行更新。这可能与创建 SingleLineAdapter 的新实例有关吗?