列表视图

时间:2017-04-01 09:19:27

标签: android

我有一个带有hym名字的列表视图。我还实现了一个textchange监听器来根据编辑文本输入过滤列表视图上的结果。过滤器工作正常,但问题是当我点击结果时它调用位置0上的项目而不是让位置7上的项目

公共类MainActivity扩展了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); }

编辑:

所有答案都无效,请帮忙。

1 个答案:

答案 0 :(得分:0)

使用setTag()在listview项目上存储其原始位置或名称ID,并使用getTag()确定点击了哪个项目