我使用在ListView中搜索的EditText开发应用程序。我的适配器是由BaseAdapter提供的。 我在谷歌上发现了一些教程,但似乎无法正常工作。我在ArrayList中有很多人名,当我想在EditText中键入名称时,我会显示所有列表。
这是我的代码:
public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {
ListView lv;
ArrayList<Person> contactList;
CustomContact adapterContact;
//Temp
ArrayList<String> groupList = new ArrayList<>();
ArrayList<String> groupListName = new ArrayList<>();
ArrayList<Person> tempList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.bSend);
send.setOnClickListener(this);
txTempName = (TextView) findViewById(R.id.textTempName);
edNumber = (EditText)findViewById(R.id.edPhoneOrName);
edMass = (EditText)findViewById(R.id.edMass);
timepicker = (TimePicker)findViewById(R.id.timePickerMain);
timepicker.setIs24HourView(true);
datePicker = (DatePicker) findViewById(R.id.datePickerMain);
lv = (ListView) findViewById(R.id.listContact);
lv.setOnItemClickListener(this);
edNumber.addTextChangedListener(new TextWatcher() {
@SuppressWarnings("unchecked")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().equalsIgnoreCase("")) {
refreshContact();
} else {
MainActivity.this.adapterContact.getFilter().filter(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// // TODO Auto-generated method stub
}
});
}
public void refreshContact(){
DbHandContact hand = new DbHandContact(this);
hand.open();
contactList = hand.getAll();
hand.close();
Collections.sort(contactList,contactSort);
adapterContact = new CustomContact(this, contactList);
lv.setAdapter(adapterContact);
tempList = (ArrayList<Person>) contactList.clone();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(true){
String currentCellPhone = tempList.get(position).getCellPhone();
String name = tempList.get(position).getName();
groupList.add(currentCellPhone);
groupListName.add(name);
Toast.makeText(this, currentCellPhone+" "+name, 2000).show();
for (int i = 0; i < groupList.size(); i++) {
txTempName.setText(groupList.get(i));
}
}
}
CustomAdapter
My Adapter class:
public class CustomContact extends BaseAdapter implements Filterable {
Activity context;
ArrayList<Person> list;
protected ArrayList<String> temporarylist;
public CustomContact(Activity context, ArrayList<Person> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.row_contact, parent,false);
ImageView imgPerson,ImgDial;
TextView txName,txLastName,txCellnum,HomeNum;
txName = (TextView) row.findViewById(R.id.row_name);
txLastName = (TextView) row.findViewById(R.id.row_lastName);
txCellnum = (TextView) row.findViewById(R.id.row_cellNum);
imgPerson = (ImageView) row.findViewById(R.id.row_picture);
txName.setText(list.get(position).getName());
txLastName.setText(list.get(position).getLastName());
txCellnum.setText(list.get(position).getCellPhone());
return row;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
list = (ArrayList<Person>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<Person> FilteredList = new ArrayList<Person>();
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = list;
results.count = list.size();
} else {
for (int i = 0; i < list.size(); i++) {
String data = list.get(i).getName();
if (data.toLowerCase().contains(constraint.toString().toLowerCase())) {
FilteredList.add(list.get(i));
}
}
results.values = FilteredList;
results.count = FilteredList.size();
}
return results;
}
};
return filter;
}
}
它的工作正常但在我过滤后我得到了列表项的错误位置
答案 0 :(得分:0)
您似乎已在数组列表中的所有项目中设置了相同的数据
list = new ArrayList<Person>();
list.add(new Person(0, "A", "af", "dasds", "ASdfas", ""));
list.add(new Person(0, "B", "af", "dasds", "ASdfas", ""));
list.add(new Person(0, "C", "af", "dasds", "ASdfas", ""));
list.add(new Person(0, "D", "af", "dasds", "ASdfas", ""));
list.add(new Person(0, "E", "af", "dasds", "ASdfas", ""));
您可以尝试更改数据吗?
答案 1 :(得分:0)
package com.example.testproject;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
EditText ed;
ArrayList<Person> mlist;
ListView lv;
CustomContact adapter;
ArrayList<Person> tempList;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.editText);
mlist = new ArrayList<Person>();
mlist.add(new Person(0, "A"));
mlist.add(new Person(0, "B"));
mlist.add(new Person(0, "C"));
mlist.add(new Person(0, "D"));
mlist.add(new Person(0, "E"));
lv = (ListView) findViewById(R.id.listView);
adapter = new CustomContact(this, mlist);
lv.setAdapter(adapter);
tempList = (ArrayList<Person>) mlist.clone();
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.toString().trim().equalsIgnoreCase("")) {
mlist = (ArrayList<Person>) tempList.clone();
} else {
MainActivity.this.adapter.getFilter().filter(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
setVisibiltyList();
}
});
}
public void setVisibiltyList() {
if (ed.getText().toString().equals("")) {
lv.setVisibility(View.GONE);
} else {
lv.setVisibility(View.VISIBLE);
}
}
class CustomContact extends BaseAdapter implements Filterable {
Activity context;
public CustomContact(Activity context, ArrayList<Person> list) {
super();
this.context = context;
mlist = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(mlist.get(position).name);
return tv;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mlist = (ArrayList<Person>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<Person> FilteredList = new ArrayList<Person>();
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = mlist;
results.count = mlist.size();
} else {
for (int i = 0; i < mlist.size(); i++) {
String data = mlist.get(i).name;
if (data.toLowerCase().contains(
constraint.toString().toLowerCase())) {
FilteredList.add(mlist.get(i));
}
}
results.values = FilteredList;
results.count = FilteredList.size();
}
return results;
}
};
return filter;
}
}
class Person {
public String name;
public Person(int id, String name) {
this.name = name;
}
}
}