在android中获取适配器的子视图

时间:2012-01-04 03:35:25

标签: android android-listview adapter custom-adapter

我有一个带有2个布局的适配器,fisrt one是带有细节的歌曲项目,第二个布局是带有细节的歌曲项目和用于当前歌曲选择布局的播放位置的搜索栏。我的适配器在这里:

package org.mrhands.player.adapter;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.mrhands.player.DataHelper;
import org.mrhands.player.R;
import org.mrhands.player.model.AudioFile;
import org.mrhands.player.service.MusicService;

import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class LaguListAdapter extends BaseAdapter implements SectionIndexer {
AlphabetIndexer indexer;
List<AudioFile> listAudioFile;
Context context;
final int LIST_LAGU = 0;
final int LIST_LAGU_WITH_SEEKBAR = 1;
int currentlyPlay = -1;
OnSeekBarChangeListener onSeekBarChangeListener;
HashMap<String, Integer> alphaIndexer;

/**
 * parameter ini digunakan untuk mendapatkan seekbar dari view yang terpilih
 * dari adapter
 * 
 * @param currentlyPlay
 */
public void setCurrentlyPlay(int currentlyPlay) {
    this.currentlyPlay = currentlyPlay;
}
public AudioFile getCurrentPlay(){
    return listAudioFile.get(currentlyPlay);
}

String[] sections;
String sortedColumn = MediaStore.Audio.Media.DISPLAY_NAME;
MusicService service;
public LaguListAdapter(Context context, List<AudioFile> listAudioFile,OnSeekBarChangeListener listener,MusicService service) {
    this.context = context;
    this.onSeekBarChangeListener=listener;
    this.service=service;
    this.listAudioFile = listAudioFile;
    alphaIndexer = new HashMap<String, Integer>();
    // in this hashmap we will store here the positions for
    // the sections

    int size = listAudioFile.size();
    for (int i = size - 1; i >= 0; i--) {
        AudioFile element = listAudioFile.get(i);
        alphaIndexer.put(element.getTitle().substring(0, 1), i);
        // We store the first letter of the word, and its index.
        // The Hashmap will replace the value for identical keys are putted
        // in
    }

    // now we have an hashmap containing for each first-letter
    // sections(key), the index(value) in where this sections begins

    // we have now to build the sections(letters to be displayed)
    // array .it must contains the keys, and must (I do so...) be
    // ordered alphabetically

    Set<String> keys = alphaIndexer.keySet(); // set of letters ...sets
    // cannot be sorted...

    Iterator<String> it = keys.iterator();
    ArrayList<String> keyList = new ArrayList<String>(); // list can be
    // sorted

    while (it.hasNext()) {
        String key = it.next();
        keyList.add(key);
    }

    Collections.sort(keyList);

    sections = new String[keyList.size()]; // simple conversion to an
    // array of object
    keyList.toArray(sections);

}

class ViewHolder {
    TextView textJudulLagu;
    TextView textPenyanyi;
    ImageView imageLagu;
    SeekBar seekbar;
    int ref;

}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder = new ViewHolder();
    int viewType = getItemViewType(position);//ambil tipe dari layout adapter
    if (view == null) {
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (viewType == LIST_LAGU) {//cek jika layout yang terpilih adalah list lagu maka gunakan layout list lagu
            view = inflate.inflate(R.layout.list_lagu, null);
        } else if (viewType == LIST_LAGU_WITH_SEEKBAR) {//yang ini menggunakan layout seekbar
            view = inflate.inflate(R.layout.list_lagu_with_seekbar, null);
            holder.seekbar = (SeekBar) view.findViewById(R.id.seek_lagu);
            holder.seekbar.setMax((int) service.getCurrentPlay().getDuration());
            holder.seekbar.setOnSeekBarChangeListener(onSeekBarChangeListener);

        }
        holder.imageLagu = (ImageView) view
                .findViewById(R.id.list_lagu_gambar);
        holder.textPenyanyi = (TextView) view
                .findViewById(R.id.list_lagu_penyanyi);
        holder.textJudulLagu = (TextView) view
                .findViewById(R.id.list_lagu_judul);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    holder.ref = position;
    AudioFile file = listAudioFile.get(position);
    holder.textPenyanyi.setText(file.getArtist());
    holder.textJudulLagu.setText(file.getTitle());
    Bitmap b;
    try {
        b = DataHelper.getAlbumArtBitmap(context, file);
        holder.imageLagu.setImageBitmap(b);
    } catch (FileNotFoundException e) {
        holder.imageLagu.setImageDrawable(context.getResources()
                .getDrawable(R.drawable.albumart));
    }
    return view;
}

public int getPositionForSection(int section) {
    String letter = sections[section];
    return alphaIndexer.get(letter);
}

@Override
public int getItemViewType(int position) {
    return position == currentlyPlay ? LIST_LAGU_WITH_SEEKBAR : LIST_LAGU;
}

@Override
public int getViewTypeCount() {
    return 2;
}

public int getSectionForPosition(int position) {
    // you will notice it will be never called (right?)
    Log.v("getSectionForPosition", "called");
    return 0;
}

public Object[] getSections() {
    return sections;
}

public int getCount() {
    return listAudioFile.size();
}

}

在这个适配器中,我只设置当用户使用onItemClickListener单击适配器时显示的搜索栏,然后我想在所选适配器上获取Seekbar。我该怎么做?我做了这样的方法..但不行。

public Seekbar getSeekbar(){
  ViewHolder view=(ViewHolder)getView(currentlyPlay,null,null);
  return view.seekBar;
}

我在onItemClick上设置了currentPlay视图,这样我就可以获得当前位置所选视图..但这对我不起作用..你能帮帮忙吗?

1 个答案:

答案 0 :(得分:2)

您可以通过

获取具体位置的视图
View clickedView = yourAdapter.getView(position, null, null);