我有一個問題,其中Child Activity的列表視圖更新了父活動的列表視圖的內容。子活動ListView更新父活動的ListView
基本上,在我父活動(DetailActivity)我有一個列表視圖叫我用下面的代碼填充lvContacts:
ListView lvDetail = (ListView)findViewById(R.id.lvContacts);
SingleLineAdapter adapter = new SingleLineAdapter(this, Items);
lvDetail.setAdapter(adapter);
lvDetail.setSelector(R.drawable.generic_selector);
如果項目是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();
}
};
}
}
The OnItemClic K表示用於在web服務的數據的查詢lvContacts然後示出了新的活動:
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的結果。
這隻發生在我的應用程序中的兩個列表,其餘所有工作都正常工作。
從其他列表加載列表時,是否有任何特別的注意事項需要注意?值得注意的是,我的應用程序中的主要活動爲CLEAR_TOP和REORDER_TO_FRONT設置了標誌...
足夠奇怪:即使我在子活動中註釋掉setAdapter行,父活動仍然會與其他活動一起更新數據。這可能與創建SingleLineAdapter的新實例有關嗎?