自定义列表视图包含图像按钮,单击按钮时播放声音

时间:2016-03-28 10:48:33

标签: java android

我有包含textview和imagebutton的自定义Lisview ...我想为一行中的每个按钮播放特定的音频文件。音频文件在原始文件夹中,我该怎么做?

MainActivity.java
public class MainActivity extends Activity {
ListViewAdapter adapter;
ArrayList<DictionaryApp> arrayList = new ArrayList();
String[] cuyuno;
String[] edefinition;
EditText editsearch;
String[] english;
ListView list;
int[] sound;
String[] tagalog;
String[] tbaybay;
String[] tdefinition;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.tagalog = new String[]{"alaala", "araw", "baliw", "basura", "kahirapan", "kaibigan", "kakatuwa", "kasunduan", "daluyang-luha", "dambuhala", "dulo", "dukutin", "gawin", "guni-guni", "hagdan", "hintay", "idlip", "idolo", "maganda", "masama", "masarap", "matalino", "nagtanan", "nawala", "pagbibitiw", "paikliin"};
    this.tbaybay = getResources().getStringArray(R.array.tbaybay);
    this.tdefinition = getResources().getStringArray(R.array.tdefinition);
    this.cuyuno = getResources().getStringArray(R.array.cuyuno);
    this.english = getResources().getStringArray(R.array.eword);
    this.edefinition = getResources().getStringArray(R.array.edefinition);
    this.sound = new int[]{R.raw.alaala, R.raw.araw, R.raw.baliw, R.raw.basura, R.raw.kaibigan, R.raw.kakatuwa, R.raw.kasunduan};
    this.list = (ListView) findViewById(R.id.listview);
    for (int i = 0; i < this.tagalog.length; i++) {
        this.arrayList.add(new DictionaryApp(this.tagalog[i], this.tbaybay[i], this.tdefinition[i], this.cuyuno[i], this.english[i], this.edefinition[i]));
    }
    this.adapter = new ListViewAdapter(this, this.arrayList);
    this.list.setAdapter(this.adapter);
    this.editsearch = (EditText) findViewById(R.id.search);
    this.editsearch.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void afterTextChanged(Editable arg0) {
            MainActivity.this.adapter.filter(MainActivity.this.editsearch.getText().toString().toLowerCase(Locale.getDefault()));
        }
    });
}
}



ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
private ArrayList<DictionaryApp> arraylist;
private List<DictionaryApp> dictionaryapplist = null;
LayoutInflater inflater;
Context mContext;

public class ViewHolder {
    TextView cuyuno;
    TextView edefinition;
    TextView english;
    ImageButton sound;
    TextView tagalog;
    TextView tbaybay;
    TextView tdefinition;
}

public ListViewAdapter(Context context, List<DictionaryApp> dictionaryapplist) {
    this.mContext = context;
    this.dictionaryapplist = dictionaryapplist;
    this.inflater = LayoutInflater.from(this.mContext);
    this.arraylist = new ArrayList();
    this.arraylist.addAll(dictionaryapplist);
}

public int getCount() {
    return this.dictionaryapplist.size();
}

public DictionaryApp getItem(int position) {
    return (DictionaryApp) this.dictionaryapplist.get(position);
}

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

public View getView(final int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = this.inflater.inflate(R.layout.listview_item, null);
        holder.tagalog = (TextView) view.findViewById(R.id.tagalog);
        holder.tbaybay = (TextView) view.findViewById(R.id.tbaybay);
        holder.tdefinition = (TextView) view.findViewById(R.id.tdefinition);
        holder.cuyuno = (TextView) view.findViewById(R.id.cuyuno);
        holder.english = (TextView) view.findViewById(R.id.english);
        holder.edefinition = (TextView) view.findViewById(R.id.edefinition);
        holder.sound = (ImageButton) view.findViewById(R.id.sound);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    holder.tagalog.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getTagalog());
    holder.tbaybay.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getTbaybay());
    holder.tdefinition.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getTdefinition());
    holder.cuyuno.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getCuyuno());
    holder.english.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getEnglish());
    holder.edefinition.setText(((DictionaryApp) this.dictionaryapplist.get(position)).getEdefinition());

    view.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            Intent intent = new Intent(ListViewAdapter.this.mContext, SingleItemView.class);
            intent.putExtra("tagalog", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getTagalog());
            intent.putExtra("tbaybay", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getTbaybay());
            intent.putExtra("tdefinition", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getTdefinition());
            intent.putExtra("cuyuno", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getCuyuno());
            intent.putExtra("english", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getEnglish());
            intent.putExtra("edefinition", ((DictionaryApp) ListViewAdapter.this.dictionaryapplist.get(position)).getEdefinition());
            ListViewAdapter.this.mContext.startActivity(intent);
        }
    });
    return view;
}

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    this.dictionaryapplist.clear();
    if (charText.length() == 0) {
        this.dictionaryapplist.addAll(this.arraylist);
    } else {
        Iterator i$ = this.arraylist.iterator();
        while (i$.hasNext()) {
            DictionaryApp da = (DictionaryApp) i$.next();
            if (da.getTagalog().toLowerCase(Locale.getDefault()).contains(charText)) {
                this.dictionaryapplist.add(da);
            }
        }
    }
    notifyDataSetChanged();
}
}


DictionaryApp.java
public class DictionaryApp {
private String cuyuno;
private String edefinition;
private String english;
private String tagalog;
private String tbaybay;
private String tdefinition;

public DictionaryApp(String tagalog, String tbaybay, String tdefinition, String cuyuno, String english, String edefinition) {
    this.tagalog = tagalog;
    this.tbaybay = tbaybay;
    this.tdefinition = tdefinition;
    this.cuyuno = cuyuno;
    this.english = english;
    this.edefinition = edefinition;
}

public String getTagalog() {
    return this.tagalog;
}

public String getTbaybay() {
    return this.tbaybay;
}

public String getTdefinition() {
    return this.tdefinition;
}

public String getCuyuno() {
    return this.cuyuno;
}

public String getEnglish() {
    return this.english;
}

public String getEdefinition() {
    return this.edefinition;
}   

}

音频文件在原始文件夹中,我想要出来的是音频将在listview内的图像按钮中播放....我是android的新手,帮助是我最欣赏的...谢谢

1 个答案:

答案 0 :(得分:0)

要播放媒体文件,您可以这样做: https://stackoverflow.com/a/16797922/1750013

要为每个行播放不同的音频,您可以使用switch case根据行号选择不同的文件。多态性是另一种选择。否则你必须更具体地了解应用程序逻辑,然后只有某人能够建议另一个逻辑。希望它有所帮助:)