1

The first time listview is initiated, headers appear as expected. The same listview when i scroll down and come up, headers disappear. This problem is solved when i remove the set and getTag codes. But that hinders the performance of the listview. What am i doing wrong here that brings up the problem.

package com.mediaplayer.adapter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;

public class ReadLisstAdapter extends BaseAdapter implements OnScrollListener {
    private Activity activity;
    ArrayList<SongInfo> song_array;
    private LayoutInflater inflater = null;
    BaseAdapter adapter;
    ListView lv;
    Thread t;
    ImageDownloader imageLoader;
    SongInfoDatabase database;
    String searchString = "";
    DecimalFormat format;
    int min, sec, total;

    public ReadLisstAdapter(Activity activity2, ArrayList<SongInfo> song_array,
            ListView lv) {
        activity = activity2;
        this.song_array = song_array;
        this.lv = lv;
        imageLoader = new ImageDownloader(this,
                activity.getApplicationContext());

        database = new SongInfoDatabase(activity.getApplicationContext());
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.lv = lv;
        imageLoader.loadImage(0, 10);
        lv.setOnScrollListener(this);
        format = new DecimalFormat("#.00");
    }

    public ArrayList<SongInfo> getUrlList() {
        return song_array;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return song_array.size();
    }

    public ListView getListView() {
        return lv;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    public class ViewHolder {
        public TextView title;
        public TextView duration;
        public TextView album;
        public TextView artist;
        public ImageView image;
        public TextView header;
        int pos;
    }

    public View getView(final int arg0, View vi, ViewGroup arg2) {
        ViewHolder holder;

        if (vi == null) {

            holder = new ViewHolder();
            vi = inflater.inflate(R.layout.songlist_item, null);
            holder.header = (TextView) vi.findViewById(R.id.label_header_textview);
            holder.title = (TextView) vi.findViewById(R.id.song_textView);
            holder.image = (ImageView) vi.findViewById(R.id.songlist_imageView);
            holder.album = (TextView) vi.findViewById(R.id.song_album_textView);
            holder.artist = (TextView) vi
                    .findViewById(R.id.song_artist_textView);
            holder.duration = (TextView) vi
                    .findViewById(R.id.song_duration_textView);
            holder.pos = arg0;
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        try {


            holder.title.setText(song_array.get(arg0).getTitle());
            total = Integer.parseInt(song_array.get(arg0).getDuration()) / 1000;
            min = total / 60;
            sec = total % 60;
            holder.duration.setText(min + ":" + sec);
            holder.album.setText("from " + song_array.get(arg0).getAlbum());
            holder.artist.setText("by " + song_array.get(arg0).getArtist());

            Uri albumArtUri = Uri
                    .parse("content://media/external/audio/albumart");
            final Uri uri = ContentUris.withAppendedId(albumArtUri,
                    Long.parseLong(song_array.get(arg0).getAlbum_id()));
            holder.image.setImageBitmap(imageLoader.getDrawble(uri.toString()));
            if (song_array.get(arg0).getTitle().trim().toUpperCase(Locale.US)
                    .charAt(0) != song_array.get(arg0 - 1).getTitle().trim()
                    .toUpperCase(Locale.US).charAt(0)) {
                setSection(holder.header, song_array.get(arg0).getTitle());
            } else {
                holder.header.setVisibility(View.GONE);

            }
        } catch (Exception e) {
            setSection(holder.header, song_array.get(arg0).getTitle());
        }

        return vi;
    }

    private void setSection(TextView text, String label) {
        text.setBackgroundColor(0xffe47168);
        text.setTextColor(Color.GREEN);
        text.setText((label.substring(0, `enter code here`1) + "").toUpperCase());
        text.setTextSize(15);
        text.setPadding(5, 0, 0, 0);
        text.setGravity(Gravity.CENTER_VERTICAL);

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        switch (scrollState) {
        case SCROLL_STATE_IDLE:
            imageLoader.loadImage(lv.getFirstVisiblePosition(),
                    lv.getLastVisiblePosition());
            break;

        }

    }

}
4

2 回答 2

1

这很简单。您只需在 if 块中设置 View.VISIBLE 即可。

 if (song_array.get(arg0).getTitle().trim().toUpperCase(Locale.US)
                .charAt(0) != song_array.get(arg0 - 1).getTitle().trim()
                .toUpperCase(Locale.US).charAt(0)) {
            setSection(holder.header, song_array.get(arg0).getTitle());
            holder.header.setVisibility(View.VISIBLE);
        } else {
            holder.header.setVisibility(View.GONE);
于 2013-06-10T12:55:53.453 回答
1

当视图被回收成为标题时,没有任何匹配项可以使标题再次可见holder.header.setVisibility(View.GONE)setVisibility(View.VISIBLE)

通常,您getView()应该在回收视图中重新配置所有内容。如果有if用于某些自定义的分支,则应该有一个else恢复默认值的分支。

于 2013-06-10T12:44:49.407 回答