2017-04-01 73 views
-1

我有一個帶有hym名稱的列表視圖。我還實現了一個textchange監聽器來根據編輯文本輸入過濾列表視圖上的結果。該過濾器工作正常,但問題是,當我在它調用現在的位置是一個項目的結果0,而不是可以說,在位置項單擊7列表視圖上的文本更改偵聽器

公共類MainActivity擴展活動

{ 
private ListView list; 

ArrayAdapter<String> adapter; 
// Search EditText 
EditText search; 
// ArrayList for Listview 
ArrayList<HashMap<String,  String>> 
colourList; 

@Override 
protected void    onCreate(Bundle  savedInstanceState) 
{ 
          super.onCreate(savedInstanceState); 
           setContentView(R.layout.main); 

list=(ListView)      findViewById(R.id.list1); 

String[] colours = new  String[] 
{ 
"Black", 
"Brown", 
"Cream", 
"Green", 
"Meroon", 
"orange", 
"Purple", 
"Red", 
"White", 
"Yellow"}; 

adapter = new ArrayAdapter <  String > (this,         R.layout.list_item,     R.id.colour_names, colours); 

list = (ListView)  findViewById(R.id.list1); 
list.setAdapter(adapter); 
search = (EditText) 
findViewById(R.id.search); 

search.addTextChangedListener(新TextWatcher(){

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


// When user changed the Text 



         MainActivity.this.adapter.getFilter().filter(cs); 


} 




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 

}}); 

list.setOnItemClickListener(new   AdapterView.OnItemClickListener() 
{ 
public void  onItemClick(AdapterView<?> 
parent,View view, 
int positions,long id) 
{ 
if(positions==0) 
{ 
Intent myIntent=new  Intent(view.getContext(),          Black.class); 
         startActivityForResult(myIntent,0); 
} 
if(positions==1) 
{ 
Intent myIntent=new Intent(view.getContext(), 
               Brown.class); 
        startActivityForResult(myIntent,0); 
} 
       if(positions==2) 
       { 
Intent myIntent=new Intent 
        (view.getContext(), 
        Cream.class); 
         startActivityForResult(myIntent,0); 
} 
if(positions==3) 
{ 
Intent myIntent=new Intent 
(view.getContext(), 
        Green.class); 
        startActivityForResult(myIntent,0); 
} 
        if(positions==4) 
{ 
Intent myIntent=new Intent(view.getContext(), 
        Meroon.class); 
        startActivityForResult(myIntent,0);  
} 
if(positions==5) 
{ 
Intent myIntent=new       Intent(view.getContext(), 

Orange.class); startActivityForResult(myIntent,0); }

編輯:

所有的答案都不能正常工作,請幫助。

+1

代碼?代碼在哪裏? –

+0

你不維護'RowObject'這就是爲什麼會出現這個問題,請檢查這個http://stackoverflow.com/questions/23566855/after-searching-for-listview-items-in-android-always-open-first-item -of-listvie – Shailesh

+0

如何在上面使用行對象 –

回答

0

使用setTag()來存儲它原來的位置或名稱的列表視圖項目的ID和使用getTag(),確定哪個項目已單擊