我有textview ang imagebutton的listview,单击按钮时声音播放正确但是当我搜索列表项时,我点击按钮,按钮产生错误的声音。声音放在原始文件夹中
MainActivity.java
public class MainActivity extends Activity {
ListViewAdapter adapter;
ArrayList<DictionaryApp> arrayList = new ArrayList();
String[] cuyuno;
String[] edefinition;
EditText editsearch;
String[] english;
ListView list;
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.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()));
}
});
}
}
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;
}
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
private ArrayList<DictionaryApp> arraylist;
private List<DictionaryApp> dictionaryapplist = null;
LayoutInflater inflater;
Context mContext;
MediaPlayer mp;
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());
holder.sound.setTag(Integer.valueOf(position));
holder.sound.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
switch ((int) ListViewAdapter.this.getItemId(position)) {
case R.styleable.View_android_theme /*0*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.alaala);
ListViewAdapter.this.mp.start();
return;
case R.styleable.View_android_focusable /*1*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.araw);
ListViewAdapter.this.mp.start();
return;
case R.styleable.View_paddingEnd /*3*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.baliw);
ListViewAdapter.this.mp.start();
return;
case R.styleable.View_theme /*4*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.basura);
ListViewAdapter.this.mp.start();
return;
case R.styleable.Toolbar_contentInsetStart /*5*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.kaibigan);
ListViewAdapter.this.mp.start();
return;
case R.styleable.Toolbar_contentInsetEnd /*6*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.kakatuwa);
ListViewAdapter.this.mp.start();
return;
case R.styleable.Toolbar_contentInsetLeft /*7*/:
ListViewAdapter.this.mp = MediaPlayer.create(ListViewAdapter.this.mContext, R.raw.kasunduan);
ListViewAdapter.this.mp.start();
return;
default:
return;
}
}
});
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();
}
}
我想要的是在搜索时在listview内的正确位置播放soundsin按钮...请帮助,我是android的新手......
答案 0 :(得分:0)
在@Autowired
检查此行
ListViewAdapter.java
这描述了很多。当您实施搜索操作后,您的列表会重新填充,holder.sound.setTag(Integer.valueOf(position));
的值也会更改。 position
的值表示该项目在position
中的实际位置。
让我们考虑一个例子。假设您列表中有7个项目。因此,第二项的ListView
值为1.(因为它从列表中第一项的0开始)。
现在,当您实施搜索并在列表中搜索关键字时,匹配项目的列表会变小。现在,当您致电position
时,它会重新填充notifyDatasetChanged
并相应地设置ListView
值。假设您在搜索关键字后列出了3个项目。这3个是您之前列表中的第二,第五和第七项。现在,如果你得到缩短列表中第3项position
的值,你会期望它返回6吗? (因为这是您之前列表中的第七项)否,此处position
的值为2。我想你可能已经理解了这里的诀窍。
那么在这些情况下可以做些什么呢?您可以通过多种方式跟踪列表项。您可以简单地维护第二个列表,该列表将包含每个列表项的声音音频文件,并使用唯一ID绑定每个列表项。单击该项目后,您不必查找position
值。只需获取与该列表项关联的唯一ID,并从您正在维护的音频文件列表中获取相应的音频文件。
所以我可能会建议你的 DictionaryApp.java 应该是这样的。
position