如何使用此搜索过滤ListView使用OnListItemClickListener

时间:2014-06-08 16:19:41

标签: android listview android-listview

我在listview中使用setOnItemClickListener时遇到问题。每次我尝试时,它都会导致太多错误,修复程序会使代码比我打算做的更糟。

这是我的代码:

public class Search extends Activity{
// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;


// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

// Listview Data
final String products[] = {"Fill in the Blocks", "The Music Bee", "Little Paragon",
"Subway Surfers", "Cytus", "Temple Run", "Clumsy Ninja", "Smash Hit"};

lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);

// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name,        products);
lv.setAdapter(adapter);

/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Search.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                          
}            
});    
}       
}

我想让listview数据中的字符串可以为我的其他活动点击,但它不起作用。我试过这段代码:

 lv.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 String openClass = adapter.getItem[position];
 if (openClass.equals("Fill in the Blocks")) {
   Intent myIntent = new Intent(view.getContext(), Fill_in_the_Blocks.class);
   startActivityForResult(myIntent, 0);
   }
 else if (openClass.equals("The Music Bee")) {
   Intent myIntent1 = new Intent(view.getContext(), The_Music_Bee.class);
   startActivityForResult(myIntent1, 0);
   }
 else if (openClass.equals("Little Paragon")) {
   Intent myIntent2 = new Intent(view.getContext(), Little_Paragon.class);
   startActivityForResult(myIntent2, 0);
   }
   }

错误如下: - 在lv.setOnItemClickListener中,“AdapterView类型中的方法setOnItemClickListener(AdapterView.OnItemClickListener)不适用于参数(new OnItemClickListener(){})” - 在(new OnItemClickListener()中,“OnItemClickListener无法解析为类型” - 在public void onItemClick(AdapterView,“AdapterView无法解析为类型”) - 在String openClass = adapter.getItem [position];中,“getItem无法解析或不是字段”

1 个答案:

答案 0 :(得分:0)

  • 在lv.setOnItemClickListener中,“AdapterView类型中的方法setOnItemClickListener(AdapterView.OnItemClickListener)不适用于参数(new OnItemClickListener(){})”
  • 在(new OnItemClickListener()中,“OnItemClickListener无法解析为类型”
  • 在public void onItemClick(AdapterView,“AdapterView无法解析为类型”

通过用

替换新的OnItemClickListener(),前三个错误应该消失
new AdapterView.OnItemClickListener()

并确保导入:

import android.widget.AdapterView;

最后的错误:

  • 在String openClass = adapter.getItem [position];中,“getItem无法解析或不是字段”

您应该调用getItem方法,而不是使用数组索引。

替换行:

String openClass = adapter.getItem[position];

String openClass = adapter.getItem(position);