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不能保证顺序,所以我对它们进行排序。
List> maps = new ArrayList>(); ArrayList sections = new ArrayList(); HashMap letters = 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方法……
发布于 2011-07-20 05:50:26
我曾多次看到getSectionForPosition以与getPositionForSection相同的方式实现
public int getPositionForSection(int section) {
String letter = (String) sections[section];
return letters.get(letter);
}
public int getSectionForPosition(int position) {
String letter = (String) sections[position];
return letters.get(letter);
}发布于 2015-03-31 20:07:45
DR的第一个答案不可能是正确的。如果需要getSectionForPosition方法的示例实现,请参见here。
您说得对,这看起来像是您的getSectionForPosition实现出了问题。到目前为止提供的答案不可能是正确的,因为您需要返回某个位置相关的部分的索引,而不是一个部分相关的位置(这就是您的getPositionForSection方法应该做的)。
因此,您应该(再次)修改您的getSectionForPosition方法。您需要将每个位置有效地映射到一个区段。文档中写道:
在给定适配器内的位置的情况下,
将返回section对象数组中相应section的索引。
如果您需要有关如何构建此类映射的示例,请参阅here。如果正确实现,它将解决跳转滚动条的问题,并正确定位它。
https://stackoverflow.com/questions/6750248
复制相似问题