之前我的问题是:Link。如果您不理解我。 我在我的应用程序上有一个带有baseAdapter类的listView,我的问题是: 用户在editText上输入一些字母,然后在listView之后返回所有" Names"用这个字母(addTextChangedListener)。现在, Evrything工作正常,但当适配器设置"新列表"结果,我无法从列表中的当前位置获取当前数据。解释理论:
我的清单是:
posistion 0.姓名:电话:111111
posistion 1.姓名:B电话:222222
posistion 2.姓名:C电话:333333
当我输入字母" C"我的清单显示了这个结果: 位置0:姓名C:电话:111111(电话是" 111111"因为该目标位置" 0")
但在" 0"我的电话价值为A。
这是我的代码:
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));
}
}
}
我的适配器类:
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)
尝试此代码过滤器部件代码仅更改:
@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 {
constraint = constraint.toString();
for (int i = 0; i < list.size(); i++) {
String data = list.get(i).getName();
if (data.toLowerCase().contains(constraint.toString())) {
FilteredList.add(list.get(i));
}
results.values = FilteredList;
results.count = FilteredList.size();
}
return results;
}
};
return filter;
}
和editext代码在这里:
search.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Call back the Adapter with current character to Filter
mAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:0)
你可以通过两种情况中的一种来解决这个问题,你可以使用应用程序类,当它在适配器中更新时,在应用程序类中更新它,然后你可以从activity / fragment中随时访问
和使用共享偏好来保存它的第二个选项以及在任何地方检索它的相同