【发布时间】:2011-07-19 16:00:12
【问题描述】:
我正在研究一个 ListActivity,它有一个内部类,它是实现 SectionIndexer 的 SimpleAdapter 的子类。
class MySimpleAdapter extends SimpleAdapter implements SectionIndexer {
HashMap<String, Integer> letters;
Object[] sections;
AlphabetIndexer alphaIndexer;
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to,
HashMap<String, Integer> letters, Object[] sections) {
super(context, data, resource, from, to);
this.letters = letters;
this.sections = sections;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.common_image_row1, null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
Integer idArtist = Integer.parseInt((String) data.get("idArtist"));
convertView.setTag(idArtist);
((TextView) convertView.findViewById(R.id.item_name))
.setText((String) data.get("sName"));
ImageView image = (ImageView) convertView.findViewById(R.id.item_image);
image.setTag((String) data.get("sImageUrl"));
image.setImageResource(R.drawable.default_artwork);
((TextView) convertView.findViewById(R.id.item_count))
.setText((String) data.get("iSongs") + " Songs");
if (!bScrolling) {
new API.DownloadImagesTask().execute(image);
}
return convertView;
}
public int getPositionForSection(int section) {
String letter = (String) sections[section];
return letters.get(letter);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
}
活动在单独的 AysncTask 中接收 JSON 对象。该对象由各种 JSONArrays 组成,它们的键是数组项的第一个字母。因此,数组由一堆以字母“B”开头的项目组成。因此,那个 JSONArray 的键是“B”。
{
B: ["ball", "buck", "bill"]
C: ["charlie", "chuck", "chap"]
}
对象不一定包含所有字母。我也知道 JSONObjects 不能保证顺序,所以我对它们进行了排序。
列表>地图=新的ArrayList>();
ArrayList 部分 = new ArrayList();
HashMap 字母 = new HashMap();
String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"
,"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int k = 0;
for (int i = 0; i < alphabet.length; i++) {
if (playlistArtists.optJSONArray(alphabet[i]) != null) {
try {
JSONArray artists = playlistArtists.getJSONArray(alphabet[i]);
sections.add(alphabet[i]);
for (int j = 0; j < artists.length(); j++) {
JSONObject artist = (JSONObject) artists.get(j);
HashMap<String, String> map= new HashMap<String, String>();
map.put("idArtist", artist.getString("idArtist"));
map.put("sName", artist.getString("sName"));
map.put("sImageUrl", artist.getString("sImageUrl-thumb"));
map.put("iSongs", artist.getString("iSongs"));
maps.add(map);
letters.put(alphabet[i], k);
k++;
}
} catch (JSONException e) {
Log.d(TAG,"JSONException in Music::GetMusicCatlog.doInBackground");
e.printStackTrace();
}
}
}
SimpleAdapter adapter = new MySimpleAdapter(Catalog.this,
maps,
R.layout.common_image_row1,
new String[] {"idArtist","sName", "sImageUrl", "iSongs" },
new int[] {R.id.item_id, R.id.item_name, R.id.item_image, R.id.item_count },
letters,
sections.toArray());
setListAdapter(adapter);
我遇到的问题是 FastScroll。我大部分时间都在工作。该列表按第一个字母分组,当使用 FastScroll 时,该字母出现在弹出窗口中并转到正确的组。问题是当我在转到所需部分后松开 FastScroll 时,FastScroll “跳转”到列表的随机部分。它不会停留在我放开它的位置。我认为它会在该部分中的任意位置,因为我没有正确实施 SectionIndexer。我认为问题出在这种方法上。
public int getSectionForPosition(int position) {
return 0;
}
我只是不确定如何正确实现 SectionIndexer 方法...
【问题讨论】:
-
作为旁注:getPositionForSection() 应该返回起始索引,而不是最后一个索引(您正在执行的操作)。因此,假设艺术家是按 A-Z 排序的,要么向后遍历艺术家数组,要么仅在该键尚不存在时将索引添加到字母哈希图中(因此您将仅添加第一个索引)
标签: android listview listadapter fastscroll