我有一個EditText
和ListView
MainActiviy。 EditText
用於搜索和過濾ListView
的產品。我有兩個數組;有兩個數組;如何保存舊陣列索引位置後過濾ListView
報紙[]擁有一些報上名,
ADRESS []擁有報紙的網絡ADRESS。我也有一個setOnItemClickListener
。在聽衆中,我得到了物品的位置,並且在該索引號的幫助下,我從adress[ ]
數組中獲取地址信息。
首先沒有篩選器的搜索框。我向EditText添加了addTextChangedListener
以過濾ListView的項目。問題從這裏開始!
當我寫一些東西給搜索框時,它正在過濾正確,但過濾後,如果我點擊任何項目,它會打開舊項目的地址。
實施例:
假設,
甲
乙
Ç
d
是過濾器前項和在陣列位置A項的是0,B = 1,C = 2,D = 3
如果我寫信只搜索「C」C在過濾後可見,如果我點擊「C」項,它會打開A的網站。因爲「A」項目是篩選前的第一項。
如何在過濾ListView後更新數組索引位置?
public class MainActivity extends Activity {
// Search EditText
EditText inputSearch;
// Listview Adapter
ArrayAdapter<String> adapter;
protected static final String SITE_ADDRESS = null;
// protected static final String SITE_NAME = null;
private ListView newspaper_list_view;
private String[] newspapers = { "A","B","C","D" };
private String[] adress = { "a.com","b.com","c.com","d.com" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setContentView(R.layout.webview);
inputSearch = (EditText) findViewById(R.id.inputSearch);
newspaper_list_view = (ListView) findViewById(R.id.gazeteliste);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, newspapers);
newspaper_list_view.setAdapter(adapter);
final Intent intent = new Intent(this, GazeteIcerik.class);
newspaper_list_view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int listposition, long arg3) {
// Toast.makeText(getApplicationContext(),"Liste Sırası:" +
// listposition, Toast.LENGTH_SHORT).show();
String site_adress = adress[listposition];
String site_name = newspapers[listposition];
intent.putExtra(SITE_ADDRESS, site_adress);
// intent.putExtra(SITE_NAME, site_name);
getActionBar().setDisplayHomeAsUpEnabled(true);
intent.putExtra(SITE_ADDRESS, new String[] { site_adress,site_name });
startActivity(intent);
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
// onCreate sonu
}
是必要的CustomAdapter與HashMap或我可以實現它ArrayAdapter? – hakiko
使用散列表,選項1,您仍然可以使用ArrayAdapter。 – ayorhan
添加了示例代碼 – ayorhan