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.