我正在尝试创建一个每行有2个editText的listView,它们使用TextWatcher接口来识别我们是否更改了editText上的当前文本我的问题是,我的适配器是非常非常滞后和缓慢。我该如何处理这个问题?这是我的适配器:
public class AccountListInfoAdapter2 extends BaseAdapter {
private Activity mContext;
private ArrayList<ModelClass> accounts;
private DisplayImageOptions options;
private LayoutInflater inflater;
private prevClass mainActivityFragment;
private ViewHolder holder;
public AccountListInfoAdapter2(Activity activity, ArrayList<ModelClass> mAccountListData, prevClass mainActivityFragment) {
this.mContext = activity;
this.accounts = mAccountListData;
this.mainActivityFragment = mainActivityFragment;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
if (accounts == null) {
return 0;
} else
return accounts.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
view = inflater.inflate(R.layout.account_info_row, parent, false);
holder = new ViewHolder();
assert view != null;
setViews(view);
view.setTag(holder);
holder.emailAddress.setText(accounts.get(position).getEmail());
holder.fullName.setText(accounts.get(position).getFullName());
holder.fullName.setId(position);
holder.emailAddress.setId(position);
//Check if full name has been changed
holder.fullName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != count) {
if (!accounts.get(position).getFullName().equals(s.toString())) {
accounts.get(position).setFullName(s.toString());
}
}
}
@Override
public void afterTextChanged(Editable s) {
if (!accounts.get(position).getFullName().equals(s.toString())) {
accounts.get(position).setFullName(s.toString());
}
}
});
//Check if email name has been changed
holder.emailAddress.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != count) {
if (!accounts.get(position).getEmail().equals(s.toString())){
accounts.get(position).getEmail(s.toString());
}
}
}
@Override
public void afterTextChanged(Editable s) {
if (!accounts.get(position).getEmail().equals(s.toString())) {
accounts.get(position).getEmail(s.toString());
}
}
});
return view;
}
public static class ViewHolder {
TextView emailAddress;
EditText fullName;
}
public void setViews(View view){
holder.emailAddress = (EditText) view.findViewById(R.id.accountInfoProfileEmailAddress);
holder.fullName = (EditText) view.findViewById(R.id.accountInfoFullName);
}
}
holder.fullName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != count)
{
if (!accounts.get(position).getFullName().equals(s.toString())) {
//accounts.get(position).setFullName(s.toString());
((MainActivity)context).changeListViewItem(position , s.toString);
}
}
}
@Override
public void afterTextChanged(Editable s) {
if (!accounts.get(position).getFullName().equals(s.toString())) {
//accounts.get(position).setFullName(s.toString());
((MainActivity)context).changeListViewItem(position , s.toString);
}
}
});
在主要活动上添加此功能
public void changeListViewItem(int position , String text)
{
Log.e("" , ""+position);
Log.e("" , ""+text);
refreshAdapter();
}
1。CCD_ 1可以添加重复。
U可以尝试在onTextChanged
中添加日志。
当您键入char
时,请注意它记录了多少次。
使用addTextChangedListener
时,添加一个tag
。
2.U应该尝试使用ViewHolder
来帮助U。