如何在用户在searchView上搜索的指定行上放置onclicklistner。香港专业教育学院一直试图弄清楚如何,但当我这样做时,它总是显示第一行的吐司或Uri链接。任何人都可以帮助我吗?
例如,当我搜索ABC时,我想点击它以查看吐司或Uri链接,当我点击它时。
Employee.java
public class Employee {
private String name;
private int age;
public Employee(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}}
EmployeeAdapter.java
public class EmployeeAdapter extends BaseAdapter implements Filterable {
public Context context;
public ArrayList<Employee> employeeArrayList;
public ArrayList<Employee> orig;
public EmployeeAdapter(Context context, ArrayList<Employee> employeeArrayList) {
super();
this.context = context;
this.employeeArrayList = employeeArrayList;
}
public class EmployeeHolder
{
TextView name;
TextView age;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Employee> results = new ArrayList<Employee>();
if (orig == null)
orig = employeeArrayList;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final Employee g : orig) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
employeeArrayList = (ArrayList<Employee>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return employeeArrayList.size();
}
@Override
public Object getItem(int position) {
return employeeArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
EmployeeHolder holder;
if(convertView==null)
{
convertView=LayoutInflater.from(context).inflate(R.layout.row, parent, false);
holder=new EmployeeHolder();
holder.name=(TextView) convertView.findViewById(R.id.txtName);
holder.age=(TextView) convertView.findViewById(R.id.txtAge);
convertView.setTag(holder);
}
else
{
holder=(EmployeeHolder) convertView.getTag();
}
holder.name.setText(employeeArrayList.get(position).getName());
holder.age.setText(String.valueOf(employeeArrayList.get(position).getAge()));
return convertView;
}}
MainActivity.java
public class MainActivity extends Activity implements SearchView.OnQueryTextListener{
private SearchView mSearchView;
private ListView mListView;
private ArrayList<Employee> employeeArrayList;
private EmployeeAdapter employeeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchView=(SearchView) findViewById(R.id.searchView1);
mListView=(ListView) findViewById(R.id.listView1);
employeeArrayList=new ArrayList<Employee>();
employeeArrayList.add(new Employee("ABC", 24));
employeeArrayList.add(new Employee("ACB", 24));
employeeArrayList.add(new Employee("BVF", 28));
employeeArrayList.add(new Employee("BRT", 28));
employeeArrayList.add(new Employee("ANM", 23));
employeeAdapter=new EmployeeAdapter(MainActivity.this, employeeArrayList);
mListView.setAdapter(employeeAdapter);
mListView.setTextFilterEnabled(true);
setupSearchView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Employee pick = employeeArrayList.get(position);
String message = "You Selected " + pick.getName();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
private void setupSearchView()
{
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
@Override
public boolean onQueryTextChange(String newText)
{
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return false;
}
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}}
提前谢谢!
答案 0 :(得分:-1)
简单,
在你的onItemClickListener()中,尝试使用employeeadapter.getItem()而不是employeearraylist.get()。尝试并对结果发表评论。 :d
顺便说一下,您的适配器已经加载了回收物品的工作。将过滤负载放在它上面也不是一个好主意。
只需在您的活动内部进行过滤并使其返回一个arraylist并将列表适配器设置为此arraylist并再次调用setOnItemClickListener()以更改现有的onItemClickListener()。