我正在尝试在android studio中学习java,但我发现自己遇到了问题。
在我查找项目的列表视图中,它显示了正确的匹配名称,但是当我点击播放时,它会播放另一个项目,而不是搜索到的项目。
如果他们使用searchview有任何解决方案,那就更好了。 谢谢
public class MainActivity extends AppCompatActivity {
private EditText et;
private ArrayList<String> lstEstados_Encontrados = new ArrayList<String>();
// variable declaration
private ListView mainList;
private MediaPlayer mp;
private final String[] listContent = {
"NHA", "iisso",
"Rolezeira",
};
private final int[] resID = {
R.raw.ne, R.raw.eso,
R.raw.rolezeira,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
et = (EditText) findViewById(R.id.etProcurar);
mp = new MediaPlayer();
mainList = (ListView) findViewById(listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
mainList.setAdapter(adapter);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
playSong(position);
}
});
//Carrega o listview com todos os itens
mainList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContent));
CarregarEncontrados();
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Abstract Method of TextWatcher Interface.
}
//Evento acionado quando o usuário teclar algo
//na caixa de texto "Procurar"
public void onTextChanged(CharSequence s, int start, int before, int count) {
CarregarEncontrados();
//Carrega o listview com os itens encontrados
mainList.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lstEstados_Encontrados));
}
});
}
public void CarregarEncontrados() {
int textlength = et.getText().length();
//Limpa o array com os estados encontrados
//para poder efetuar nova busca
lstEstados_Encontrados.clear();
for (int i = 0; i < listContent.length; i++) {
if (textlength <= listContent[i].length()) {
//Verifica se existe algum item no array original
//caso encontre é adicionado no array de encontrados
if (et.getText().toString().equalsIgnoreCase((String)listContent[i].subSequence(0, textlength))) {
lstEstados_Encontrados.add(listContent[i]);
}
}
}
}
public void playSong(int songIndex) {
mp.reset();
mp = MediaPlayer.create(getApplicationContext(), resID[songIndex]);// create's
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
答案 0 :(得分:0)
搜索和过滤项目时,请勿对所选项目使用位置。这会让你选择错误的项目。因为,与当前屏幕上显示的项目相关的位置。
我的解决方案:
在布局中设置textView,以便在其中存储对象ID。然后在setOnItemClickListener方法中从您选择的项目中获取 ID 。然后选择具有所选ID的相应正确数据,而不是位置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txt_id"
android:testSize="0dp"
android:layout_width="wrap_content"
android:layout_height="0dp" />
...
...
... other element
</LinearLayout >
在java代码中:
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
String id=((TextView)view.findViewById(R.id.txt_id)).getText().toString();
// playSong(id);
}
});