1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| abstract public class ResultActivity extends ListActivity {
SearchQuery currentQuery = null;
ResultSearch mainResult = null;
List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
List<Map<String, String>> prepareList = new ArrayList<Map<String, String>>();
boolean searching = false;
boolean hasFocus = false;
final Handler uiThreadCallback = new Handler();
final Runnable runInUIThreadOnScroll = new Runnable() {
public void run() {
onUpdateResultList();
adapter.notifyDataSetChanged();
onUpdateTitle();
getParent().setProgressBarIndeterminateVisibility(false);
searching = false;
}
};
final Runnable runInUIThreadOnResultSearch = new Runnable() {
public void run() {
onUpdateResultList();
adapter.notifyDataSetChanged();
onUpdateTitle();
getParent().setProgressBarIndeterminateVisibility(false);
searching = false;
}
};
private Context context;
private SimpleAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_list);
context = this;
String[] from = {"id", "name", "locality", "subAdminArea", "category", "date", "price", "image", "urgent"};
int[] to = {R.id.ad_detail_id, R.id.ad_detail_name, R.id.ad_detail_locality, R.id.ad_detail_subadmin_area, R.id.ad_detail_category, R.id.ad_detail_date, R.id.ad_detail_price};
adapter = new SimpleAdapterWithRemoteImage(this.getApplicationContext(), resultList, R.layout.ad_row, from, to);
setListAdapter(adapter);
ListView listView = getListView();
listView.setOnScrollListener(new OnScrollListener() {
int lastItem = 0;
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( adapter!=null && mainResult!=null && currentQuery!=null && !searching) {
if (lastItem == getListAdapter().getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE && adapter.getCount()<mainResult.nbAds) {
searching = true;
getParent().setProgressBarIndeterminateVisibility(true);
new Thread() {
public void run() {
try {
currentQuery.page++;
ResultSearch rs = ProjectService.searchItems(context, currentQuery);
if ( mainResult==null) {
mainResult = rs;
} else if (mainResult.ads==null) {
mainResult.ads = rs.ads;
} else {
mainResult.ads.addAll(rs.ads);
}
prepare(rs);
} catch (ProjectException e) {
currentQuery.page--;
}
uiThreadCallback.post(runInUIThreadOnScroll);
}
}.start();
} else {}
} else {}
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount;
}
});
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent);
} else {}
}
public void onResume() {
super.onResume();
hasFocus = true;
if (searching==false) {
onUpdateTitle();
}
}
public void onPause() {
super.onPause();
hasFocus = false;
}
public void onNewIntent(final Intent newIntent) {
super.onNewIntent(newIntent);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent);
} else {}
}
private void doSearchQuery(final Intent queryIntent) {
SearchQuery query = queryIntent.getExtras().getParcelable(SearchResultsActivity.INTENT_EXTRA_SEARCH_QUERY);
query = (SearchQuery)query.clone();
doSearchQuery(query);
}
private void doSearchQuery(SearchQuery query) {
if (mainResult!=null) {
mainResult = null;
resultList.clear();
prepareList.clear();
}
currentQuery = query;
searching = true;
getParent().setProgressBarIndeterminateVisibility(true);
getParent().setTitle(getString(R.string.search_title_processing, currentQuery.search));
new Thread() {
public void run() {
try {
mainResult = ProjectService.searchItems(context, currentQuery);
prepare(mainResult);
} catch ( ProjectException e) {
}
uiThreadCallback.post(runInUIThreadOnResultSearch);
}
}.start();
}
private void onUpdateTitle() {
if ( currentQuery!=null/* && hasWindowFocus()*/ && hasFocus) {
if ( mainResult!=null && mainResult.nbAds>0) {
if ( currentQuery.search!=null && currentQuery.search.length()>0) {
getParent().setTitle(getString(R.string.search_title_result, Math.min(currentQuery.page*ProjectService.SEARCH_PAGE_SIZE, mainResult.nbAds), mainResult.nbAds, currentQuery.search));
} else {
getParent().setTitle(getString(R.string.search_title_result_for_category, Math.min(currentQuery.page*ProjectService.SEARCH_PAGE_SIZE, mainResult.nbAds), mainResult.nbAds));
}
} else {
getParent().setTitle(getString(R.string.search_title_no_results));
}
} else {
getParent().setTitle(getString(R.string.search_query_results));
}
}
private void prepare(ResultSearch rs) {
if ( rs!=null && rs.ads!=null) {
if ( !rs.ads.isEmpty()) {
for (ClassifiedAd classifiedAd : rs.ads) {
if ( classifiedAd!=null) {
Map<String, String> map = new HashMap<String, String>();
map.put("id", classifiedAd.id);
map.put("url", classifiedAd.url);
map.put("name", classifiedAd.name);
map.put("locality", classifiedAd.city);
map.put("subAdminArea", classifiedAd.department);
map.put("category", classifiedAd.category);
map.put("date", classifiedAd.createDate);
map.put("price", classifiedAd.price);
map.put("image", classifiedAd.thumb);
prepareList.add(map);
}
}
}
} else {}
}
private void onUpdateResultList() {
resultList.addAll(prepareList);
prepareList.clear();
}
} |