-1

I have a list view with hym names. I also hav implemented a textchange listener to filter results on list view as per edit text input. The filter works fine but the problem is when i click on the result it calls an item on postion 0 instead of lets say item on position 7

public class MainActivity extends Activity {  
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(new         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);
}

EDIT:

All the answers are not working, please help.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
D.Freeman
  • 1
  • 1
  • 3
  • You are not maintain `RowObject` that's why this issue occur, Please check this http://stackoverflow.com/questions/23566855/after-searching-for-listview-items-in-android-always-open-first-item-of-listvie – Farmer Apr 01 '17 at 09:29
  • how do I use row object on the above – D.Freeman Apr 01 '17 at 19:31

1 Answers1

0

Use setTag() to store its original position or the id of the name on the listview item and use getTag() to determine which item has clicked

jafarbtech
  • 6,842
  • 1
  • 36
  • 55