自定义listview包含imagebutton,单击按钮时播放声音

eimct9ow  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(226)

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

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;
}   

}

音频文件在原始文件夹中,我想出来的是,音频将在列表视图内的imagebutton中播放…我是android的新手,帮助是我最感激的…谢谢

q3qa4bjr

q3qa4bjr1#

要播放媒体文件,您可以这样做:https://stackoverflow.com/a/16797922/1750013
要为每行播放不同的音频,可以使用switch case根据行号选择不同的文件。多态性是另一种选择。否则,您必须更具体地了解应用程序逻辑,然后只有其他人才能建议其他逻辑。希望有帮助:)

相关问题